首先咱們在java環境中使用了ActiveMQ,而後咱們又在Spring中使用了ActiveMQjava
原本這樣已經能夠了,可是最近SpringBoot也來了。因此在其中也須要使用試試。spring
能夠提早透露的時候,在SpringBoot使用是最簡單的一種apache
在原有SpringBoot項目的依賴加入下面app
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>dom
我是用的.yml文件進行配置,application.yml配置tcp
spring:
activemq:
broker-url: tcp://127.0.0.1:61616
user: admin
password: adminspring-boot
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中那些其餘的配置,超清晰。url
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配置 */ @Configurationpublic 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開發的同窗若是看到下面的狀況不要強迫症,由於我也很難受
就是紅線,可是運行是正常的
參考博客:http://blog.csdn.net/qincidong/article/details/76114434