![76dce432e6be5b4acae384da2eedb8b2.png](https://i-blog.csdnimg.cn/blog_migrate/dc977c69c062e0dd0ed9623fc43a8544.jpeg)
一,下载并安装ActiveMQ
首先我们到apache官网上下载activeMQ(http://activemq.apache.org/download.html),进行解压后运行其bin目录下面的activemq.bat文件启动activeMQ。
二,新建一个maven 项目并导入相关jar包
三,在maven中引入:
org.apache.activemq activemq-core 5.7.0org.apache.activemq activemq-pool 5.12.1
四,spring-active.xml配置
<?xml version="1.0" encoding="UTF-8"?>first-queue
五,springmvc.xml配置
<?xml version="1.0" encoding="UTF-8"?>
六, web.xml配置
略
七,消息发送接口代码
(1)消息发送接口
import javax.jms.Destination; public interface ProducerService { void sendMessage(Destination destination, final String msg,final int i); }
(2)消息发送接口实现类
import com.demo.test1.activemq.service.ProducerService;import org.springframework.jms.core.JmsTemplate;import org.springframework.jms.core.MessageCreator;import org.springframework.stereotype.Service; import javax.annotation.Resource;import javax.jms.*; @Servicepublic class ProducerServiceImpl implements ProducerService { @Resource(name="jmsTemplate") private JmsTemplate jmsTemplate; @Override public void sendMessage(Destination destination, final String msg, final int i) { System.out.println(Thread.currentThread().getName()+" 向队列"+destination.toString()+"发送消息--------->"+msg); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage(msg); textMessage.setIntProperty("con