Debian Codegraph @

python/examples/tx_send.py

raw
1 #!/usr/bin/env python3
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 import optparse
22 from proton import Message, Url
23 from proton.reactor import Container
24 from proton.handlers import MessagingHandler, TransactionHandler
25
26
27 class TxSend(MessagingHandler, TransactionHandler):
28 def __init__(self, url, messages, batch_size):
29 super(TxSend, self).__init__()
30 self.url = Url(url)
31 self.current_batch = 0
32 self.committed = 0
33 self.confirmed = 0
34 self.total = messages
35 self.batch_size = batch_size
36
37 def on_start(self, event):
38 self.container = event.container
39 self.conn = self.container.connect(self.url)
40 self.sender = self.container.create_sender(self.conn, self.url.path)
41 self.container.declare_transaction(self.conn, handler=self)
42 self.transaction = None
43
44 def on_transaction_declared(self, event):
45 self.transaction = event.transaction
46 self.send()
47
48 def on_sendable(self, event):
49 self.send()
50
51 def send(self):
52 while self.transaction and self.sender.credit and (self.committed + self.current_batch) < self.total:
53 seq = self.committed + self.current_batch + 1
54 msg = Message(id=seq, body={'sequence': seq})
55 self.transaction.send(self.sender, msg)
56 self.current_batch += 1
57 if self.current_batch == self.batch_size:
58 self.transaction.commit()
59 self.transaction = None
60
61 def on_accepted(self, event):
62 if event.sender == self.sender:
63 self.confirmed += 1
64
65 def on_transaction_committed(self, event):
66 self.committed += self.current_batch
67 if self.committed == self.total:
68 print("all messages committed")
69 event.connection.close()
70 else:
71 self.current_batch = 0
72 self.container.declare_transaction(self.conn, handler=self)
73
74 def on_disconnected(self, event):
75 self.current_batch = 0
76
77
78 parser = optparse.OptionParser(usage="usage: %prog [options]",
79 description="Send messages transactionally to the supplied address.")
80 parser.add_option("-a", "--address", default="localhost:5672/examples",
81 help="address to which messages are sent (default %default)")
82 parser.add_option("-m", "--messages", type="int", default=100,
83 help="number of messages to send (default %default)")
84 parser.add_option("-b", "--batch-size", type="int", default=10,
85 help="number of messages in each transaction (default %default)")
86 opts, args = parser.parse_args()
87
88 try:
89 Container(TxSend(opts.address, opts.messages, opts.batch_size)).run()
90 except KeyboardInterrupt:
91 pass