Use of message-oriented middleware (Spring Boot)

Source: Internet
Author: User
Tags rabbitmq

Use of message-oriented middleware (Spring Boot)

I. Preface

There are two important concepts in message-oriented middleware: Message proxy and destination. When a message sender sends a message, the message is taken over by the message proxy, which ensures that the message is transmitted to the specified destination.

Common Message proxies include JMS and AMQP specifications. Correspondingly, their common implementations are ActiveMQ and RabbitMQ.

2. Integrate ActiveMQ

2.1 Add dependency

<Dependency> <groupId> org. springframework. boot </groupId> <artifactId> spring-boot-starter-activemq </artifactId> </dependency> <! -- If you need to configure the connection pool, add the following dependency --> <dependency> <groupId> org. apache. activemq </groupId> <artifactId> activemq-pool </artifactId> </dependency>

2.2 add Configuration

# Activemq configure spring. activemq. broker-url = tcp: // 192.168.2.12: 61616spring. activemq. user = adminspring. activemq. password = adminspring. activemq. pool. enabled = falsespring. activemq. pool. max-connections = 50 # when using the publish/subscribe mode, set the following configuration to truespring. jms. pub-sub-domain = false

Spring. activemq. pool. enabled = false indicates that the connection pool is closed.

2.3 Encoding

Configuration class:

@Configurationpublic class JmsConfirguration {  public static final String QUEUE_NAME = "activemq_queue";    public static final String TOPIC_NAME = "activemq_topic";    @Bean  public Queue queue() {    return new ActiveMQQueue(QUEUE_NAME);  }    @Bean  public Topic topic() {    return new ActiveMQTopic(TOPIC_NAME);  }}

Creates queues and topics.

Message producer:

@Componentpublic class JmsSender {  @Autowired  private Queue queue;    @Autowired  private Topic topic;    @Autowired  private JmsMessagingTemplate jmsTemplate;    public void sendByQueue(String message) {    this.jmsTemplate.convertAndSend(queue, message);  }    public void sendByTopic(String message) {    this.jmsTemplate.convertAndSend(topic, message);  }}

Message consumer:

@ Componentpublic class JmsReceiver {@ JmsListener (destination = JmsConfirguration. QUEUE_NAME) public void receiveByQueue (String message) {System. out. println ("receive queue message:" + message) ;}@ JmsListener (destination = JmsConfirguration. TOPIC_NAME) public void receiveByTopic (String message) {System. out. println ("receive topic message:" + message );}}

The message consumer uses the @ JmsListener annotation to listen to messages.

2.4 Test

@RunWith(SpringRunner.class)@SpringBootTestpublic class JmsTest {  @Autowired  private JmsSender sender;  @Test  public void testSendByQueue() {    for (int i = 1; i < 6; i++) {      this.sender.sendByQueue("hello activemq queue " + i);    }  }    @Test  public void testSendByTopic() {    for (int i = 1; i < 6; i++) {      this.sender.sendByTopic("hello activemq topic " + i);    }  }}

Print result:

Receive queue message: hello activemq queue 1
Receive queue message: hello activemq queue 2
Receive queue message: hello activemq queue 3
Receive queue message: hello activemq queue 4
Receive queue message: hello activemq queue 5

Set spring. jms. pub-sub-domain = true when testing the publishing/subscription mode.

Receive topic message: hello activemq topic 1
Receive topic message: hello activemq topic 2
Receive topic message: hello activemq topic 3
Receive topic messages: hello activemq topic 4
Receive topic messages: hello activemq topic 5

3. Integrate RabbitMQ

3.1 add dependency

<dependency>  <groupId>org.springframework.boot</groupId>  <artifactId>spring-boot-starter-amqp</artifactId></dependency>

3.2 add Configuration

spring.rabbitmq.host=192.168.2.30spring.rabbitmq.port=5672spring.rabbitmq.username=lightspring.rabbitmq.password=lightspring.rabbitmq.virtual-host=/test

3.3 Encoding

Configuration class:

@ Configurationpublic class AmqpConfirguration {// ================ simple, work queue mode ====================== public static final String SIMPLE_QUEUE = "simple_queue "; @ Bean public Queue queue () {return new Queue (SIMPLE_QUEUE, true );} // ===================== publish/subscribe mode =============== public static final String PS_QUEUE_1 =" ps_queue_1 "; public static final String PS_QUEUE_2 = "ps_queue_2"; public static final String FANOUT_EXCHANGE = "fanout_exchange"; @ Bean public Queue psQueue1 () {return new Queue (PS_QUEUE_1, true );} @ Bean public Queue psQueue2 () {return new Queue (PS_QUEUE_2, true);} @ Bean public FanoutExchange fanoutExchange () {return new FanoutExchange (FANOUT_EXCHANGE );} @ Bean public Binding fanoutBinding1 () {return BindingBuilder. bind (psQueue1 ()). to (fanoutExchange ();} @ Bean public Binding fanoutBinding2 () {return BindingBuilder. bind (psQueue2 ()). to (fanoutExchange ());} // ====================== public static final String ROUTING_QUEUE_1 = "routing_queue_1"; public static final String partition = "ROUTING_QUEUE_2"; public static final String DIRECT_EXCHANGE = "direct_exchange"; @ Bean public Queue routingQueue1 () {return new Queue (ROUTING_QUEUE_1, true );} @ Bean public Queue routingQueue2 () {return new Queue (ROUTING_QUEUE_2, true);} @ Bean public DirectExchange directExchange () {return new DirectExchange (DIRECT_EXCHANGE );} @ Bean public Binding directBinding1 () {return BindingBuilder. bind (routingQueue1 ()). to (directExchange ()). with ("user") ;}@ Bean public Binding directBinding2 () {return BindingBuilder. bind (routingQueue2 ()). to (directExchange ()). with ("order ");} // ===================== topic mode =============== public static final String TOPIC_QUEUE_1 = "topic_queue_1"; public static final String TOPIC_QUEUE_2 = "topic_queue_2"; public static final String TOPIC_EXCHANGE = "topic_exchange"; @ Bean public Queue topicQueue1 () {return new Queue (TOPIC_QUEUE_1, true );} @ Bean public Queue topicQueue2 () {return new Queue (TOPIC_QUEUE_2, true);} @ Bean public TopicExchange topicExchange () {return new TopicExchange (TOPIC_EXCHANGE );} @ Bean public Binding topicBinding1 () {return BindingBuilder. bind (topicQueue1 ()). to (topicExchange ()). with ("user. add ") ;}@ Bean public Binding topicBinding2 () {return BindingBuilder. bind (topicQueue2 ()). to (topicExchange ()). with ("user. #");}}

RabbitMQ has multiple working modes, so there are many configurations. For more information, see RabbitMQ working mode introduction or Baidu related information.

Message producer:

@ Componentpublic class AmqpSender {@ Autowired private AmqpTemplate amqpTemplate;/*** send in simple mode ** @ param message */public void simpleSend (String message) {this. amqpTemplate. convertAndSend (AmqpConfirguration. SIMPLE_QUEUE, message);}/*** send in publish/subscribe mode ** @ param message */public void psSend (String message) {this. amqpTemplate. convertAndSend (AmqpConfirguration. FANOUT_EXCHANGE, "", message);}/*** send in routing mode ** @ param message */public void routingSend (String routingKey, String message) {this. amqpTemplate. convertAndSend (AmqpConfirguration. DIRECT_EXCHANGE, routingKey, message);}/*** topic mode to send ** @ param routingKey * @ param message */public void topicSend (String routingKey, String message) {this. amqpTemplate. convertAndSend (AmqpConfirguration. TOPIC_EXCHANGE, routingKey, message );}}

Message consumer:

@ Componentpublic class AmqpReceiver {/*** receive in simple mode ** @ param message */@ RabbitListener (queues = AmqpConfirguration. SIMPLE_QUEUE) public void simpleReceive (String message) {System. out. println ("Receive message:" + message);}/*** receive message in publish/subscribe mode ** @ param message */@ RabbitListener (queues = AmqpConfirguration. PS_QUEUE_1) public void psReceive1 (String message) {System. out. println (AmqpConfirguration. PS_QUEUE_1 + "Receive message:" + message) ;}@ RabbitListener (queues = AmqpConfirguration. PS_QUEUE_2) public void psReceive2 (String message) {System. out. println (AmqpConfirguration. PS_QUEUE_2 + "Receive message:" + message);}/*** receive message in routing mode ** @ param message */@ RabbitListener (queues = AmqpConfirguration. ROUTING_QUEUE_1) public void routingReceive1 (String message) {System. out. println (AmqpConfirguration. ROUTING_QUEUE_1 + "Receive message:" + message) ;}@ RabbitListener (queues = AmqpConfirguration. ROUTING_QUEUE_2) public void routingReceive2 (String message) {System. out. println (AmqpConfirguration. ROUTING_QUEUE_2 + "Receive message:" + message);}/*** receive message in topic mode ** @ param message */@ RabbitListener (queues = AmqpConfirguration. TOPIC_QUEUE_1) public void topicReceive1 (String message) {System. out. println (AmqpConfirguration. TOPIC_QUEUE_1 + "Receive message:" + message) ;}@ RabbitListener (queues = AmqpConfirguration. TOPIC_QUEUE_2) public void topicReceive2 (String message) {System. out. println (AmqpConfirguration. TOPIC_QUEUE_2 + "Receive message:" + message );}}

The message consumer uses the @ RabbitListener annotation to listen to messages.

3.4 Test

@RunWith(SpringRunner.class)@SpringBootTestpublic class AmqpTest {  @Autowired  private AmqpSender sender;  @Test  public void testSimpleSend() {    for (int i = 1; i < 6; i++) {      this.sender.simpleSend("test simpleSend " + i);    }  }  @Test  public void testPsSend() {    for (int i = 1; i < 6; i++) {      this.sender.psSend("test psSend " + i);    }  }    @Test  public void testRoutingSend() {    for (int i = 1; i < 6; i++) {      this.sender.routingSend("order", "test routingSend " + i);    }  }    @Test  public void testTopicSend() {    for (int i = 1; i < 6; i++) {      this.sender.topicSend("user.add", "test topicSend " + i);    }  }}

The test result is skipped...

Pitfall reminder 1: ACCESS_REFUSED-Login was refused using authentication mechanism PLAIN

Solution:

1) Make sure that the user name and password are correct. Note that the user name and password value contain spaces or tabs (the authentication fails due to a Tab character added to the password during the test ).

2) If the test account uses guest, modify the rabbitmq. conf file. Add "loopback_users = none" to the file.

Pitfall reminder 2: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it

Solution:

You can log on to the management interface of RabbitMQ and manually add the corresponding Queue in the Queue option.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.