Spring集成ActiveMQ配置 --转

转自:http://suhuanzheng7784877.iteye.com/blog/969865

  1. 集成环境

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

  1. 集成步骤

将下载的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

文件全部拷贝到项目中。

而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

当然还需要一些其他的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

项目的依赖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. <!-- 配置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. <!-- 消息监听     -->
  36. <bean id="listenerContainer"
  37. class="org.springframework.jms.listener.DefaultMessageListenerContainer">
  38. <property name="concurrentConsumers" value="10" />
  39. <property name="connectionFactory" ref="jmsFactory" />
  40. <property name="destinationName" value="subject" />
  41. <property name="messageListener" ref="messageReceiver" />
  42. <property name="pubSubNoLocal" value="false"></property>
  43. </bean>
  44. <bean id="messageReceiver"
  45. class="com.liuyan.jms.consumer.ProxyJMSConsumer">
  46. <property name="jmsTemplate" ref="jmsTemplate"></property>
  47. </bean>
  48. </beans>

编写代码

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

Java代码

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

消息接收

Java代码

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

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

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

Java代码

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

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

时间: 2024-08-28 00:02:03

Spring集成ActiveMQ配置 --转的相关文章

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

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

Spring 集成mybatis 配置方法

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/sch

spring集成ActiveMQ居然要依赖这么多包

做spring和ActiveMQ的集成,作maven依赖的时候有感(以前都不在乎,现在不一样了........省略) <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-spring --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-spring</ar

spring 集成mongo配置

spring继承Mongo使用的是spring-data. 如果需要加入两个mongo与spring集成的包,spring-data-commons-1.7.0.RELEASE.jar,spring-data-mongodb-1.4.0.RELEASE.jar. mongo的配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.

160530、memcached集群(spring集成的配置)

第一步:在linux机或windows机上安装memcached服务端(server) linux中安装memcached:centos中命令 yum -y install memcached 如果没有联网下载mencached的安装包(注意:需要安装依赖libevent) 从网上下载安装包,解压后make&&make install就完成了 第二步:项目中导入memcached客户端jar(client)有两个版本,选择2.4的 第三步:spring集成memcached(memcach

Spring集成JPA配置懒加载两个报错解决办法

一:报错no session 因为entitymanager对象在事物提交后就关闭了 报错的 no session相当于sql的session 解决办法:解决办法 在web.xmL配置一个过滤器 使其在这个session中的manager在结束后再关闭open <!--配置openmanager--> <filter> <filter-name>openEntity</filter-name> <filter-class>org.springfr

spring集成activeMQ

1.安装activehttp://activemq.apache.org/activemq-5140-release.html2.运行D:\apache-activemq-5.14.0\bin\win64\activemq.bat如果端口占用就netstat -ano|findstr 56723.运行项目 下载

解决Spring集成Activemq使用ObjectMessage报错

**Exception in thread "main" org.springframework.jms.UncategorizedJmsException: Uncategorized exception occured during JMS processing; nested exception is javax.jms.JMSException: Failed to build body from content. Serializable class not availabl

spring 集成 log4j 配置

在web.xml中增加如下代码: <context-param> <param-name>log4jConfigLocation</param-name> <param-value>/WEB-INF/classes/log4j.properties</param-value> </context-param> <context-param> <param-name>webAppRootKey</param