Package: tamarco-amqp
tamarco-amqpAMQP resource for Tamarco microservice framework.SettingsThis resource depends on the following configuration schema:system:resources:amqp:host:127.0.0.1port:5672vhost:/user:microservicepassword:1234connection_timeout:10queues_prefix:"prefix"Inputs and outputsThe inputs and outputs need to be declared in the resource.Three different communication patterns can be used.A specific input is required for each message pattern, but the same output is valid for all message patterns. Each message pattern has a different method in the output.With the JsonCodec the input and the output are directly Python dictionaries.Publish-subscribe patternIn the pub-sub pattern a message reaches all the services subscribed to the queue. It can be used to notify events to anyone who needs it.InputTheAMQPSubscriptionInputcan be used as a decorator.classAmqpMicroservice():amqp=AMQPResource()@AMQPSubscriptionInput(resource=amqp,queue='cows',codec=JsonCodec)asyncdefconsume_messages(self,message):self.logger.info(f'Consumed message from cows subscription queue:{message}')Or as a async iterator:classAmqpMicroservice():cows_input=AMQPSubscriptionInput(queue='cows',codec=JsonCodec)amqp=AMQPResource(inputs=[cows_input])@taskasyncdefconsume_cows(self):asyncformessageinself.cows_input:self.logger.info(f'Consumed message from cows subscription queue:{message}')OutputUse thepublishmethod of the output.classAmqpMicroservice():cows_output=AMQPOutput(queue='cows',codec=JsonCodec)amqp=AMQPResource(outputs=[cows_output])@task_timer(interval=1000,autostart=True)asyncdefmetric_producer(self):awaitcows_output.publish({'my_json_message':'to_cow_queue'})Push-pull patternThe push-pull pattern is a work queue where each message is only pulled by one of the services subscribed to the queue. Commonly used to distribute the work in a pull of instances.InputAMQPPullInputis used to declare a input pull queue, and example used as decorator:classAmqpMicroservice():amqp=AMQPResource()@AMQPPullInput(resource=amqp,queue='cows',codec=JsonCodec)asyncdefconsume_messages(self,message):self.logger.info(f'Consumed message from cows pull queue:{message}')It can be used as an async iterator in the same way that the pub/sub pattern.OutputUse thepushmethod of theAMQPOutput. In the followingclassAmqpMicroservice():cows_output=AMQPOutput(queue='cows',codec=JsonCodec)amqp=AMQPResource(outputs=[cows_output])@task_timer(interval=1000,autostart=True)asyncdefmetric_producer(self):awaitcows_output.push({'my_json_message':'to_cow_queue'})Request-response patternIn the request-response pattern each request is handled by one of the instances subscribed to the queue and unlike the other patterns, each request has an answer.InputAMQPRequestInputis used to declare a input request response queue. An example used as decorator:classAmqpMicroservice():amqp=AMQPResource()@AMQPRequestInput(resource=amqp,queue='cows',codec=JsonCodec)asyncdefconsume_messages(self,message,response_handler):self.logger.info(f'Consumed message from cows queue:{message}')awaitresponse_handler.send_response({'cows':'response'})In this case the handler takes two input objects, and one of them send the response.OutputThe output is like the rest of them but it returns a response.classAmqpMicroservice():cows_output=AMQPOutput(queue='cows',codec=JsonCodec)amqp=AMQPResource()@task_timer(interval=1000,autostart=True)asyncdefrequest_response(self):message={'cow':'MOOOO'}response=awaitcows_output.request(message)self.logger.info(f'Response from cows queue:{response}')How to run the examplesTo run them you just need to launch the docker-compose, install the requirements and run it.pipinstall-rexamples/requirements.txtdocker-composeup-dpythonexamples/microservice_stream_input.py