Simple Example of AMQP Message Queue RabbitMQ, amqp queue rabbitmq
The previous article explains how to quickly build an ActiveMQ sample program. ActiveMQ is the implementation of JMS, in this article, let's look at another example of Message Queue AMQP to implement RabbitMQ. Before proceeding to the specific explanation, we will first use a diagram to give an overview:
1. Add Maven dependency
<!-- rabbitmq begin --> <dependency> <groupId>org.springframework.amqp</groupId> <artifactId>spring-rabbit</artifactId> <version>1.7.2.RELEASE</version> </dependency> <!-- rabbitmq end -->
2.Add rabbitmq configurations to the Spring configuration file.
1) message sending
<! -- Configure connection-factory and specify the parameters for connecting to the rabbit server --> <rabbit: connection-factory id = "connectionFactory" host = "127.0.0.1" port = "5672" username = "guest" password = "guest"/> <! -- Define rabbit template for receiving and sending data --> <rabbit: template id = "amqpTemplate" connection-factory = "connectionFactory" exchange = "bounter. fanout "routing-key =" bounter. key "/> <! -- By specifying the following admin information, exchange and queue in the current producer will be automatically generated on the rabbitmq server --> <rabbit: admin connection-factory = "connectionFactory"/> <! -- Define queue --> <rabbit: queue name = "bounter. queue"/> <! -- Define fanout exchange and publish and subscribe mode --> <rabbit: fanout-exchange name = "bounter. fanout "> <rabbit: bindings> <rabbit: binding queue =" bounter. queue "/> </rabbit: bindings> </rabbit: fanout-exchange>
2) receive messages
<! -- Define the listener container for receiving messages --> <rabbit: listener-container connection-factory = "connectionFactory"> <rabbit: listener ref = "amqpFanoutListener" method = "onFanout" queue-names = "bounter. queue "/> <! -- <Rabbit: listener ref = "amqpDirectListener" method = "onDirect" queue-names = "bounter. queue"/> --> </rabbit: listener-container>
3. Define message senders and message listenersFor more information, see the sender and Cycler packages in the project source code.
4. Test message sending and receivingFor details, refer to the unit test class in the project source code.
RabbitMQTestIs it easy! Try it on your own! If you have any suggestions, please leave a message! Github Source Code address: https://github.com/13babybear/mq-client