ActiveMq使用(三) Topic消息持久化

1.生产者


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
    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-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd ">

    
    <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.128:61616" />
        <property name="userName" value="admin" />
        <property name="password" value="admin" />
    bean>

    
    <bean id="mqConnectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        
        <property name="targetConnectionFactory" ref="amqConnectionFactory" />
        
        <property name="sessionCacheSize" value="100" />
    bean>

    
    <bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="mqConnectionFactory" />
        <property name="pubSubDomain" value="false" />
    bean>

    
    <bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
        <constructor-arg ref="mqConnectionFactory" />
        
        <property name="pubSubDomain" value="true" />
        
        <property name="deliveryMode" value="2">property>
        <property name="explicitQosEnabled" value="true"/>
    bean>
beans>

注:
1. 配置开启发布订阅模式
2. 将topic配置持久化 deliveryMode=2 持久化 deliveryMode=不持久化
3. 将explicitQosEnabled配置成true,否则不生效

2.消费端


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:task="http://www.springframework.org/schema/task"
    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-4.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
        http://www.springframework.org/schema/data/jpa 
        http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
        http://www.springframework.org/schema/jms
        http://www.springframework.org/schema/jms/spring-jms.xsd
        http://activemq.apache.org/schema/core
        http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd ">

    
    <bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://192.168.25.128:61616" />
        <property name="userName" value="admin" />
        <property name="password" value="admin" />
    bean>

    
    <bean id="mqConnectionFactory"
        class="org.springframework.jms.connection.CachingConnectionFactory">
        
        <property name="targetConnectionFactory" ref="amqConnectionFactory" />
        
        <property name="sessionCacheSize" value="100" />
        <property name="clientId" value="client_100">property>
    bean>

    
    <jms:listener-container destination-type="durableTopic"
        container-type="default" connection-factory="mqConnectionFactory"
        acknowledge="auto" >
        
        <jms:listener destination="pinyougou.genHtml" ref="genHtmlListener" />
        <jms:listener destination="pinyougou.delHtml" ref="delHtmlListener" />
    jms:listener-container>

beans>

注:
1. 连接工厂配置clientId
2. 将topic的destination-type配置成持久化Topic

你可能感兴趣的:(java)