消费者,使用监听的实现方式。

1. pom.xml

2. 生产者

  1. package org.ygy.mq.lesson04;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.Session;
  5. import org.springframework.jms.core.JmsTemplate;
  6. import org.springframework.jms.core.MessageCreator;
  7. /**
  8. *
  9. * @author 于贵洋
  10. * @description 生产者
  11. * @time 2013-10-14
  12. * @version V1.0
  13. */
  14. public class Lesson4Producer {
  15. private JmsTemplate jmsTemplate;
  16. public JmsTemplate getJmsTemplate() {
  17. return jmsTemplate;
  18. }
  19. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  20. this.jmsTemplate = jmsTemplate;
  21. }
  22. //使用JMSTemplate发送消息
  23. public void send(final String msg) {
  24. jmsTemplate.send(new MessageCreator() {
  25. public Message createMessage(Session session) throws JMSException {
  26. return session.createTextMessage(msg);
  27. }
  28. });
  29. }
  30. }

3. 消费者

  1. package org.ygy.mq.lesson04;
  2. import javax.jms.JMSException;
  3. import javax.jms.Message;
  4. import javax.jms.MessageListener;
  5. import javax.jms.TextMessage;
  6. /**
  7. *
  8. * @author 于贵洋
  9. * @description 通过监听的方式实现消费者
  10. * @time 2013-10-14
  11. * @version V1.0
  12. */
  13. public class Lesson4Consumer implements MessageListener {
  14. @Override
  15. public void onMessage(Message message) {
  16. TextMessage textMsg = (TextMessage) message;
  17. try {
  18. System.out.println("接收到了消息,消息内容是:" + textMsg.getText());
  19. } catch (JMSException e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. }

4. 配置文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  6. http://www.springframework.org/schema/context
  7. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  8. <!-- 1.配置connectionFactory -->
  9. <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
  10. <property name="connectionFactory">
  11. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  12. <property name="brokerURL">
  13. <value>tcp://127.0.0.1:61616</value>
  14. </property>
  15. </bean>
  16. </property>
  17. <property name="maxConnections" value="100"></property>
  18. </bean>
  19. <!-- 2.定义消息目标 -->
  20. <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
  21. <constructor-arg index="0" value="queue_temp"></constructor-arg>
  22. </bean>
  23. <!-- 3.配置Spring JMS Template -->
  24. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  25. <!-- 3.1 注入jmsFactory -->
  26. <property name="connectionFactory" ref="jmsFactory"></property>
  27. <!-- 3.2 配置消息目标 -->
  28. <property name="defaultDestination" ref="destination" />
  29. </bean>
  30. <!-- 4.配置生产者 -->
  31. <bean id="lesson4Producer" class="org.ygy.mq.lesson04.Lesson4Producer">
  32. <property name="jmsTemplate" ref="jmsTemplate"/>
  33. </bean>
  34. <!-- 5.配置消费者 -->
  35. <bean id="lesson4Consumer" class="org.ygy.mq.lesson04.Lesson4Consumer"></bean>
  36. <!-- 消息监听容器 -->
  37. <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  38. <property name="connectionFactory" ref="jmsFactory" />
  39. <property name="destination" ref="destination" />
  40. <property name="messageListener" ref="lesson4Consumer" />
  41. </bean>
  42. </beans>

配置文件的话,重点关注消费者的配置,将其配置在一个消息监听容器中。

这里的监听容器有多个,明天会分享或者,直接转载一些文章。

5. 测试

    1. package org.ygy.mq.lesson04;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. /**
    5. * 配置了监听容器
    6. *
    7. * 参考:http://haohaoxuexi.iteye.com/blog/1893038
    8. * @author Administrator
    9. *
    10. */
    11. public class Client4 {
    12. public static void main(String[] args) {
    13. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("lesson4.xml");
    14. Lesson4Producer producer = (Lesson4Producer) applicationContext.getBean("lesson4Producer");
    15. producer.send("你在哪里啊?");
    16. producer.send("嗨,你好吗?");
    17. }
    18. }

ActiveMQ与Spring整合-MessageListener的更多相关文章

  1. ActiveMQ (三) Spring整合JMS入门

    Spring整合JMS入门 前提:安装好了ActiveMQ  ActiveMQ安装 Demo结构:   生产者项目springjms_producer: pom.xml <?xml versio ...

  2. 消息中间件ActiveMQ及Spring整合JMS

    一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...

  3. ActiveMQ与spring整合

    第一步:引用相关的jar包 <dependency> <groupId>org.springframework</groupId> <artifactId&g ...

  4. 消息中间件ActiveMQ及Spring整合JMS的介绍

    一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...

  5. ActiveMQ学习总结------Spring整合ActiveMQ 04

    通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...

  6. Spring整合ActiveMQ实现消息延迟投递和定时投递

    linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...

  7. ActiveMQ 与 Spring

    1. ActiveMQ安装 1.1 下载(版本5.14.5) 点我官网下载 1.2 安装 解压下载的压缩文件到任意目录中(eg. C:\Program Files (x86)\apache-activ ...

  8. spring整合MQ

    ---恢复内容开始--- 一. 导入依赖 <dependencies> <!-- ActiveMQ客户端完整jar包依赖 --> <dependency> < ...

  9. spring 整合 ActiveMQ

    1.1     JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...

随机推荐

  1. php excel

    项目中需要把excel转为索引数组,不输出key 只说下普世技巧 找了php excel插件 发现需要createReader方法,在sublime中search,可以搜索文件内容,找到使用creat ...

  2. html5 datalist

    教程:http://www.w3school.com.cn/html5/html5_datalist.asp 提供自动完成的文本框

  3. [zjoi2010]cheese

    题目: 贪吃的老鼠(cheese.c/cpp/pas/in/out) 时限:每个测试点10秒 [问题描述] 奶酪店里最近出现了m只老鼠!它们的目标就是把生产出来的所有奶酪都吃掉.奶酪店中一天会生产n块 ...

  4. power designer简单教程

    1.file->new model 2.根据菜单或功能模型新建physical diagram(当然也可以新建package) 选中项目,右键——>new ->physical di ...

  5. 论EFMS模拟量部分采集电路的修改

    论1:电阻R11的作用 如图1是2014-3-11之前模拟量采集的部分硬件电路,图2是纠正后的正确电路. D5是SA20CA,TVS双向二极管,有效防止外接电源的浪涌冲击情况,保护电路.  D6是稳压 ...

  6. adb错误 - INSTALL_FAILED_NO_MATCHING_ABIS

    #背景 换组啦,去了UC国际浏览器,被拥抱变化了.还在熟悉阶段,尝试了下adb,然后就碰到了这个INSTALL_FAILED_NO_MATCHING_ABIS的坑... #解决方法 INSTALL_F ...

  7. netcore 发布 到 windows server IIS

    net core 和普通net 发布没有什么不同,只需要在个别地方注意下: 1. 在visual Studio 2017 发布 2. 把发布好的文件copy到服务器上,并新建一个网站,同时要注意选择无 ...

  8. c#依参数自动生成控件

    很多系统都带有自定义报表的功能,而此功能都需依参数自动生成控件,举例如下: 如上图,一条查询语句当中,包含了3个参数,其中两个是日期型(使用:DATE!进行标识),一个是字符型(使用:进行标识),要生 ...

  9. Lerning Entity Framework 6 ------ Introduction to TPT

    Sometimes, you've created a table for example named Person. Just then, you want to add some extra in ...

  10. redis缓存存在的隐患及其解决方案

    redis缓存1.缓存穿透 1>.什么是缓存穿透? 业务系统需要查训的数据根本不存在,当业务系统查询时, 首先会在缓存中查训,由于缓存中不存在,然后在往数据 库中查,由于该数据在数据库中也不存在 ...