|
| 1 | +/* |
| 2 | + * |
| 3 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. The ASF licenses this file |
| 7 | + * to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with the License. You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + * |
| 20 | + */ |
| 21 | + |
| 22 | +#include "options.hpp" |
| 23 | + |
| 24 | +#include <proton/connection.hpp> |
| 25 | +#include <proton/container.hpp> |
| 26 | +#include <proton/message.hpp> |
| 27 | +#include <proton/message_id.hpp> |
| 28 | +#include <proton/messaging_handler.hpp> |
| 29 | +#include <proton/types.hpp> |
| 30 | +#include <proton/transaction.hpp> |
| 31 | + |
| 32 | +#include <iostream> |
| 33 | +#include <map> |
| 34 | +#include <string> |
| 35 | + |
| 36 | +#include <chrono> |
| 37 | +#include <thread> |
| 38 | + |
| 39 | +class tx_recv : public proton::messaging_handler, proton::transaction_handler { |
| 40 | + private: |
| 41 | + proton::receiver receiver; |
| 42 | + std::string url; |
| 43 | + int expected; |
| 44 | + int batch_size; |
| 45 | + int current_batch = 0; |
| 46 | + int committed = 0; |
| 47 | + |
| 48 | + proton::session session; |
| 49 | + proton::transaction transaction; |
| 50 | + public: |
| 51 | + tx_recv(const std::string &s, int c, int b): |
| 52 | + url(s), expected(c), batch_size(b) {} |
| 53 | + |
| 54 | + void on_container_start(proton::container &c) override { |
| 55 | + receiver = c.open_receiver(url); |
| 56 | + } |
| 57 | + |
| 58 | + void on_session_open(proton::session &s) override { |
| 59 | + session = s; |
| 60 | + std::cout << " [on_session_open] declare_txn started..." << std::endl; |
| 61 | + s.declare_transaction(*this); |
| 62 | + std::cout << " [on_session_open] declare_txn ended..." << std::endl; |
| 63 | + } |
| 64 | + |
| 65 | + void on_transaction_declare_failed(proton::transaction) {} |
| 66 | + void on_transaction_commit_failed(proton::transaction t) { |
| 67 | + std::cout << "Transaction Commit Failed" << std::endl; |
| 68 | + t.connection().close(); |
| 69 | + exit(-1); |
| 70 | + } |
| 71 | + |
| 72 | + void on_transaction_declared(proton::transaction t) override { |
| 73 | + std::cout << "[on_transaction_declared] txn called " << (&t) |
| 74 | + << std::endl; |
| 75 | + std::cout << "[on_transaction_declared] txn is_empty " << (t.is_empty()) |
| 76 | + << "\t" << transaction.is_empty() << std::endl; |
| 77 | + receiver.add_credit(batch_size); |
| 78 | + transaction = t; |
| 79 | + } |
| 80 | + |
| 81 | + void on_message(proton::delivery &d, proton::message &msg) override { |
| 82 | + std::cout<<"# MESSAGE: " << msg.id() <<": " << msg.body() << std::endl; |
| 83 | + transaction.accept(d); |
| 84 | + current_batch += 1; |
| 85 | + if(current_batch == batch_size) { |
| 86 | + transaction = proton::transaction(); // null |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void on_transaction_committed(proton::transaction t) override { |
| 91 | + committed += current_batch; |
| 92 | + current_batch = 0; |
| 93 | + std::cout<<" [OnTxnCommitted] Committed:"<< committed<< std::endl; |
| 94 | + if(committed == expected) { |
| 95 | + std::cout << "All messages committed" << std::endl; |
| 96 | + t.connection().close(); |
| 97 | + } |
| 98 | + else { |
| 99 | + session.declare_transaction(*this); |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | +}; |
| 104 | + |
| 105 | +int main(int argc, char **argv) { |
| 106 | + std::string address("127.0.0.1:5672/examples"); |
| 107 | + int message_count = 9; |
| 108 | + int batch_size = 3; |
| 109 | + example::options opts(argc, argv); |
| 110 | + |
| 111 | + opts.add_value(address, 'a', "address", "connect and send to URL", "URL"); |
| 112 | + opts.add_value(message_count, 'm', "messages", "number of messages to send", "COUNT"); |
| 113 | + opts.add_value(batch_size, 'b', "batch_size", "number of messages in each transaction", "BATCH_SIZE"); |
| 114 | + |
| 115 | + try { |
| 116 | + opts.parse(); |
| 117 | + |
| 118 | + tx_recv recv(address, message_count, batch_size); |
| 119 | + proton::container(recv).run(); |
| 120 | + |
| 121 | + return 0; |
| 122 | + } catch (const example::bad_option& e) { |
| 123 | + std::cout << opts << std::endl << e.what() << std::endl; |
| 124 | + } catch (const std::exception& e) { |
| 125 | + std::cerr << e.what() << std::endl; |
| 126 | + } |
| 127 | + |
| 128 | + return 1; |
| 129 | +} |
0 commit comments