您的位置:首页 > 运维架构 > Linux

linux下 消息中间件ActiveMQ整合spring笔记二 接收消息

2017-08-20 13:03 686 查看
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> 
<!-- JMS厂商提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg name="brokerURL" value="tcp://192.168.25.127:61616"></constructor-arg>
</bean>

<!-- spring对ConnectionFactory的封装 -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="targetConnectionFactory"></property>
</bean>

<!--配置消息的Destination对象 queue方式 -->
<bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg name="name" value="test-queue"></constructor-arg>
</bean>

<!--配置消息的Destination对象 topic方式 -->
<bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg name="name" value="test-topic"></constructor-arg>
</bean>

<!-- 配置消息的接收者 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="test-queue" />
<property name="messageListener" ref="myMessageListener" />
</bean>
<bean id="myMessageListener" class="com.taotao.search.listener.MyMessageListener"/>
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="test-queue" />
<property name="messageListener" ref="myMessageListener" />
</bean>

</beans>

创建一个实现MessageListener接口的类  用于接收消息

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage
4000
;

public class MyMessageListener implements MessageListener {

@Override
public void onMessage(Message message) {

TextMessage messages = (TextMessage) message;
try {
System.out.println("xioaxi"+messages.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}


测试代码
import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpringActiveMQ {

@Test
public void test() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");

System.in.read();
}

}

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: