The use of activeMQ of springboot

1. Introduction

Springboot has unprecedentedly powerful integration capabilities, and naturally provides support for messages. It integrates with activeMQ. We can simply import a dependency and use it through annotations.

 

2. Dependence

<!--activemq-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>${activemq-pool.version}</version>
</dependency>

As above, we introduced activemq dependencies, as well as dependencies for pooling

 

3. Configure the external server

The server side of the message is generally separated from the program, so although springboot has embedded activeMQ, we rely on the selection of external configuration and add in the application.properties file:

# activeMQ
spring.activemq.broker-url=tcp://IP:61616
spring.activemq.user=用户名
spring.activemq.password = Password
# pool
spring.activemq.pool.enabled=true
spring.activemq.pool.max-connections=50

 

Fourth, point-to-point

ActiveMQ auto-configured by springboot is point-to-point by default, and we can easily define producers and consumers:

@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
/**
* Producer
*/
public void sendMessage() {
    jmsMessagingTemplate.convertAndSend(new ActiveMQQueue("queue1"), "message content");
}
/**
* Consumer
*/
@JmsListener(destination = "queue1")
public void consumeMessage(String message){
    System.out.println("consumer1: " + message);
}

As above, we use the jmsMessageTemplate template to send point-to-point messages, and the methods annotated with @JmsListener are used to consume messages.

 

Five, publish and subscribe

Springboot is point-to-point by default, so if we want to use publish and subscribe, we need to customize one: Bean of JmsListenerContainerFactory

Such as:

   @Bean
    public JmsListenerContainerFactory myJmsListenerContainerFactory(ConnectionFactory connectionFactory){
        SimpleJmsListenerContainerFactory factory = new SimpleJmsListenerContainerFactory();
         // Enable publish-subscribe mode 
        factory.setPubSubDomain( true );
        factory.setConnectionFactory(connectionFactory);
        return factory;
    }

Then the code used will be changed to the following:

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    public void sendMessage() {
        jmsMessagingTemplate.convertAndSend(new ActiveMQTopic("topic"), "message content");
    }

    @JmsListener(destination = "topic", containerFactory = "myJmsListenerContainerFactory")
    public void consumeMessage(String message){
        System.out.println("consumer1: " + message);
    }

    @JmsListener(destination = "topic", containerFactory = "myJmsListenerContainerFactory")
    public void consumeMessage2(String message){
        System.out.println("consumer2: " + message);
    }

Notice:

1) We used an ActiveMQTopic instance instead of an ActiveMQQueue instance when we created the Destination, which represents a topic message;

2) In the @JmsListener annotation, we added the bean instance of JmsListenerContainerFactory to the annotation of containerFactory;

3) And, we define multiple JmsListeners to consume messages.

 

Guess you like

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