在spring boot下使用ActiveMQ,需要一下几个条件
1.安装并启动了ActiveMQ,参考:http://www.cnblogs.com/sxdcgaq8080/p/7919489.html
2.搭建了spring boot项目,参考:http://www.cnblogs.com/sxdcgaq8080/p/7712874.html
======================================================================================================================
好了下面正式开始吧:
1.pom.xml文件添加maven依赖
<!--ActiveMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency>
2.application.properties配置文件配置
#activeMQ
#61616为消息代理接口 ,8161 为管理界面
spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
3.消息生产者Producer.java
package com.sxd.jms; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Service; import javax.jms.Destination; @Service("producer1") public class Producer1 { @Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装 private JmsMessagingTemplate jmsTemplate; // 发送消息,destination是发送到的队列,message是待发送的消息 public void sendMessage(Destination destination, final String message){ jmsTemplate.convertAndSend(destination, message); } }