| 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 |
| 23 |
from proton.handlers import MessagingHandler |
| 24 |
from proton.reactor import Container |
| 25 |
|
| 26 |
|
| 27 |
class Send(MessagingHandler): |
| 28 |
def __init__(self, url, messages): |
| 29 |
super(Send, self).__init__() |
| 30 |
self.url = url |
| 31 |
self.sent = 0 |
| 32 |
self.confirmed = 0 |
| 33 |
self.total = messages |
| 34 |
|
| 35 |
def on_start(self, event): |
| 36 |
event.container.create_sender(self.url) |
| 37 |
|
| 38 |
def on_sendable(self, event): |
| 39 |
while event.sender.credit and self.sent < self.total: |
| 40 |
msg = Message(id=(self.sent + 1), body={'sequence': (self.sent + 1)}) |
| 41 |
event.sender.send(msg) |
| 42 |
self.sent += 1 |
| 43 |
|
| 44 |
def on_accepted(self, event): |
| 45 |
self.confirmed += 1 |
| 46 |
if self.confirmed == self.total: |
| 47 |
print("all messages confirmed") |
| 48 |
event.connection.close() |
| 49 |
|
| 50 |
def on_disconnected(self, event): |
| 51 |
self.sent = self.confirmed |
| 52 |
|
| 53 |
|
| 54 |
parser = optparse.OptionParser(usage="usage: %prog [options]", |
| 55 |
description="Send messages to the supplied address.") |
| 56 |
parser.add_option("-a", "--address", default="localhost:5672/examples", |
| 57 |
help="address to which messages are sent (default %default)") |
| 58 |
parser.add_option("-m", "--messages", type="int", default=100, |
| 59 |
help="number of messages to send (default %default)") |
| 60 |
opts, args = parser.parse_args() |
| 61 |
|
| 62 |
try: |
| 63 |
Container(Send(opts.address, opts.messages)).run() |
| 64 |
except KeyboardInterrupt: |
| 65 |
pass |