Debian Codegraph @

python/examples/tx_recv.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 Url
23 from proton.reactor import Container
24 from proton.handlers import MessagingHandler, TransactionHandler
25
26
27 class TxRecv(MessagingHandler, TransactionHandler):
28 def __init__(self, url, messages, batch_size):
29 super(TxRecv, self).__init__(prefetch=0, auto_accept=False)
30 self.url = Url(url)
31 self.expected = messages
32 self.batch_size = batch_size
33 self.current_batch = 0
34 self.committed = 0
35
36 def on_start(self, event):
37 self.container = event.container
38 self.conn = self.container.connect(self.url)
39 self.receiver = self.container.create_receiver(self.conn, self.url.path)
40 self.container.declare_transaction(self.conn, handler=self)
41 self.transaction = None
42
43 def on_message(self, event):
44 print(event.message.body)
45 self.transaction.accept(event.delivery)
46 self.current_batch += 1
47 if self.current_batch == self.batch_size:
48 self.transaction.commit()
49 self.transaction = None
50
51 def on_transaction_declared(self, event):
52 self.receiver.flow(self.batch_size)
53 self.transaction = event.transaction
54
55 def on_transaction_committed(self, event):
56 self.committed += self.current_batch
57 self.current_batch = 0
58 if self.expected == 0 or self.committed < self.expected:
59 self.container.declare_transaction(self.conn, handler=self)
60 else:
61 event.connection.close()
62
63 def on_disconnected(self, event):
64 self.current_batch = 0
65
66
67 parser = optparse.OptionParser(usage="usage: %prog [options]")
68 parser.add_option("-a", "--address", default="localhost:5672/examples",
69 help="address from which messages are received (default %default)")
70 parser.add_option("-m", "--messages", type="int", default=100,
71 help="number of messages to receive; 0 receives indefinitely (default %default)")
72 parser.add_option("-b", "--batch-size", type="int", default=10,
73 help="number of messages in each transaction (default %default)")
74 opts, args = parser.parse_args()
75
76 try:
77 Container(TxRecv(opts.address, opts.messages, opts.batch_size)).run()
78 except KeyboardInterrupt:
79 pass