概述:

下面将介绍如何在Spring下集成ActiveMQ。
消费者:同步接收;
目的地:topic



环境:

主要包括4个文件:
  1. HelloSender.java;
  2. JMSTest.java;
  3. ProxyJMSConsumer.java;
  4. applicationContext-jms.xml(配置文件);
需要使用的jar包如下:

源文件:

HelloSender.java

  1. package com.ll.springActiveMQ1;
  2. import javax.jms.Destination;
  3. import javax.jms.JMSException;
  4. import javax.jms.Message;
  5. import javax.jms.Session;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.ClassPathXmlApplicationContext;
  8. import org.springframework.jms.core.JmsTemplate;
  9. import org.springframework.jms.core.MessageCreator;
  10. public class HelloSender {
  11. /**
  12. * @param args
  13. * jmsTemplate和destination都是在spring配置文件中进行配制的
  14. * Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性
  15. */
  16. public static void main(String[] args) {
  17. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
  18. JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");
  19. Destination destination = (Destination) applicationContext.getBean("destination");
  20. template.send(destination, new MessageCreator() {
  21. public Message createMessage(Session session) throws JMSException {
  22. return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
  23. }
  24. });
  25. System.out.println("成功发送了一条JMS消息");
  26. }
  27. }

ProxyJMSConsumer.java

  1. package com.ll.springActiveMQ1;
  2. import javax.jms.Destination;
  3. import javax.jms.TextMessage;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. import org.springframework.jms.core.JmsTemplate;
  7. /**
  8. * JMS消费者 消息题的内容定义 消息对象 接收消息对象后: 接收到的消息体*
  9. * <p>
  10. */
  11. public class ProxyJMSConsumer {
  12. /**
  13. * 构造函数
  14. */
  15. public ProxyJMSConsumer() {
  16. }
  17. /**
  18. *
  19. */
  20. private JmsTemplate jmsTemplate;
  21. public JmsTemplate getJmsTemplate() {
  22. return jmsTemplate;
  23. }
  24. public void setJmsTemplate(JmsTemplate jmsTemplate) {
  25. this.jmsTemplate = jmsTemplate;
  26. }
  27. /**
  28. * 监听到消息目的有消息后自动调用onMessage(Message message)方法
  29. */
  30. public void recive() {
  31. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
  32. "applicationContext-jms.xml");
  33. Destination destination = (Destination) applicationContext
  34. .getBean("destination");
  35. while (true) {
  36. try {
  37. //同步接收
  38. TextMessage txtmsg = (TextMessage) jmsTemplate
  39. .receive(destination);
  40. if (null != txtmsg) {
  41. System.out.println("[DB Proxy] " + txtmsg);
  42. System.out.println("[DB Proxy] 收到消息内容为: "
  43. + txtmsg.getText());
  44. } else {
  45. break;
  46. }
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. }
  51. }
  52. }

JMSTest.java

  1. package com.ll.springActiveMQ1;
  2. import org.apache.commons.logging.Log;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class JMSTest {
  6. /**
  7. * @param args
  8. */
  9. public static void main(String[] args) {
  10. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
  11. ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
  12. proxyJMSConsumer.recive();
  13. System.out.println("初始化消息消费者");
  14. }
  15. }


配置文件:

applicationContext-jms.xml

  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. default-autowire="byName">
  9. <!-- 配置connectionFactory -->
  10. <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
  11. destroy-method="stop">
  12. <property name="connectionFactory">
  13. <bean class="org.apache.activemq.ActiveMQConnectionFactory">
  14. <property name="brokerURL">
  15. <value>tcp://127.0.0.1:61616</value>
  16. </property>
  17. </bean>
  18. </property>
  19. <property name="maxConnections" value="100"></property>
  20. </bean>
  21. <!-- Spring JMS Template -->
  22. <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
  23. <property name="connectionFactory">
  24. <ref local="jmsFactory" />
  25. </property>
  26. <property name="defaultDestinationName" value="subject" />
  27. <!-- 区别它采用的模式为false是p2p为true是订阅 -->
  28. <property name="pubSubDomain" value="true" />
  29. </bean>
  30. <!-- 发送消息的目的地(一个队列) -->
  31. <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
  32. <!-- 设置消息队列的名字 -->
  33. <constructor-arg index="0" value="subject" />
  34. </bean>
  35. <bean id="messageReceiver" class="com.ll.springActiveMQ1.ProxyJMSConsumer">
  36. <property name="jmsTemplate" ref="jmsTemplate"></property>
  37. </bean>
  38. </beans>



运行顺序:


首先运行JMSTest,然后运行HelloSender。


运行结果:








附件列表

【ActiveMQ入门-8】ActiveMQ学习-与Spring集成的更多相关文章

  1. 【spring源码学习】spring集成orm数据框架

    [一]简易的数据源配置 (1)配置文件 <!--springJdbcTemplemate数据操作配置信息 --> <bean id="driver" class= ...

  2. 【ActiveMQ入门-9】ActiveMQ学习-与Spring集成2

    概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:Queue 环境: 共5个文件 Receiver.java ReceiverTest.java Sender. ...

  3. ActiveMQ入门之四--ActiveMQ持久化方式

    消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和 ...

  4. MyBatis 学习-与 Spring 集成篇

    根据官方的说法,在 ibatis3,也就是 Mybatis3 问世之前,Spring3 的开发工作就已经完成了,所以 Spring3 中还是没有对 Mybatis3 的支持.因此由 Mybatis 社 ...

  5. ActiveMQ 入门和与 Spring 整合

    ActiveMQ 入门演示 activemq 依赖 <dependency> <groupId>org.apache.activemq</groupId> < ...

  6. Spring学习笔记--spring+mybatis集成

    前言: 技术的发展, 真的是日新月异. 作为javaer, 都不约而同地抛弃裸写jdbc代码, 而用各种持久化框架. 从hibernate, Spring的JDBCTemplate, 到ibatis, ...

  7. Spring Boot学习笔记——Spring Boot与ActiveMQ的集成

    Spring Boot对JMS(Java Message Service,Java消息服务)也提供了自动配置的支持,其主要支持的JMS实现有ActiveMQ.Artemis等.这里以ActiveMQ为 ...

  8. 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)

    你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...

  9. 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)

    从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...

随机推荐

  1. C++模板详解——使用篇

    假如我们需要取得两个变量中较大的变量,或许,我们可以通过重载的方式实现,如下. int max(int fir,int sec); float max(float fir,float sec); do ...

  2. Redis (一) 概念安装

    一.阿里云安装Redis 1.安装Redis yum -y install redis 2.启动Redis service redis start 或者(推荐使用) systemctl start  ...

  3. OPENWRT常用设置

    常用设置: 计划任务,定时重启 系统--计划任务,每行一个计划任务. 然后是计划任务列表的格式: [minute] [hour] [day of month] [month] [day of week ...

  4. 一、WCF学习之旅-创建第一个服务

    WCF基本介绍:http://baike.baidu.com/link?url=TGjLYt3HS4dt4-hIiGRknLy6udRsZ52QxJz9cmRKlR4NXbP9rCZDsKn2fDfG ...

  5. oracle之TRUNC函数

    TRUNC(number,num_digits) Number 需要截尾取整的数字. Num_digits 用于指定取整精度的数字.Num_digits 的默认值为 0.如果Num_digits为正数 ...

  6. Java通过class文件得到所在jar包

    今天遇到一个问题,需要通过知道的class文件得到该文件所在的jar包,试过很多办法都不行,最后在网上找到了一个解决办法,如下: demo.java 1 2 String path = XXX.cla ...

  7. STL标准库-算法-常用算法

    技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 介绍11种STL标准库的算法,从这11种算法中总结一下算法的基本使用 1.accumulate() 累加 2.for_each( ...

  8. Java实现责任链模式

    责任链模式: 将接受者对象连成一条链,并在该链上传递请求,直到一个几首这对象处理它.通过让更多对象有机会处理请求,避免了请求发送者和接受者之间的耦合. 责任链模式的优缺点: 优点:高内聚,低耦合.业务 ...

  9. iOS-----推送机制(上)

    推 送 机 制 使用NSNotificationCenter通信 NSNotificationCenter实现了观察者模式,允许应用的不同对象之间以松耦合的方式进行通信. NSNotification ...

  10. Hign-Speed Tracking with Kernelzied Correlation Filters

    reference:Hign-Speed Tracking with Kernelzied Correlation Filters questions: The core componet of mo ...