Spring Boot教程(十四)Spring Boot整合ActiveQ实现消息收发和订阅

来源:素文宅博客 https://blog.yoodb.com/yoodb/article/detail/1426
javax.jms.ConnectionFactory接口提供了一个标准的用于创建一个javax.jms.Connection的方法,javax.jms.Connection用于和JMS代理(broker)交互。尽管为了使用JMS,Spring需要一个ConnectionFactory,但通常不需要直接使用它,而是依赖于上层消息抽象,Spring Boot会自动配置发送和接收消息需要的设施(infrastructure)。

如果发现ActiveMQ在classpath下可用,Spring Boot会配置一个ConnectionFactory。如果需要代理,将会开启一个内嵌的,已经自动配置好的代理(只要配置中没有指定代理URL)。

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

org.springframework.boot
spring-boot-starter-activemq

ActiveMQ配置是通过spring.activemq.*中的外部配置来控制的。在application.properties配置文件增加如下内容:
spring.activemq.broker-url=tcp://192.168.1.100:9876
spring.activemq.user=admin
spring.activemq.password=secret
默认情况下如果目标不存在,ActiveMQ将创建一个,所以目标是通过它们提供的名称解析出来的。

应用整合AxtiveQ支持案例

消息生产者,具体代码如下:
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来实现消息驱动POJO。

简单测试消息情况,具体代码如下:
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

推荐↓↓↓↓↓↓
这里写图片描述
更多推荐:微信公众号《优哉游哉》
关注微信公众号“优哉游哉”(w_z90110),回复关键字领取资料:如Hadoop,Dubbo,CAS源码等等,免费领取资料视频和项目等。
微信公众号涵盖:程序人生、搞笑视频、算法与数据结构、黑客技术与网络安全、前端开发、Java、Python、Redis缓存、spring源码、各大主流框架、Web开发、大数据技术、Storm、Hadoop、MapReduce、Spark、elasticsearch、单点登录统一认证、分布式框架、集群、安卓开发、iOS开发、C/C++、.NET、Linux、MySQL、Oracle、NoSQL非关系型数据库、运维等。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值