import pika from PikaBus.PikaBusSetup import PikaBusSetup # Use pika connection params to set connection details credentials = pika.PlainCredentials('amqp', 'amqp') connParams = pika.ConnectionParameters( host='localhost', port=5672, virtual_host='/', credentials=credentials) # Create a PikaBusSetup instance without a listener queue pikaBusSetup = PikaBusSetup(connParams) # Create a temporary bus to publish messages. bus = pikaBusSetup.CreateBus() payload = {'hello': 'world!', 'reply': False} # To publish a message means publishing a message on a topic received by any subscribers of the topic. bus.Publish(payload=payload, topic='myTopic')
if payload['reply']: payload['reply'] = False bus.Reply(payload=payload) # Use pika connection params to set connection details credentials = pika.PlainCredentials('amqp', 'amqp') connParams = pika.ConnectionParameters( host='localhost', port=5672, virtual_host='/', credentials=credentials) # Create a PikaBusSetup instance with a listener queue, and add the message handler method. pikaBusSetup = PikaBusSetup(connParams, defaultListenerQueue='myQueue', defaultSubscriptions='myTopic') pikaBusSetup.AddMessageHandler(MessageHandlerMethod) # Start consuming messages from the queue. pikaBusSetup.StartConsumers() # Create a temporary bus to subscribe on topics and send, defer or publish messages. bus = pikaBusSetup.CreateBus() bus.Subscribe('myTopic') payload = {'hello': 'world!', 'reply': True} # To send a message means sending a message explicitly to one receiver. bus.Send(payload=payload, queue='myQueue') # To defer a message means sending a message explicitly to one receiver with some delay before it is processed.
payload: dict = kwargs['payload'] print(payload) raise Exception("I'm just failing as I'm told ..") # Use pika connection params to set connection details credentials = pika.PlainCredentials('amqp', 'amqp') connParams = pika.ConnectionParameters(host='localhost', port=5672, virtual_host='/', credentials=credentials) # Create a PikaBusSetup instance with a listener queue and your own PikaErrorHandler definition. pikaErrorHandler = PikaErrorHandler(errorQueue='error', maxRetries=1) pikaBusSetup = PikaBusSetup(connParams, defaultListenerQueue='myFailingQueue', pikaErrorHandler=pikaErrorHandler) pikaBusSetup.AddMessageHandler(failingMessageHandlerMethod) # Start consuming messages from the queue. pikaBusSetup.StartConsumers() # Create a temporary bus to subscribe on topics and send, defer or publish messages. bus = pikaBusSetup.CreateBus() payload = {'hello': 'world!', 'reply': True} # To send a message means sending a message explicitly to one receiver. # In this case the message will keep failing and end up in an dead-letter queue called `error`. # Locate the failed message in the `error` queue at the RabbitMq admin portal on http://localhost:15672 (user=amqp, password=amqp) bus.Send(payload=payload, queue='myFailingQueue')
import pika import json from PikaBus.PikaBusSetup import PikaBusSetup from PikaBus.abstractions.AbstractPikaBus import AbstractPikaBus # Use pika connection params to set connection details. credentials = pika.PlainCredentials('amqp', 'amqp') connParams = pika.ConnectionParameters(host='localhost', port=5672, virtual_host='/', credentials=credentials) # Create a PikaBusSetup instance without a listener queue. pikaBusSetup = PikaBusSetup(connParams) # Run Init to create default listener queue, exchanges and subscriptions. pikaBusSetup.Init(listenerQueue='myQueue', subscriptions='myQueue') # Create a temporary bus transaction using the `with` statement # to transmit all outgoing messages at the end of the transaction. with pikaBusSetup.CreateBus() as bus: bus: AbstractPikaBus = bus payload = {'hello': 'world!', 'reply': False} bus.Send(payload=payload, queue='myQueue') bus.Publish(payload=payload, topic='myQueue') # Fetch and print all messages from the queue synchronously. with pikaBusSetup.CreateBus() as bus: bus: AbstractPikaBus = bus message = bus.channel.basic_get('myQueue', auto_ack=True) while message[0] is not None: