消息队列 消费端配置
pom
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.6.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>5.11.0</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.1.3.RELEASE</version> </dependency>
spring-message
<?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:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-lazy-init="true"> <!-- ActiveMQ 真正产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="activeMqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://100.100.100.100:61616" /> <property name="userName" value="admin"></property> <property name="password" value="admin"></property> </bean> <!-- 配置spring 管理 connectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="activeMqConnectionFactory" /> <property name="sessionCacheSize" value="100" /> </bean> <!--这个是主题(topic)目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="厦门易联众" /> </bean> <!-- 消息监听类,自己写 --> <bean id="messageListener" class="com.xxx.MessageListener" /> <!-- 消息监听器 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory"></property> <property name="destination" ref="topicDestination"></property> <property name="messageListener" ref="MessageListener"></property> </bean> </beans>
java类
package com.xxx;
import javax.jms.BytesMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import org.mortbay.log.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class PushMsgMessageListener implements MessageListener {
private Logger logger = LoggerFactory.getLogger(MessageListener.class);
@Autowired
private IParkPassRecordService parkPassRecordService;
@Override
public void onMessage(Message message) {
BytesMessage bytesMessage = (BytesMessage ) message;
logger.info("=======推送消息接收,从消息队列中读取================");
try {
byte [] data=new byte[(int) bytesMessage.getBodyLength()];
bytesMessage.readBytes(data);
} catch (Exception e) {
logger.error("处理失败,{}",e.getMessage());
}
}
}