直接上配置
消息生产者,分别配了队列模式,跟主题模式,都做了持久化配置
消息生产者配置:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--通用配置,配置连接工厂-->
<context:property-placeholder location="classpath:activemq.properties"/>
<!--真正的acitveMq对象-->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${brokerURL}"/>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<!--继承了SingleConnectionFactory的所有方法,添加缓存功能,缓存session,message等-->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
<property name="sessionCacheSize" value="100"></property>
</bean>
<!--##########配置生产者#######-->
<!--队列模板-->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="pubSubDomain" value="false"/>
<!-- 存活时间 -->
<!-- <property name="timeToLive" value="6000000"/> -->
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置explicitQosEnabled为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 配置是否持久化消息,1为非持久化,2为持久化 -->
<property name="deliveryMode" value="2"/>
</bean>
<!--主题模板-->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="pubSubDomain" value="true"/>
<!-- 存活时间 -->
<!-- <property name="timeToLive" value="6000000"/> -->
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置explicitQosEnabled为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 配置是否持久化消息,1为非持久化,2为持久化 -->
<property name="deliveryMode" value="2"/>
</bean>
<!--##########################-->
<!--目的地bean-->
<bean id="addSolrQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="add.solr.queue"/>
</bean>
<bean id="delSolrQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg value="del.solr.queue"/>
</bean>
<bean id="addPageTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="add.page.topic"/>
</bean>
<bean id="delPageTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="del.page.topic"/>
</bean>
</beans>
关键配置:
1.explicitQosEnabled是质量服务的开关,要持久化必须开启
<!-- deliveryMode, priority, timeToLive 的开关,要生效,必须配置explicitQosEnabled为true,默认false-->
<property name="explicitQosEnabled" value="true" />
<!-- 配置是否持久化消息,1为非持久化,2为持久化 -->
<property name="deliveryMode" value="2"/>
消费者配置(广播监听,如果要监听队列,加多一个监听队列的容器即可,跟广播监听的配置类似)
<?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:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd">
<!--通用配置,配置连接工厂-->
<context:property-placeholder location="classpath:application.properties"/>
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${brokerURL}"/>
<property name="userName" value="admin"></property>
<property name="password" value="admin"></property>
</bean>
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
<property name="clientId" value="client_001"></property>
<property name="sessionCacheSize" value="100"></property>
</bean>
<!--配置消费者(广播监听)-->
<!--destination-type指定为durableTopic(持久主题消息)-->
<jms:listener-container connection-factory="connectionFactory" destination-type="durableTopic">
<!--clientid+subcription的值为监听器注册到broker的名字-->
<jms:listener destination="add.page.topic" ref="addPageMessageListener" subscription="add.Page"/>
<jms:listener destination="del.page.topic" ref="delPageMessageListener" subscription="del.Page"/>
</jms:listener-container>
<!--自定义的监听类-->
<bean id="addPageMessageListener" class="com.pinyougou.item.listener.AddPageMessageListener"/>
<bean id="delPageMessageListener" class="com.pinyougou.item.listener.DelPageMessageListener"/>
</beans>
注意,持久化的消费方一定要配置clientId,消费方用这个clientId+subscription到broker注册服务名,让中间件区分开多个不同消费者以及消费者内的监听容器,这样就算生产方发送消息的时候,消费者不在线(也就是服务没启动),中间件也会把消息留给没启动的消费者,等消费者启动后能消费到.
测试注意事项
首先要启动一次消费者,让消费者在broker报名订阅,再关闭消费者.这样生产者发送消息并持久化后,就算broker宕机,或者消费者没启动,等broker或者消费者启动完毕,消费者能成功消费消息.