【ActiveMQ入门-8】ActiveMQ学习-与Spring集成

概述:



下面将介绍如何在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。


运行结果:


来自为知笔记(Wiz)

附件列表

时间: 2024-08-03 14:32:20

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

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

概述: 下面将介绍如何在Spring下集成ActiveMQ. 消费者:同步接收: 目的地:Queue 环境: 共5个文件 Receiver.java ReceiverTest.java Sender.java SenderTest.java applicationContext.xml 使用的jar包如下: 源文件: Receiver.java package com.ll.springActiveMQ2; import javax.jms.JMSException; import javax.

ActiveMQ(二)与Spring集成

声明 转载请注明出处! Reprint please indicate the source! http://www.hiknowledge.top/2017/04/03/activemq%ef%bc%88%e4%ba%8c%ef%bc%89%e4%b8%8espring%e9%9b%86%e6%88%90/ ActiveMQ与Spring集成 由于历史原因,JMS有4个版本.Spring提供了用于简化JMS API使用的抽象框架,并且对用户屏蔽了JMS API中1.0.2和1.1版本的差异.

Spring集成ActiveMQ配置 --转

转自:http://suhuanzheng7784877.iteye.com/blog/969865 集成环境 Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可以下载.本文是将Spring集成ActiveMQ来发送和接收JMS消息. 集成步骤 将下载的ActiveMQ解压缩后文件夹如下 activemq-all-5.4.2.jar是activemq的所有的类jar包.lib下面是模块分解后的jar包.将lib下面的 Java代码 /lib/activati

Spring集成Activemq使用(未完待续)

现在任何一个框架的使用都会结合spring框架,quartz.cxf与平时常见的Hibernate.mybatis.Struts等都可以与spring集成起来使用,在这里研究了activemq结合spring的使用方法. 1.理论篇 spring集成JMS连接ActiveMq ConnectionFactory:用于管理连接的工厂(Spring为我们提供的连接池,因为JmsTemplate每次发消息都会重新创建连接.会话和producer,这个操作非常消耗性能,所以Spring提供了连接池) s

【ActiveMQ入门-5】ActiveMQ学习-消息持久性

ActiveMQ中的消息持久性     ActiveMQ很好的支持了消息的持久性(Persistence).消息持久性对于可靠消息传递来说应该是一种比较好的方法,有了消息持久化,即使发送者和接受者不是同时在线或者消息中心在发送者发送消息后宕机了,在消息中心重新启动后仍然可以将消息发送出去,如果把这种持久化和ReliableMessaging结合起来应该是很好的保证了消息的可靠传送. 消息持久性的原理很简单,就是在发送者将消息发送出去后,消息中心首先将消息存储到本地数据文件.内存数据库或者远程数据

java高级视频课程Dubbo、Redis、ActiveMQ、Nginx、Mycat、Spring、MongoDB、ZeroMQ、Git、Nosql、Jvm、Mecached、Netty、Nio、Mina

* { font-family: "Microsoft YaHei" !important } h1 { color: #FF0 } 15套java架构师.集群.高可用.高可扩 展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布 式项目实战视频教程 视频课程包含: 高级Java架构师包含:Spring boot.Spring  cloud.Dubbo.Redis.ActiveMQ.Nginx.Mycat

java架构师负载均衡、高并发、nginx优化、tomcat集群、异步性能优化、Dubbo分布式、Redis持久化、ActiveMQ中间件、Netty互联网、spring大型分布式项目实战视频教程百度网盘

15套Java架构师详情 * { font-family: "Microsoft YaHei" !important } h1 { background-color: #006; color: #FF0 } 15套java架构师.集群.高可用.高可扩展.高性能.高并发.性能优化.Spring boot.Redis.ActiveMQ.Nginx.Mycat.Netty.Jvm大型分布式项目实战视频教程 视频课程包含: 高级Java架构师包含:Spring boot.Spring  clo

JMS实现-ActiveMQ,介绍,安装,使用,注意点,spring整合

[TOC] 缘由: 最近在用netty开发游戏服务器,目前有这样的一个场景,聊天服务器和逻辑服务器要进行消息交互,比如,某个玩家往某个公会提交了加入申请,这个申请动作是在逻辑服务器上完成的,但是要产生一条申请消息,由聊天服务器推送到对应的公会频道,目前这个申请消息就是通过jms发送到聊天服务器上,聊天服务器监听到后,推送到对应的公会频道. 下面主要介绍以下几点 - JMS简介 - 消息传递模型 - ActiveMQ介绍 - 安装使用 - spring整合JMS - 代码相关 JMS简介 J Ja

ActiveMQ入门实例

1.下载ActiveMQ 去官方网站下载:http://activemq.apache.org/ 2.运行ActiveMQ 解压缩apache-activemq-5.5.1-bin.zip,然后双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序. 启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue. 3.创建Eclipse项目并运行 创建project:Ac