以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開發的同學如果看到下面的情況不要強迫症,因為我也很難受

image

就是紅線,但是運行是正常的

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM