RabbitMQ getting started tutorial For Java [9]-integration with Spring

Source: Internet
Author: User
Tags sendmsg rabbitmq

RabbitMQ getting started tutorial For Java [9]-integration with Spring

RabbitMQ getting started tutorial For Java [9]-integration with Spring

Introduction:

RabbitMQ has two projects integrated with Spring. One project is the message producer, responsible for sending messages, and the other is the message consumer, responsible for listening to messages. The general flowchart is as follows:

Project Environment:

64-bit Windows 7

Eclipse Kepler SR2

JDK 1.7

Tomcat 7

RabbitMQ 3.6.0

Project Source Code address:

Production: https://github.com/chwshuang/spring-rabbitmq-producer

Consumer: https://github.com/chwshuang/spring-rabbitmq-customer

 

Producer:

Maven is used for projects integrated with Spring, and only one dependency configuration is required:

 

                 
 
  
   org.springframework.amqp
  spring-rabbit
  
   1.3.5.RELEASE
  
 

The configuration of the message producer in Spring is also relatively simple. You only need a connection factory and a connection template class.
        <!--{cke_protected}{C}%3C!%2D%2D%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3DRabbitMQ%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%20%2D%2D%3E--><rabbit:connection-factory id="connectionFactory" host="localhost" publisher-confirms="true" virtual-host="test" username="test" password="1234"><rabbit:template id="rabbitTemplate" connection-factory="connectionFactory"></rabbit:template></rabbit:connection-factory>

When a producer is used in Spring, you only need to define a service interface implementation class.
@ Service ("rabbitService") public class RabbitServiceImpl {private static Logger log = LoggerFactory. getLogger (RabbitServiceImpl. class); @ Autowiredprivate RabbitTemplate rabbitTemplate;/*** send message * @ param msg message content * @ param routingKey route keyword * void */public void setMessage (String msg, String routingKey) {rabbitTemplate. convertAndSend (routingKey, msg); log.info ("rabbitmq -- message sending completed: routingKey [{}]-msg [{}]", routingKey, msg );}}

The producer also needs to create a test page to send messages to the control layer of the producer through Ajax technology. The control layer calls the message service layer to send messages.

<%@ page contentType="text/html;charset=UTF-8" language="java"%>
<Script src = "http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"> </script> <Script type = "text/javascript"> function sendMsg () {var t = $ ("# type "). val (); // alert (t); $. post ('/producer/rabbit/setmessage', {msg: 'Hello world' + t + 'rabbit! ', Type: t}, function (result) {if (result. success) {// alert ("sent successfully") ;}else {alert ("failed to send") ;}, 'json') ;}</script> send a message RedBlue Send

 

 

Consumer:

The message consumer of RabbitMQ needs to listen to messages and process received messages. Therefore, a listener must be configured to declare a queue to be listened to and a message processor. The consumer and spring integration have less intrusion.

The message processor can be a common Bean object managed through spring. A method with the same name defined in xml is required. 
Public class RedQueueListener {private static Logger log = LoggerFactory. getLogger (RedQueueListener. class);/*** process the message * @ param message * void */public void onMessage (String message) {log.info ("RedQueueListener -- receved:" + message );}}
RabbitMQ creates a message Environment

After the project development is complete, we need to create vhost, user, and queues in RabbitMQ. In this way, the consumer will not report errors when logging on to and declaring virtual machines and queues at startup.

1. Create the test Virtual Machine and click the Name tab to enter the Virtual Machine and add permissions.

2. Go to the Admin menu and click the Users tab to create a user. Then, click the user name to go to the details page to set permissions.

3. Go to the [Queues] menu and create a queue.

Run the test:

Add the producer and consumer project to Tomcat, start the project, and enter [http: // localhost: 8080/producer/test. jsp], enter the test page, send red and blue messages to the queue respectively, the consumer side will display the receipt log

Producer logs

18:09:26 722 [INFO] c. a. p. c. RabbitController-rabbitmq -- receive the message to be sent: type [blue]-msg [hello world blue rabbit!] 18:09:26 723 [INFO] c. a. p.s. RabbitServiceImpl-rabbitmq -- message sending completed: routingKey [blue]-msg [hello world blue rabbit!] 18:09:28 715 [INFO] c. a. p. c. RabbitController-rabbitmq -- receive the message to be sent: type [red]-msg [hello world red rabbit!] 18:09:28 716 [INFO] c. a. p.s. RabbitServiceImpl-rabbitmq -- message sending completed: routingKey [red]-msg [hello world red rabbit!]

Consumer logs
2016-01-25 18:09:26 727 [INFO] c.a.c.BlueQueueListener - BlueQueueListener Receved:hello world blue rabbit!2016-01-25 18:09:28 719 [INFO] c.a.c.RedQueueListener - RedQueueListener Receved:hello world red rabbit!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.