博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring集成ActiveMQ配置
阅读量:4031 次
发布时间:2019-05-24

本文共 7806 字,大约阅读时间需要 26 分钟。

1.       集成环境

Spring采用2.5.6版本,ActiveMQ使用的是5.4.2,从apache站点可以下载。本文是将Spring集成ActiveMQ来发送和接收JMS消息。

2.       集成步骤

将下载的ActiveMQ解压缩后文件夹如下

 activemq-all-5.4.2.jar是activemq的所有的类jar包。lib下面是模块分解后的jar包。将lib下面的

Java代码  
  1. /lib/activation-1.1.jar   
  2. /lib/activemq-camel-5.4.2.jar   
  3. /lib/activemq-console-5.4.2.jar   
  4. /lib/activemq-core-5.4.2.jar   
  5. /lib/activemq-jaas-5.4.2.jar   
  6. /lib/activemq-pool-5.4.2.jar   
  7. /lib/activemq-protobuf-1.1.jar   
  8. /lib/activemq-spring-5.4.2.jar   
  9. /lib/activemq-web-5.4.2.jar  
/lib/activation-1.1.jar/lib/activemq-camel-5.4.2.jar/lib/activemq-console-5.4.2.jar/lib/activemq-core-5.4.2.jar/lib/activemq-jaas-5.4.2.jar/lib/activemq-pool-5.4.2.jar/lib/activemq-protobuf-1.1.jar/lib/activemq-spring-5.4.2.jar/lib/activemq-web-5.4.2.jar

文件全部拷贝到项目中。

而Spring项目所需要的jar包如下 

Java代码  
  1. /lib/spring-beans-2.5.6.jar   
  2. /lib/spring-context-2.5.6.jar   
  3. /lib/spring-context-support-2.5.6.jar   
  4. /lib/spring-core-2.5.6.jar   
  5. /lib/spring-jms-2.5.6.jar   
  6. /lib/spring-tx.jar  
/lib/spring-beans-2.5.6.jar/lib/spring-context-2.5.6.jar/lib/spring-context-support-2.5.6.jar/lib/spring-core-2.5.6.jar/lib/spring-jms-2.5.6.jar/lib/spring-tx.jar

当然还需要一些其他的jar文件

Java代码  
  1. /lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar   
  2. /lib/jms-1.1.jar   
  3. /lib/log4j-1.2.15.jar   
  4. /lib/slf4j-api-1.6.1.jar   
  5. /lib/slf4j-nop-1.6.1.jar  
/lib/geronimo-j2ee-management_1.1_spec-1.0.1.jar/lib/jms-1.1.jar/lib/log4j-1.2.15.jar/lib/slf4j-api-1.6.1.jar/lib/slf4j-nop-1.6.1.jar

项目的依赖jar都准备好后就可以写配置文件了。

 Spring配置文件

配置文件内容如下

Java代码  
  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.   
  10.   
  11.     <!-- 配置connectionFactory -->   
  12.     <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"  
  13.         destroy-method="stop">   
  14.         <property name="connectionFactory">   
  15.             <bean class="org.apache.activemq.ActiveMQConnectionFactory">   
  16.                 <property name="brokerURL">   
  17.                     <value>tcp://127.0.0.1:61616</value>   
  18.                 </property>   
  19.             </bean>   
  20.         </property>   
  21.         <property name="maxConnections" value="100"></property>   
  22.     </bean>   
  23.   
  24.     <!-- Spring JMS Template -->   
  25.     <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">   
  26.         <property name="connectionFactory">   
  27.             <ref local="jmsFactory" />   
  28.         </property>   
  29.         <property name="defaultDestinationName" value="subject" />   
  30.         <!-- 区别它采用的模式为false是p2p为true是订阅 -->   
  31.         <property name="pubSubDomain" value="true" />   
  32.     </bean>   
  33.   
  34.     <!-- 发送消息的目的地(一个队列) -->   
  35.     <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">   
  36.         <!-- 设置消息队列的名字 -->   
  37.         <constructor-arg index="0" value="subject" />   
  38.     </bean>   
  39.   
  40.     <!-- 消息监听     -->   
  41.     <bean id="listenerContainer"  
  42.         class="org.springframework.jms.listener.DefaultMessageListenerContainer">   
  43.         <property name="concurrentConsumers" value="10" />   
  44.         <property name="connectionFactory" ref="jmsFactory" />   
  45.         <property name="destinationName" value="subject" />   
  46.         <property name="messageListener" ref="messageReceiver" />   
  47.         <property name="pubSubNoLocal" value="false"></property>   
  48.     </bean>   
  49.   
  50.     <bean id="messageReceiver"  
  51.         class="com.liuyan.jms.consumer.ProxyJMSConsumer">   
  52.         <property name="jmsTemplate" ref="jmsTemplate"></property>   
  53.     </bean>   
  54. </beans>  
tcp://127.0.0.1:61616

编写代码

消息发送者:这里面消息生产者并没有在Spring配置文件中进行配置,这里仅仅使用了Spring中的JMS模板和消息目的而已。 

Java代码  
  1. public class HelloSender {   
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {   
  7.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(   
  8.                 new String[] { "classpath:/spring/applicationContext-jms.xml" });   
  9.            
  10.         JmsTemplate template = (JmsTemplate) applicationContext   
  11.                 .getBean("jmsTemplate");   
  12.            
  13.         Destination destination = (Destination) applicationContext   
  14.                 .getBean("destination");   
  15.   
  16.         template.send(destination, new MessageCreator() {   
  17.             public Message createMessage(Session session) throws JMSException {   
  18.                 return session   
  19.                         .createTextMessage("发送消息:Hello ActiveMQ Text Message!");   
  20.             }   
  21.         });   
  22.         System.out.println("成功发送了一条JMS消息");   
  23.   
  24.     }   
  25.   
  26. }  
public class HelloSender {	/**	 * @param args	 */	public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(				new String[] { "classpath:/spring/applicationContext-jms.xml" });				JmsTemplate template = (JmsTemplate) applicationContext				.getBean("jmsTemplate");				Destination destination = (Destination) applicationContext				.getBean("destination");		template.send(destination, new MessageCreator() {			public Message createMessage(Session session) throws JMSException {				return session						.createTextMessage("发送消息:Hello ActiveMQ Text Message!");			}		});		System.out.println("成功发送了一条JMS消息");	}}

消息接收

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

* 消息题的内容定义 *

* 消息对象 接收消息对象后: 接收到的消息体*

*/public class ProxyJMSConsumer { public ProxyJMSConsumer() { } private JmsTemplate jmsTemplate; public JmsTemplate getJmsTemplate() { return jmsTemplate; } public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } /** * 监听到消息目的有消息后自动调用onMessage(Message message)方法 */ public void recive() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext( new String[] { "classpath:/spring/applicationContext-jms.xml" }); Destination destination = (Destination) applicationContext .getBean("destination"); while (true) { try { TextMessage txtmsg = (TextMessage) jmsTemplate .receive(destination); if (null != txtmsg) { System.out.println("[DB Proxy] " + txtmsg); System.out.println("[DB Proxy] 收到消息内容为: " + txtmsg.getText()); } else break; } catch (Exception e) { e.printStackTrace(); } } }}

这里边也是并不是直接使用Spring来初始化建立消息消费者实例,而是在此消费者注入了JMS模板而已。

写一个main入口,初始化消息消费者  

Java代码  
  1. public class JMSTest {   
  2.   
  3.     /**  
  4.      * @param args  
  5.      */  
  6.     public static void main(String[] args) {   
  7.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext(   
  8.                 new String[] { "classpath:/spring/applicationContext-jms.xml" });   
  9.            
  10.         ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext   
  11.                 .getBean("messageReceiver");   
  12.            
  13.         System.out.println("初始化消息消费者");   
  14.     }   
  15.   
  16. }  
public class JMSTest {	/**	 * @param args	 */	public static void main(String[] args) {		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(				new String[] { "classpath:/spring/applicationContext-jms.xml" });				ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext				.getBean("messageReceiver");				System.out.println("初始化消息消费者");	}}

使用的时候先开启ActiveMQ服务,默认是占用了61616端口。之后开启测试程序,开启2个消息消费者监听。之后再运行消息生产者的代码后,消息就可以被消息消费者接收到了。 

 

转载地址:http://znebi.baihongyu.com/

你可能感兴趣的文章
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
jtag dump内存数据
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
snprintf 函数用法
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>
没有路由器的情况下,开发板,虚拟机Ubuntu,win10主机,三者也可以ping通
查看>>
本地服务方式搭建etcd集群
查看>>
安装k8s Master高可用集群
查看>>