ActiveMQ(三)
来源:互联网 发布:生产线电子看板数据 编辑:程序博客网 时间:2024/06/05 00:45
spring整合ActiveMQ
配置ConnectionFactory
<!--JMS厂商提供的ConnectionFactory --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg name="brokerURL" value="tcp://ip:61616"></constructor-arg> </bean> <!--Spring对ConnectionFactory的封装 --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"></property> </bean>
配置生产者
<!-- 配置JMS template --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"></property> </bean> <!-- 配置destination对象 --> <bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="test-queue"></constructor-arg> </bean> <bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="test-topic"></constructor-arg> </bean>
发送消息,写测试类
@Test public void testJMSTemlate() { // 初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-activemq.xml"); // 从容器中获得JMS对象 JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class); // 从容器中获取Destination对象 Destination destination = (Destination) applicationContext.getBean("test-queue"); // 发送消息 jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { TextMessage textMessage = session.createTextMessage("spring activeMQ"); return textMessage; } }); }
2.配置消费者
一个MessageListenerContainer的时候需要指定三个属性:
1、一个是表示从哪里监听的ConnectionFactory
2、一个是表示监听什么的Destination;
3、一个是接收到消息以后进行消息处理的MessageListener。
常用的MessageListenerContainer实现类是DefaultMessageListenerContainer。
MessageListener.java
public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { // 接收到消息 try { TextMessage textMessage = (TextMessage) message; String text = textMessage.getText(); // 打印消息 System.out.println(text); } catch (JMSException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
spring配置文件
<!--JMS厂商提供的ConnectionFactory --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg name="brokerURL" value="tcp://ip:61616"></constructor-arg> </bean> <!--Spring对ConnectionFactory的封装 --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"></property> </bean> <!-- 配置destination对象 --> <bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="test-queue"></constructor-arg> </bean> <bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="test-topic"></constructor-arg> </bean> <!-- MessageListener对象 --> <bean id="myMessageListener" class="com.taotao.listener.MyMessageListener"></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>
接收消息,编写测试类
@Test public void testSpringActiveMq() throws IOException { // 初始化spring容器 ApplicationContext context = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-activemq.xml"); // 等待 System.in.read(); }
阅读全文
0 0
- ActiveMQ 三
- ActiveMQ(三)
- ActiveMQ(三)
- ActiveMQ(三)
- ActiveMQ(三)--ActiveMQ服务启动
- activeMQ学习三
- ActiveMQ 八 (实战三)
- apache-activemq(三)
- activemq的部署(三)
- ActiveMQ之三:启动ActiveMQ的Broker
- 【ActiveMQ】activemq 三种消息监听器
- Apache ActiveMQ教程(三)
- ActiveMQ应用笔记三:安全性
- Apache ActiveMQ教程(三)
- ActiveMQ 分析系列(三)
- activemq 三种消息监听器
- ActiveMQ(三):消息监听器 MessageListener
- activemq 三种消息监听器
- TensorFlow实战4:实现简单的多层神经网络案例
- c++编译提示:系统找不到指定路径
- Android 快速搭建App架构
- 接口代理
- Linux下安装rar命令,简易而不简单
- ActiveMQ(三)
- Python编程从入门到实践:习题4-10~4-12
- [NOIP2017][luogu3959]宝藏treasure
- 久未更新,希望能跟上大家的进度,装载问题。
- Salesforce通过Schema Namespace的DescribeSObjectResult Class获取对象的基本信息
- 权限控制filter
- 盒子不占一行
- 菜鸟学Java之Set接口之HashSet浅析
- leetcode43. Multiply Strings