Spring Boot series (12) Spring Boot integrates ActiveQ to realize message sending and receiving and subscription

The javax.jms.ConnectionFactory interface provides a standard method for creating a javax.jms.Connection, which is used to interact with a JMS broker. Although Spring needs a ConnectionFactory in order to use JMS, it is usually not necessary to use it directly, but relies on the upper-level message abstraction, and Spring Boot automatically configures the infrastructure needed to send and receive messages.

 

Spring Boot configures a ConnectionFactory if ActiveMQ is found to be available on the classpath. If a proxy is required, an embedded, auto-configured proxy will be enabled (as long as the proxy URL is not specified in the configuration).

 

Introduce spring-boot-starter-activemq and add the following configuration to the pom.xml configuration file (based on the pom.xml file in the previous chapter "Spring Boot Construction Framework"):

 

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

 

ActiveMQ configuration is controlled via external configuration in spring.activemq.*. Add the following to the application.properties configuration file:

 

spring.activemq.broker-url=tcp://192.168.1.100:9876
spring.activemq.user=admin
spring.activemq.password=secret

 

By default ActiveMQ will create a target if it doesn't exist, so the target is resolved by the name they provide.

 

Application Integration AxtiveQ Support Case

 

Message producer, the specific code is as follows:

 

import javax.jms.Destination;  
  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.jms.core.JmsMessagingTemplate;  
import org.springframework.stereotype.Service;  
  
@Service("producer")  
public class Producer {  
       // 该方式可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装 
    @Autowired
    private JmsMessagingTemplate jmsTemplate; 
     
    // 发送消息,destination是发送到的队列,message是待发送的消息  
    public void sendMessage(Destination destination, final String message){  
        jmsTemplate.convertAndSend(destination, message);  
    }  
}

 

Two message consumers, the specific code is as follows:

 

/***************** Consumer1 ******************/

import org.springframework.jms.annotation.JmsListener;  
import org.springframework.stereotype.Component;  
  
@Component  
public class Consumer {  
        // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息  
    @JmsListener(destination = "mytest.queue")  
    public void receiveQueue(String text) {  
        System.out.println("Consumer收到的报文为:"+text);  
    }  
} 

/***************** Consumer2 ******************/

import org.springframework.jms.annotation.JmsListener;  
import org.springframework.stereotype.Component;  
  
@Component  
public class Consumer2 {  
        // 使用JmsListener配置消费者监听的队列,其中text是接收到的消息  
    @JmsListener(destination = "mytest.queue")  
    public void receiveQueue(String text) {  
        System.out.println("Consumer2收到的报文为:"+text);  
    }  
}

 

Note that the message consumer class must be annotated with @Component or @Service. In this case, the message consumer class will be delegated to the Listener class. The principle is similar to using SessionAwareMessageListener and MessageListenerAdapter to implement message-driven POJOs.

 

Simply test the message situation, the specific code is as follows:

 

import javax.jms.Destination;  
  
import org.apache.activemq.command.ActiveMQQueue;  
import org.junit.Test;  
import org.junit.runner.RunWith;  
import org.springframework.beans.factory.annotation.Autowired;  
import org.springframework.boot.test.context.SpringBootTest;  
import org.springframework.test.context.junit4.SpringRunner;  
  
@RunWith(SpringRunner.class)  
@SpringBootTest  
public class SpringbootJmsApplicationTests {  
      
    @Autowired  
    private Producer producer;  
      
    @Test  
    public void contextLoads() throws InterruptedException {  
        Destination destination = new ActiveMQQueue("mytest.queue");  
          
        for(int i=0; i<100; i++){  
            producer.sendMessage(destination, "my site is blog.yoodb.com");  
        }  
    }  
  
}

 

The results are as follows:

 

Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com
Consumer收到的报文为:my site is blog.yoodb.com
Consumer2收到的报文为:my site is blog.yoodb.com

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324441512&siteId=291194637