以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ

前言

首先我们在java环境中使用了ActiveMQ,然后我们又在Spring中使用了ActiveMQ

本来这样已经可以了,但是最近SpringBoot也来了。所以在其中也需要使用试试。

可以提前透露的时候,在SpringBoot使用是最简单的一种

 

导入依赖

在原有SpringBoot项目的依赖加入下面

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

 

配置ActiveMQ连接信息

我是用的.yml文件进行配置,application.yml配置

spring:

activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: admin

 
 

队列模式消息发送配置

import

org.apache.activemq.command.ActiveMQQueue; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.jms.core.JmsMessagingTemplate; import
org.springframework.stereotype.Service; import
javax.jms.Destination; /**


 * 发送消息 */
@Service(

“producerService”
) public class
ProducerService { @Autowired private
JmsMessagingTemplate jmsMessagingTemplate; public void
sendMessage(String message){ Destination destination = new ActiveMQQueue(“queue-test”
); jmsMessagingTemplate.convertAndSend(destination, message); System.out.println(“发送消息:” +

 message); } }

单元测试

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.SpringJUnit4ClassRunner; /**


 * 发送消息单元测试 */
@RunWith(SpringJUnit4ClassRunner.class

) @SpringBootTest public class
ProducerServiceTest { @Autowired private
ProducerService producerService; @Test public void sendMessage() throws
Exception { producerService.sendMessage(“测试消息”

); } }

然后运行单元测试进入http://localhost:8161查看队列信息你就会发现,消息已经发送了。

是不是很惊讶,这样就已经全部配置好了,没有之前在spring中那些其他的配置,超清晰。

 

消息消费者配置

import

org.springframework.jms.annotation.JmsListener; import
org.springframework.stereotype.Service; /**


 * 消息的消费者 */
@Service(

“consumerService”
) public class
ConsumerService { @JmsListener(destination = “queue-test”
) public void
receiveQueue(String text) { System.out.println(“收到消息:” +

 text); } }

是不是更简单了

直接启动SpringBoot就能收到刚才发送的消息了

 

主题模式使用

SpringBoot默认使用的是队列模式,如果要使用主题模式需要修改application.yml

spring: jms:
pub-sub-domain: true

 

然后需要修改消息发送者代码中:Destination destination = new ActiveMQTopic(“topic-test”);

 

消息消费者代码中:@JmsListener(destination = “topic-test”)

 

可惜的是,这样又只能使用主题模式了,队列模式不能使用了那么怎么同时使用两种模式呢?

 
 

同时使用两种模式

首先新建JMS配置类

import

org.apache.activemq.command.ActiveMQQueue; import
org.apache.activemq.command.ActiveMQTopic; import
org.springframework.context.annotation.Bean; import
org.springframework.context.annotation.Configuration; import
org.springframework.jms.config.DefaultJmsListenerContainerFactory; import
org.springframework.jms.config.JmsListenerContainerFactory; import
javax.jms.ConnectionFactory; import
javax.jms.Queue; import
javax.jms.Topic; /**


 * JMS配置 */
@Configuration 

public class
JmsConfig { public final static String TOPIC = “topic-test”
; public final static String QUEUE = “queue-test”
; @Bean public
Queue queue() { return new
ActiveMQQueue(QUEUE); } @Bean public
Topic topic() { return new
ActiveMQTopic(TOPIC); } @Bean public JmsListenerContainerFactory<?>
jmsListenerContainerTopic(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory bean = new
DefaultJmsListenerContainerFactory(); bean.setPubSubDomain(true
); bean.setConnectionFactory(activeMQConnectionFactory); return
bean; } @Bean public JmsListenerContainerFactory<?>
jmsListenerContainerQueue(ConnectionFactory activeMQConnectionFactory) { DefaultJmsListenerContainerFactory bean = new
DefaultJmsListenerContainerFactory(); bean.setConnectionFactory(activeMQConnectionFactory); return

 bean; } }

消息的发送者

/**
 * 发送消息 */
@Service(

“producerService”
) public class
ProducerService { @Autowired private
JmsMessagingTemplate jmsMessagingTemplate; @Autowired private
Queue queue; @Autowired private
Topic topic; public void
sendMessage(String message) { jmsMessagingTemplate.convertAndSend(queue, message); jmsMessagingTemplate.convertAndSend(topic, message); System.out.println(“发送消息:” +

 message); } }

消息的消费者

import

org.springframework.jms.annotation.JmsListener; import
org.springframework.stereotype.Service; /**


 * 消息的消费者 */
@Service(

“consumerService”
) public class
ConsumerService { @JmsListener(destination = JmsConfig.TOPIC, containerFactory = “jmsListenerContainerTopic”
) public void
onTopicMessage(String msg) { System.out.println(“接收到topic消息:” +
msg); } @JmsListener(destination = JmsConfig.QUEUE, containerFactory = “jmsListenerContainerQueue”
) public void
onQueueMessage(String msg) { System.out.println(“接收到queue消息:” +

 msg); } }

然后进行测试就可以了

 

总结

总的来说在SpringBoot中使用ActiveMQ已经方便了很多,减少了很多的配置,看起来也更加的清晰了

ActiveMQ的所有配置属性说明

# ACTIVEMQ (ActiveMQProperties)

spring.activemq.broker-url= # URL of the ActiveMQ broker. Auto-generated by default. For instance `tcp://localhost:61616`

spring.activemq.in-memory=true # Specify if the default broker URL should be in memory. Ignored if an explicit broker has been specified.

spring.activemq.password= # Login password of the broker. spring.activemq.user= # Login user of the broker.

spring.activemq.packages.trust-all=false # Trust all packages. spring.activemq.packages.trusted= # Comma-separated list of specific packages to trust (when not trusting all packages).

spring.activemq.pool.configuration.*= # See PooledConnectionFactory.

spring.activemq.pool.enabled=false # Whether a PooledConnectionFactory should be created instead of a regular ConnectionFactory.

spring.activemq.pool.expiry-timeout=0 # Connection expiration timeout in milliseconds.

spring.activemq.pool.idle-timeout=30000 # Connection idle timeout in milliseconds. spring.activemq.pool.max-connections=1 # Maximum number of pooled connections.

 
 

最后PS:使用IDEA开发的同学如果看到下面的情况不要强迫症,因为我也很难受

《以ActiveMQ为例JAVA消息中间件学习【3】——SpringBoot中使用ActiveMQ》

就是红线,但是运行是正常的

 

参考博客:http://blog.csdn.net/qincidong/article/details/76114434

    原文作者:LinkinStar
    原文地址: https://www.cnblogs.com/linkstar/p/7570986.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞