3333#include < map>
3434#include < string>
3535
36+ #include < chrono>
37+ #include < thread>
38+
3639class tx_send : public proton ::messaging_handler, proton::transaction_handler {
3740 private:
3841 proton::sender sender;
3942 std::string url;
4043 int total;
4144 int batch_size;
4245 int sent;
46+ int batch_index = 0 ;
4347 int current_batch = 0 ;
4448 int committed = 0 ;
4549 int confirmed = 0 ;
50+
4651 proton::container *container;
4752 // proton::transaction_handler transaction_handler;
4853 proton::transaction transaction;
@@ -56,68 +61,90 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
5661 sender = c.open_sender (url);
5762 connection = sender.connection ();
5863 std::cout << " [on_container_start] declare_txn started..." << std::endl;
59- transaction = c.declare_transaction (connection, *this );
60- std::cout << " [on_container_start] completed!! txn: " << &transaction << std::endl;
64+ c.declare_transaction (connection, *this );
65+ std::cout << " [on_container_start] completed!!" << &transaction
66+ << std::endl;
6167 }
6268
63- void on_transaction_aborted (proton::transaction) {}
6469 void on_transaction_declare_failed (proton::transaction) {}
65- void on_transaction_commit_failed (proton::transaction) {}
70+ void on_transaction_commit_failed (proton::transaction) {
71+ std::cout << " Transaction Commit Failed" << std::endl;
72+ connection.close ();
73+ exit (-1 );
74+ }
6675
6776 void on_transaction_declared (proton::transaction t) override {
68- std::cout << " [on_transaction_declared] txn: " << (&transaction)
69- << " new_txn: " << (t._impl ->id ) << std::endl;
70- connection.close ();
71- // transaction = &t;
72- // ASSUME: THIS FUNCTION DOESN"T WORK
73- // send();
77+ std::cout << " [on_transaction_declared] txn called " << (&t)
78+ << std::endl;
79+ // connection.close();
80+ std::cout << " [on_transaction_declared] txn is_empty " << (t.is_empty ())
81+ << " \t " << transaction.is_empty () << std::endl;
82+ transaction = t;
83+
84+ send (sender);
7485 }
7586
7687 void on_sendable (proton::sender &s) override {
7788 // send();
78- // std::cout<<" [OnSendable] transaction: "<< &transaction << std::endl;
79- // send(s);
89+ std::cout << " [OnSendable] transaction: " << &transaction
90+ << std::endl;
91+ send (s);
8092 }
8193
8294 void send (proton::sender &s) {
8395 // TODO: Add more condition in while loop
84- // transaction != null
85- while ( sender.credit () && (committed + current_batch) < total)
86- {
96+ while (!transaction.is_empty () && sender.credit () &&
97+ (committed + current_batch) < total) {
8798 proton::message msg;
8899 std::map<std::string, int > m;
89100 m[" sequence" ] = committed + current_batch;
90101
91102 msg.id (committed + current_batch + 1 );
92103 msg.body (m);
104+ std::cout << " [example] transaction send msg: " << msg
105+ << std::endl;
93106 transaction.send (sender, msg);
94107 current_batch += 1 ;
95108 if (current_batch == batch_size)
96109 {
97- transaction.commit ();
98- // transaction = NULL;
110+ std::cout << " >> Txn attempt commit" << std::endl;
111+ if (batch_index % 2 == 0 ) {
112+ transaction.commit ();
113+ } else {
114+ transaction.abort ();
115+ }
116+
117+ transaction = proton::transaction ();
118+ batch_index++;
99119 }
100120 }
101-
102121 }
103122
104123 void on_tracker_accept (proton::tracker &t) override {
105124 confirmed += 1 ;
125+ std::cout << " [example] on_tracker_accept:" << confirmed
126+ << std::endl;
106127 }
107128
108129 void on_transaction_committed (proton::transaction t) override {
109130 committed += current_batch;
131+ current_batch = 0 ;
110132 std::cout<<" [OnTxnCommitted] Committed:" << committed<< std::endl;
111133 if (committed == total) {
112- std::cout << " All messages committed" ;
113- // connection.close();
134+ std::cout << " All messages committed" << std::endl ;
135+ connection.close ();
114136 }
115137 else {
116- // current_batch = 0;
117- // container->declare_transaction(connection, transaction_handler);
138+ container->declare_transaction (connection, *this );
118139 }
119140 }
120141
142+ void on_transaction_aborted (proton::transaction t) override {
143+ std::cout << " Meesages Aborted ....." << std::endl;
144+ current_batch = 0 ;
145+ container->declare_transaction (connection, *this );
146+ }
147+
121148 void on_sender_close (proton::sender &s) override {
122149 current_batch = 0 ;
123150 }
@@ -126,8 +153,8 @@ class tx_send : public proton::messaging_handler, proton::transaction_handler {
126153
127154int main (int argc, char **argv) {
128155 std::string address (" 127.0.0.1:5672/examples" );
129- int message_count = 10 ;
130- int batch_size = 10 ;
156+ int message_count = 6 ;
157+ int batch_size = 3 ;
131158 example::options opts (argc, argv);
132159
133160 opts.add_value (address, ' a' , " address" , " connect and send to URL" , " URL" );
0 commit comments