def backend(): return LocalSQSBackend( wait_time_seconds=1, visibility_timeout=10, host=os.environ.get("SQSHOST") or "http://localhost", port=os.environ.get("SQSPORT") or 4566, sns_host=os.environ.get("SNSHOST") or "http://localhost", sns_port=os.environ.get("SNSPORT") or 4566, )
def backend(self): return LocalSQSBackend( wait_time_seconds=1, visibility_timeout=10, host=os.environ.get("SQSHOST"), port=os.environ.get("SQSPORT"), sns_host=os.environ.get("SNSHOST"), sns_port=os.environ.get("SNSPORT"), )
def test_async_consumer(request): # We'll use the ElasticMQ as backend since it works like a real SQS queue backend = LocalSQSBackend(host=os.environ.get("SQSHOST"), port=os.environ.get("SQSPORT")) queue_name = "testqueue" # Initialize the queue queue = MessagingBackendFactory(backend).init_queue(queue_name) # Delete the queue upon finishing the execution of the test def teardown(): backend.delete_queue(queue) request.addfinalizer(teardown) # Create a consumer that, upon receiving a message, will set # the variable "value set" to later assert that this value # has, indeed, been set by the consumer that is running on another thread state = State() def set_state(message: Dict) -> None: state.value_set = message["value"] consumer = Consumer(on_message=set_state) registry = SerializerRegistry(serializer_settings) handler = SimpleMessageDispatcher(consumer, registry, backend=backend) # Start the consumer loop thread to run the consumer loop in the background threading.Thread(target=lambda: handler.consume_loop(queue_name), daemon=True).start() # Publish a message and... publisher = QueuePublisher(registry, backend) publisher.publish(queue_name, {"value": 1}) # ...wait until the value is set probe = StateProbe(state) probe.wait() assert_that(state.value_set, is_(1))
import os from melange import MessagingBackendFactory from melange.backends import LocalSQSBackend if __name__ == "__main__": backend = LocalSQSBackend(host=os.environ.get("SQSHOST"), port=os.environ.get("SQSPORT")) factory = MessagingBackendFactory(backend=backend, ) factory.init_queue("payment-updates") factory.init_queue("order-updates") factory.init_queue("saga-updates") print("Queues created.")
from melange.serializers import PickleSerializer, SerializerRegistry class MyConsumer(SingleDispatchConsumer): @consumer def on_my_test_message_received(self, event: MyTestMessage) -> None: print(event.message) if __name__ == "__main__": serializer_settings = { "serializers": { "pickle": PickleSerializer }, "serializer_bindings": { DomainEvent: "pickle" }, } serializer_registry = SerializerRegistry(serializer_settings) backend = LocalSQSBackend(host="localhost", port=9324) consumer = MyConsumer() message_dispatcher = SimpleMessageDispatcher( consumer, serializer_registry, backend=backend, ) print("Consuming...") message_dispatcher.consume_loop("melangetutorial-queue")
from melange import MessagingBackendFactory from melange.backends import LocalSQSBackend backend = LocalSQSBackend() factory = MessagingBackendFactory(backend) factory.init_queue( "payment-updates.fifo", ["my-topic-1", "my-topic-2", "my-topic-3"], dead_letter_queue_name="payment-updates.fifo", )