Spring + activemq

依赖

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>  
            <version>4.0.6.RELEASE</version>  
        </dependency>

<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>


<?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"
xmlns:cache="http://www.springframework.org/schema/cache" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                     http://www.springframework.org/schema/context
                     http://www.springframework.org/schema/context/spring-context-4.0.xsd
                     http://www.springframework.org/schema/aop
                     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
                     http://www.springframework.org/schema/tx
                     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/cache 
http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/jdbc 
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd">

<context:annotation-config />

<!--  配置mq.url和其它参数-->

<context:property-placeholder location="classpath:config.properties" />


<!-- 
<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${mq.url}" />
<property name="useAsyncSend" value="true" />   
</bean>
-->
 
<bean id="connectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory"   >  
    <property name="connectionFactory">  
           <bean  class="org.apache.activemq.ActiveMQConnectionFactory">

      <property name="brokerURL" value="${mq.url}" />

                       <!-- 异步发送消息-->

      <property name="useAsyncSend" value="true" />   
     </bean>
    </property>  
    <!--   <property name="maxConnections" value="500"></property>  -->
</bean>  

<!-- 生产者 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
</bean>

<!-- TOPIC send jms模板 -->   
    <bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate">   

        <property name="connectionFactory" ref="connectionFactory"></property> 

<!-- 定义了defaultDestination  在代码中用 topicJmsTemplate 发送消息时没有指定topic时 消息就会发送到  topicName中--> 

        <property name="defaultDestination" ref="myTopic" />   
        <!-- 开启订阅模式 -->   
        <property name="pubSubDomain" value="true"/>   
    </bean>   

<!-- Topic -->
<bean id="myTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg index="0" value="topicName" />
</bean>

<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">  
        <constructor-arg index="0" value="queueName" />  
    </bean>
    
    <!-- 消息接收方 
    <bean id="productsMessageListener" class=" com.test.service.MessageReceiver">   
    </bean>   
       -->   
    <!-- 主题消息监听容器 -->   
    <bean id="listenerContainer"     
        class="org.springframework.jms.listener.DefaultMessageListenerContainer"  >     
        <property name="connectionFactory" ref="connectionFactory" />     
        <property name="pubSubDomain" value="true"/><!-- default is false  topic 方式下必须开启-->   
        <property name="destination" ref="myTopic" />  <!-- listen topic: myTopic-->   
        <property name="subscriptionDurable" value="true"/>   
        <property name="concurrentConsumers"  value="5"></property>
        <property name="messageListener" ref="productsMessageListener" />     
    </bean>   
    
    <!-- 消费者 -->
    <bean id="listenerContainer"   class="org.springframework.jms.listener.DefaultMessageListenerContainer"> 
    <!--并发  -->
        <property name="concurrentConsumers" value="10" />
        <property name="connectionFactory" ref="connectionFactory" />   
        <property name="destinationName" value="queueName" />
        <property name="messageListener" ref="messageReceiver" />
    </bean>
    
    <bean id="messageReceiver" class=com.test.TestMessageListener">   
        <property name="jmsTemplate" ref="jmsTemplate"></property>   
    </bean>

</beans>

public class TestSendMessageListener {
@Autowired private JmsTemplate jmsTemplate;


public void sendMessage(Destination destination, final String message) {  
        
        System.out.println("---------------生产者发了一个消息:" + message);  

      //1.

        //Destination destination

       //jmsTemplate .convertAndSend(  destination, msgPojo); 

   //2.
        //直接写队列名字
        jmsTemplate.send("test_queue", new MessageCreator() {  
            public Message createMessage(Session session) throws JMSException {  
                return session.createTextMessage(message);  
            }
        });  
    }   


}


public class TestMessageListener implements MessageListener{


@Override
public void onMessage(Message message) {
//这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换  
        TextMessage textMsg = (TextMessage) message;  
        System.out.println("接收到一个纯文本消息。");  
        try {
            System.out.println("消息内容是:" + textMsg.getText());  
        } catch (JMSException e) {  
            e.printStackTrace();  
        }
}


}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值