Commit feb8cf22 authored by aikchar's avatar aikchar Committed by GitHub

Merge pull request #2 from codeghar/add-examples

Add examples
parents 6b3d9d22 4a7acfe3
......@@ -2,6 +2,5 @@ import logging
import sys
FORMAT = "%(asctime)s : %(pathname)s : %(name)s : %(module)s : %(funcName)s : %(lineno)d : %(levelname)s : %(message)s"
date_format = "%Y%m%d-%H%M%S-%f"
logging.basicConfig(format=FORMAT, level=logging.DEBUG, stream=sys.stdout)
logger = logging.getLogger("brokerlso")
......@@ -9,8 +9,11 @@ class RequestCmd:
self.object_name = "org.apache.qpid.broker:broker:amqp-broker"
logger.debug("object name -> {0}".format(self.object_name))
self.properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
logger.debug("Message properties -> {0}".format(self.properties))
self.method_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Message properties -> {0}".format(self.method_properties))
self.query_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
logger.debug("Message properties -> {0}".format(self.query_properties))
def create_queue(self, name, strict=True, auto_delete=False, auto_delete_timeout=10):
"""Create message content and properties to create queue with QMFv2
......@@ -28,7 +31,7 @@ class RequestCmd:
Default: 10
:type auto_delete_timeout: int
:returns: Tuple containing content and properties
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "create",
......@@ -39,7 +42,7 @@ class RequestCmd:
"qpid.auto_delete_timeout": auto_delete_timeout}}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def create_exchange(self, name, type_="fanout", strict=True, auto_delete=False, auto_delete_timeout=10):
"""Create message content and properties to create exchange with QMFv2
......@@ -47,7 +50,7 @@ class RequestCmd:
:param name: Name of exchange to create
:type name: str
:param type_: Type of exchange to create
Possible values are fanout, ...?
Possible values are: direct, fanout, topic?
:type type_: str
:param strict: Whether command should fail when unrecognized properties are provided
Not used by QMFv2
......@@ -60,7 +63,7 @@ class RequestCmd:
Default: 10
:type auto_delete_timeout: int
:returns: Tuple containing content and properties
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "create",
......@@ -72,7 +75,7 @@ class RequestCmd:
"exchange-type": type_}}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def create_binding(self, name, strict=True, auto_delete=False, auto_delete_timeout=10):
"""Create message content and properties to create binding with QMFv2
......@@ -90,7 +93,7 @@ class RequestCmd:
Default: 10
:type auto_delete_timeout: int
:returns: Tuple containing content and properties
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "create",
......@@ -101,65 +104,73 @@ class RequestCmd:
"qpid.auto_delete_timeout": auto_delete_timeout}}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def delete_queue(self, name):
"""Create message content and properties to delete queue with QMFv2
:param name: Name of queue to delete
:type name: str
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "delete",
"options": {"type": "queue", "name": name, "options": dict()}}
"_arguments": {"type": "queue",
"name": name,
"options": dict()}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def delete_exchange(self, name):
"""Create message content and properties to delete exchange with QMFv2
:param name: Name of exchange to delete
:type name: str
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "delete",
"options": {"type": "exchange", "name": name, "options": dict()}}
"_arguments": {"type": "exchange", "name": name, "options": dict()}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def delete_binding(self, name):
"""Create message content and properties to delete exchange with QMFv2
:param name: Name of exchange to delete in format "exchange/queue/key"
:type name: str
:returns: Tuple containing content and method properties
"""
content = {"_object_id": {"_object_name": self.object_name},
"_method_name": "delete",
"options": {"type": "binding", "name": name, "options": dict()}}
"_arguments": {"type": "binding", "name": name, "options": dict()}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.method_properties
def list_queues(self):
"""Create message content and properties to list all queues with QMFv2
:returns: Tuple containing content and properties
:returns: Tuple containing content and query properties
"""
content = {"_what": "OBJECT",
"_schema_id": {"_class_name": "queue"}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.query_properties
def list_exchanges(self):
"""Create message content and properties to list all exchanges with QMFv2
:returns: Tuple containing content and properties
:returns: Tuple containing content and query properties
"""
content = {"_what": "OBJECT",
"_schema_id": {"_class_name": "exchange"}}
logger.debug("Message content -> {0}".format(content))
return content, self.properties
return content, self.query_properties
"""Examples to list, create, and delete queues and exchanges using Qpid Proton and brokerlso
Requires user to provide a valid URL to a working broker when running the script
"""
import logging
import sys
from proton import Message
from proton.handlers import MessagingHandler
from proton.reactor import Container
from brokerlso.qmfv2 import RequestCmd
FORMAT = "%(asctime)s : %(pathname)s : %(name)s : %(module)s : %(funcName)s : %(lineno)d : %(levelname)s : %(message)s"
logging.basicConfig(format=FORMAT, level=logging.DEBUG, stream=sys.stdout)
logger = logging.getLogger("brokerlso-example1")
class Example1(MessagingHandler):
"""Example usage of brokerlso to list, create, and delete queues and exchanges with QMFv2"""
def __init__(self, url):
super(Example1, self).__init__()
self.default_exchange = "qmf.default.direct"
logger.debug("default exchange for request messages {0}".format(self.default_exchange))
self.default_subject = "broker"
logger.debug("subject {0}".format(self.default_subject))
self.url = "/".join([url, self.default_exchange])
logger.debug("url {0}".format(self.url))
reqcmd = RequestCmd()
self.request_messages = [reqcmd.list_queues(),
reqcmd.create_queue("MYFIRSTQUEUE"),
reqcmd.list_queues(),
reqcmd.delete_queue("MYFIRSTQUEUE"),
reqcmd.list_exchanges(),
reqcmd.create_exchange("MYFIRSTEXCAHNGE"),
reqcmd.list_exchanges(),
reqcmd.delete_exchange("MYFIRSTEXCAHNGE")]
logger.debug("requests {0}".format(self.request_messages))
def on_start(self, event):
self.sender = event.container.create_sender(self.url)
self.receiver = event.container.create_receiver(self.sender.connection, None, dynamic=True)
def next_message(self):
if self.receiver.remote_source.address:
content, properties = self.request_messages[0]
logger.debug("message content/body {0}".format(content))
logger.debug("message properties {0}".format(properties))
message = Message(reply_to=self.receiver.remote_source.address,
body=content,
properties=properties,
subject=self.default_subject)
logger.debug("request message {0}".format(message))
self.sender.send(message)
def on_link_opened(self, event):
if event.receiver == self.receiver:
self.next_message()
def on_message(self, event):
latest_message = self.request_messages.pop(0)
logger.debug("latest message {0}".format(latest_message))
latest_response = event.message.body
logger.debug("latest response {0}".format(latest_response))
if self.request_messages:
self.next_message()
else:
event.connection.close()
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Run example1 of using brokerlso with Qpid Proton')
parser.add_argument('url', action='store',
help='Valid amqp(s) URL, e.g. amqp://user:password@localhost:5672')
args = parser.parse_args()
logger.debug("user provided url {0}".format(args.url))
url = args.url[:-1] if args.url[-1] == "/" else args.url # Remove last "/" if it exists as it's added in Example1
logger.debug("fixed url {0}".format(url))
Container(Example1(url=url)).run()
brokerlso
python-qpid-proton==0.14.0
......@@ -29,7 +29,7 @@ class TestRequestBinding:
"qpid.auto_delete_timeout": 10}}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......@@ -46,10 +46,10 @@ class TestRequestBinding:
expected_content = {"_object_id": {"_object_name": "org.apache.qpid.broker:broker:amqp-broker"},
"_method_name": "delete",
"options": {"type": "binding", "name": binding, "options": dict()}}
"_arguments": {"type": "binding", "name": binding, "options": dict()}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......
......@@ -31,7 +31,7 @@ class TestRequestExchange:
"exchange-type": exchange_type}}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......@@ -48,10 +48,10 @@ class TestRequestExchange:
expected_content = {"_object_id": {"_object_name": "org.apache.qpid.broker:broker:amqp-broker"},
"_method_name": "delete",
"options": {"type": "exchange", "name": exchange, "options": dict()}}
"_arguments": {"type": "exchange", "name": exchange, "options": dict()}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......
......@@ -29,7 +29,7 @@ class TestRequestQueue:
"qpid.auto_delete_timeout": 10}}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......@@ -46,10 +46,10 @@ class TestRequestQueue:
expected_content = {"_object_id": {"_object_name": "org.apache.qpid.broker:broker:amqp-broker"},
"_method_name": "delete",
"options": {"type": "queue", "name": queue, "options": dict()}}
"_arguments": {"type": "queue", "name": queue, "options": dict()}}
logger.debug("Expected content -> {0}".format(expected_content))
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_query_request", "method": "request"}
expected_properties = {"x-amqp-0-10.app-id": "qmf2", "qmf.opcode": "_method_request", "method": "request"}
logger.debug("Expected properties -> {0}".format(expected_properties))
assert content == expected_content
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment