主要步骤:
一:在电脑安装win版本的activemq且启动
二:在pom.xml 配置的依赖activemq和jms
三:在web项目添加依赖的包
四:新建配置文件spring-mq.xml,在该配置文件中创建activemq的连接和jms的对象用于接收和发送信息
五:在web.xml中整合spring-mq.xml,将该配置文件中的对象交给springmvc管理
六.新建发送消息的类MqProducer和接收消息的类MyListener
七.新建测试类TestMq,运行tomcat,进行测试
一:在电脑安装win版本的activemq且启动
通过网址:http://activemq.apache.org/download-archives.html选择要下载的版本,此次演示的是下载了5.14.0的windows版本,window版本的进入到安装目录下的apache-activemq-5.14.0-bin\apache-activemq-5.14.0\bin\win64的运行activemq
运行mq之后,可以看到运行界面
在浏览器输入:http://127.0.0.1:8161/admin/index.jsp 默认用户名:admin和密码:admin,登录activemq的后台证明已经成功安装activemq
二:在pom.xml 配置的依赖activemq和jms
<!--activemq-->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-spring</artifactId>
<version>5.14.5</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
三:在web项目添加依赖的包
四:新建配置文件spring-mq.xml,在该配置文件中创建activemq的连接和jms的对象用于接收和发送信息
在该文件中spring-mq.xml添加以下内容
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.0.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.14.5.xsd
">
<context:annotation-config/>
<!-- 第一步:读取配置文件 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" >
<array>
<value>classpath:conf/mq.properties</value>
</array>
</property>
</bean>
<!-- 第二步:连接 activemq-->
<amq:connectionFactory id="amqConnectionFactory" brokerURL="${activemq_url}" userName="${activemq_username}" password="${activemq_password}"/>
<!-- 采用连接池的方式连接PooledConnectionFactoryBean -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 配置连接 -->
<property name="targetConnectionFactory" ref="amqConnectionFactory"/>
<!-- 会话的最大连接数 -->
<property name="sessionCacheSize" value="100"/>
</bean>
<!-- 配置JMS模板(topic),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<!-- true为topic,false为queue -->
<property name="pubSubDomain" value="true"/>
</bean>
<bean id="mqListen" class="com.ivy.mq.MqListen"/>
<!-- 监听方式,这种方式更实用,可以一直监听消息 -->
<jms:listener-container destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="hello" ref="mqListen"/>
</jms:listener-container>
</beans>
五:在web.xml中整合spring-mq.xml,将该配置文件中的对象交给springmvc管理
六.新建发送消息的类MqProducer和接收消息的类MyListener
在mq的包下面新建类MqProducer用于发送消息
package com.ivy.mq;
import org.springframework.jms.JmsException;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;
import java.util.HashMap;
import java.util.Map;
/**
* Created by Administrator on 2018/5/19.
*/
@Service
public class MqProducer {
@Resource(name="jmsTemplate")
private JmsTemplate jmsTemplate;
public void totouchmq1() {
jmsTemplate.send("hello",new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
TextMessage msg = session.createTextMessage();
Map<String,String> map=new HashMap<String, String>();
map.put("name","你好");
msg.setText(map.toString());
return msg;
}
});
}
}
在mq的包下面新建类MqProducer用于接收消息
package com.ivy.mq;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
/**
* Created by Administrator on 2018/5/19.
*/
public class MqListen implements MessageListener {
public void onMessage(Message message) {
System.out.println("正在监听======");
try {
TextMessage tm = (TextMessage)(message);
System.out.println("监听到的信息是:"+tm.getText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
七.新建测试类TestMq,运行tomcat,进行测试
package com.ivy.mq;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* Created by Administrator on 2018/5/19.
*/
@Controller //声明为控制器
@RequestMapping(path = "/testmq") //请求映射
public class TestMq {
@Resource
MqProducer mqProducer;
@RequestMapping(path = "/a") //请求映射
public void handle2(){
mqProducer.totouchmq1();
}
}
在浏览器输入:http://localhost:8080/项目名/testmq/a
在console控制台可以看到