-
前言
本篇内容主要记录spring集成消息中间件MQ的,分队列模式queue和主题模式topic两种模式,我使用的是activeMq。网上现在主流的是rabbitMq,功能更加强大,但rabbitMq用的是Erlang语言写的,需要安装Erlang环境。如果是要用到大数据相关的,可以使用kafka,我这为了简便就用了activeMq。activeMq的apache官网下载地址 -
启动activeMq
下载下activeMq之后解压缩,在bin目录下有32和64的,根据电脑位数运行,我的是64位的,运行exe文件 -
pom.xml文件引入依赖
<!--activeMq start--> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.9.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.0.0.RELEASE</version> </dependency> <!--activeMq end-->
-
创建mq的xml文件application-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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <!--<context:component-scan base-package="com.winner.spring"/>--> <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="${activeMq.brokerUrl}"/> <property name="userName" value="${activeMq.username}"/> <property name="password" value="${activeMq.password}"/> </bean> </property> <!--最大连接数--> <property name="maxConnections" value="100"></property> </bean> <!--使用缓存可以提升效率--> <bean id="cachingConnectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <property name="targetConnectionFactory" ref="jmsFactory"/> <property name="sessionCacheSize" value="1"/> </bean> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="cachingConnectionFactory"/> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> <!--测试Queue,队列的名字是spring-queue--> <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue"> <!--<constructor-arg index="0" value="spring-queue"/>--> <constructor-arg name="name" value="spring-queue"/> </bean> <!--测试Topic--> <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg index="0" value="spring-topic"/> </bean> <!--DefaultMessageListenerContainer允许异步接收和缓存消息和session--> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="cachingConnectionFactory"/> <property name="destination" ref="destinationQueue"/> <property name="messageListener" ref="messageListener"/> </bean> <!--消息监听器--> <bean id="messageListener" class="com.djkj.demo.common.MyMessageListener"> </bean> </beans>
mq的连接地址根据实际情况设定,用户名密码根据实际情况填写,activeMq默认的是admin/admin,假如activemq配置了连接认证,则需要用配置的认证用户登录,具体用法是在activeMq的conf文件夹下activemq.xml里添加插件
<plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username="username" password="password" groups="admin,users"/> </users> </simpleAuthenticationPlugin> </plugins>
-
service发送消息
package com.djkj.demo.service.serviceImpl; import com.djkj.demo.service.AmqSenderService; import org.apache.log4j.Logger; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; @Service("AmqSenderService") public class AmqSenderServiceImpl implements AmqSenderService { private static final Logger logger = Logger.getLogger(AmqSenderServiceImpl.class); @Resource private JmsTemplate jmsTemplate; @Resource(name = "destinationQueue") private Destination destination; @Override public void sendMsg(String msg) { logger.info("----发送队列信息开始----"); try { jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return session.createTextMessage(msg); } }); }catch (Exception e){ e.printStackTrace(); logger.error("发送队列失败!"); } } }
controller调用service方法发送消息
-
接收消息
我是把消息生产者和消费者放在一起的,实际开发中消费者和生产者绝大多数情况是分开在不同系统的。消费者主要通过监听来实现接收消息
上面的mq的xml文件最底下配置了一个我的消息监听器MyMessageListener,代码如下
package com.djkj.demo.common; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { if (message instanceof TextMessage) { try { TextMessage txtMsg = (TextMessage) message; String msg = txtMsg.getText(); WebSocketUtil.sendMessageAll(msg); } catch (JMSException e) { throw new RuntimeException(e); } } else { throw new IllegalArgumentException("Message must be of type TextMessage"); } } }
接收到消息后通过websocket发送到前台页面实现实时的效果的,大家根据自己的需求来写。以后再写关于websocket的集成。