| 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.handlers import MessagingHandler |
| 23 |
from proton.reactor import Container |
| 24 |
|
| 25 |
|
| 26 |
class Recv(MessagingHandler): |
| 27 |
def __init__(self, url, count): |
| 28 |
super(Recv, self).__init__() |
| 29 |
self.url = url |
| 30 |
self.expected = count |
| 31 |
self.received = 0 |
| 32 |
|
| 33 |
def on_start(self, event): |
| 34 |
self.acceptor = event.container.listen(self.url) |
| 35 |
|
| 36 |
def on_message(self, event): |
| 37 |
if event.message.id and event.message.id < self.received: |
| 38 |
# ignore duplicate message |
| 39 |
return |
| 40 |
if self.expected == 0 or self.received < self.expected: |
| 41 |
print(event.message.body) |
| 42 |
self.received += 1 |
| 43 |
if self.received == self.expected: |
| 44 |
event.receiver.close() |
| 45 |
event.connection.close() |
| 46 |
self.acceptor.close() |
| 47 |
|
| 48 |
|
| 49 |
parser = optparse.OptionParser(usage="usage: %prog [options]") |
| 50 |
parser.add_option("-a", "--address", default="localhost:5672/examples", |
| 51 |
help="address from which messages are received (default %default)") |
| 52 |
parser.add_option("-m", "--messages", type="int", default=100, |
| 53 |
help="number of messages to receive; 0 receives indefinitely (default %default)") |
| 54 |
opts, args = parser.parse_args() |
| 55 |
|
| 56 |
try: |
| 57 |
Container(Recv(opts.address, opts.messages)).run() |
| 58 |
except KeyboardInterrupt: |
| 59 |
pass |