一直想写博客,技术用的都太多,但是都不久,总是忘记,老大让搞老项目,今天自己在公司搞了MQ,记录一下:
介绍就不需要了,网上一大堆,我喜欢直接上干货,直接撸,看起来也爽。
spring的原理就不用说了,首先spring里面配置能够扫描到这个xml
我的xml命名为applicationContext-activemq-container.xml 需要spring加载
xml内容如下
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd"
default-autowire="byName">
<bean id="myConnectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="sessionCacheSize" value="10" />
<property name="clientId" value="client_119" />
<property name="targetConnectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL"
value="你的MQ URL" />
<property name="nonBlockingRedelivery" value="true" />
<property name="sendTimeout" value="3000" />
<property name="alwaysSyncSend" value="true" />
<property name="redeliveryPolicy.useExponentialBackOff"
value="true" />
<property name="redeliveryPolicy.backOffMultiplier" value="2" />
<property name="redeliveryPolicy.initialRedeliveryDelay"
value="6000" />
<property name="redeliveryPolicy.maximumRedeliveries"
value="6" />
<property name="redeliveryPolicy.maximumRedeliveryDelay"
value="180000" />
bean>
property>
bean>
<bean id="myDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="这里直接写你主题的名字" />
bean>
<bean id="myTopicConsumer" class="这个里面写你类的全路径,如果有多个继续往下配置bean" />
<bean id="myTopicListener"
class="org.springframework.jms.listener.adapter.MessageListenerAdapter">
<constructor-arg ref="myTopicConsumer" />
<property name="defaultListenerMethod" value="这里写你listener里面需要监听的方法名字" />
<property name="messageConverter">
<null />
property>
bean>
<bean id="myListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="myConnectionFactory" />
<property name="pubSubDomain" value="true" />
<property name="subscriptionDurable" value="true" />
<property name="receiveTimeout" value="10000" />
<property name="clientId" value="client_119" />
<property name="durableSubscriptionName" value="client_119" />
<property name="destination" ref="myDestination" />
<property name="messageListener" ref="myTopicListener" />
bean>
beans>
我xml里面注释都写的很清楚,照着改就行了,看一眼也能明白撒。
配置已经有了 接下来就是写我们的listener了,代码如下,简单易懂:
package mtty.listener;
import java.util.ArrayList;
import java.util.List;
import javax.jms.JMSException;
import javax.jms.TextMessage;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.JmsException;
public class RouteLimitUpdateMqListener {
private final Log logger = LogFactory.getLog(RouteLimitUpdateMqListener.class);
/**
* 处理MQ消息
*
* @param message
* @throws JmsException
* @throws JMSException
* @date: 2017年12月27日
* @author: jiachen.zhang_c
*/
public void receive(TextMessage message) throws JmsException, JMSException {
logger.info("这里接收MQ消息,如果接收到,大吉大利,今晚吃鸡!哈哈");
logger.info("message消息为:"+message.getText());
}
怎么样,看完是不是很爽?直接粘贴就撸
*微信:zjc13213248598
Q Q:517709617
欢迎各位大神大牛路过指导,感激不尽!*