Spring整合ActiveMQ--消息接收端(一)

先安装apache-activemq消息中间件,消息发送给中间件->activemq->从中间件获取消息。

activemq.xml

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/data/jpahttp://www.springframework.org/schema/data/jpa/spring-jpa.xsd" >
       
       <context:property-placeholder location=  "classpath*:env.properties,classpath*:test.properties"  ignore-unresolvable="true"/>
  
      
       <!-- 指定消息中介地址-->
       <bean id="connectionFactory"  class="org.apache.activemq.ActiveMQConnectionFactory">
             <property name="brokerURL" value="${MQ_URL}"/>
             <property name="useAsyncSend" value="true" />
       </bean>

   
     <!-- 配置消息目标 -->
       <bean id="myQueueOne" class="org.apache.activemq.command.ActiveMQQueue">
             <constructor-arg index="0" value="EVENT_CREATE"/>
       </bean>

     <!-- 消息模板 -->
    <bean id="jmsTemplateOne"  class="org.springframework.jms.core.JmsTemplate">
             <property name="connectionFactory" ref="connectionFactory"/>
             <!-- 设置默认的消息目的地-->
             <property name="defaultDestination" ref="myQueueOne"/>
             <property name="deliveryMode" value="1"></property>
             <!-- 消息不持久化 -->
             <property name="explicitQosEnabled" value="true"></property>
             <!-- 由于receiver方法是同步的,所以我们在这里对接收设置超时时间-->
             <!--  <property name="receiveTimeout" value="60000"/>   -->
       </bean>

    <!-- 显示注入消息监听容器(Queue) -->
    <bean id="containerOne" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
             <property name="connectionFactory" ref="connectionFactory"/>
             <property name="destination" ref="myQueueOne"/>
             <property name="messageListener" ref="jmsListener"/>
       </bean>

    <!-- 消息监听、执行器 --> 
       <bean id="jmsListener" class="com.xxx.controller.MsgListener" />
       
</beans>

test.properties文件中包含

MQ_URL=tcp://192.168.2.15:61616

mq消息接收

package com.xxx.controller;

import java.util.Observable;
import java.util.Set;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.log4j.Logger;
import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.springframework.context.ApplicationContext;

import com.zchz.testbase.Consts;

import net.sf.json.JSONObject;

public class MsgListener  implements  MessageListener{
    public static MqObservable instance;

    //类的构造函数
    public static MsgListener getInstance(){
        if(instance == null){
              synchronized(MqObservable.class){
                   if (instance == null){
                       ApplicationContext factory=new ClassPathXmlApplicationContext("activemq.xml");
     
                       instance = (MqObservable)factory.getBean("jmsListener");
                   }
               }
        }
        return instance;
    }
    

    private MsgListener() {
        
    }

    @Override
    public void onMessage(Message message) {
        
        TextMessage msg = (TextMessage) message;//获取的mq消息
        //JSONObject jsonMsg =  JSONObject.fromObject(msg.getText());
        //下面是对获取的消息的处理

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐乐Gold

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值