Spring Boot整合ActiveQ实现消息收发和订阅

javax.jms.ConnectionFactory接口提供了一个标准的用于建立一个javax.jms.Connection的方法,javax.jms.Connection用于和JMS代理(broker)交互。尽管为了使用JMSSpring须要一个ConnectionFactory,但一般不须要直接使用它,而是依赖于上层消息抽象,Spring Boot会自动配置发送和接收消息须要的设施(infrastructure)。java

若是发现ActiveMQclasspath下可用,Spring Boot会配置一个ConnectionFactory。若是须要代理,将会开启一个内嵌的,已经自动配置好的代理(只要配置中没有指定代理URL)。spring

引入spring-boot-starter-activemq,在pom.xml配置文件中增长配置以下(基于以前章节“Spring Boot 构建框架”中的pom.xml文件):apache

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

ActiveMQ配置是经过spring.activemq.*中的外部配置来控制的。在application.properties配置文件增长以下内容:app

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

默认状况下若是目标不存在,ActiveMQ将建立一个,因此目标是经过它们提供的名称解析出来的。框架

应用整合AxtiveQ支持案例tcp

消息生产者,具体代码以下:spring-boot

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);  
    }  
}

两个消息消费者,具体代码以下:测试

/***************** 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);  
    }  
}

注意的是消息消费者的类上必须加上@Component@Service注解,这样的话,消息消费者类就会被委派给Listener类,原理相似于使用SessionAwareMessageListener以及MessageListenerAdapter来实现消息驱动POJOurl

简单测试消息状况,具体代码以下:spa

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");  
        }  
    }  
  
}

运行结果以下:

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
相关文章
相关标签/搜索