1.参考文献

  1. Spring集成ActiveMQ配置
  2. Spring
    JMS异步发收消息 ActiveMQ

2.环境

在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spring环境下集成ActiveMQ。如果要在spring下集成ActiveMQ,那么就需要将如下jar包导入项目:

本文有两篇参考文献,因此有两个实例,项目结构如下图所示:

3.实例1

信息发送者:HelloSender.java

package edu.sjtu.erplab.springactivemq;

import javax.jms.JMSException;
import javax.jms.Session;

import javax.jms.Destination;
import javax.jms.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class HelloSender {

/**
* @param args
* jmsTemplate和destination都是在spring配置文件中进行配制的
* Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("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 Message2!");
}
});
System.out.println("成功发送了一条JMS消息");
}
}

信息接受者:ProxyJMSConsumer.java

package edu.sjtu.erplab.springactivemq;

import javax.jms.Destination;
import javax.jms.TextMessage;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

/**
* JMS消费者
* 消息题的内容定义
* 消息对象 接收消息对象后: 接收到的消息体* <p>
*/
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("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();
}

}
}

}

客户端:JMSTest.java

package edu.sjtu.erplab.springactivemq;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JMSTest {

/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
proxyJMSConsumer.recive();

System.out.println("初始化消息消费者");
}

}

Spring配置文件:applicationContext-jms.xml

<?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/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">

<!-- 配置connectionFactory -->
<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
destroy-method="stop">
<property name="connectionFactory">
<bean class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://127.0.0.1:61616</value>
</property>
</bean>
</property>
<property name="maxConnections" value="100"></property>
</bean>

<!-- Spring JMS Template -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref local="jmsFactory" />
</property>
<property name="defaultDestinationName" value="subject" />
<!-- 区别它采用的模式为false是p2p为true是订阅 -->
<property name="pubSubDomain" value="true" />
</bean>

<!-- 发送消息的目的地(一个队列) -->
<bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
<!-- 设置消息队列的名字 -->
<constructor-arg index="0" value="subject" />
</bean>

<bean id="messageReceiver" class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer">
<!--class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer">-->
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>



</beans>

测试方法:首先运行JMSTest,然后运行HelloSender。

4.实例2

消息发送者: Sender

package edu.sjtu.erplab.springactivemq2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class Sender {
private JmsTemplate jmsTemplate;
//getter and setter
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

public void sendInfo() {
jmsTemplate.send(new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
MapMessage message = session.createMapMessage();
message.setString("lastName", "ppp");
return message;
}

});
}
}

消息发送客户端:SenderTest

package edu.sjtu.erplab.springactivemq2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SenderTest {
public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Sender sender = (Sender) context.getBean("sender");
sender.sendInfo();
}
}

消息接收者:Receiver

package edu.sjtu.erplab.springactivemq2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;

public class Receiver {
private JmsTemplate jmsTemplate;
//getter and setter
public JmsTemplate getJmsTemplate() {
return jmsTemplate;
}
public void setJmsTemplate(JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
}

/**
* 构造函数
*/
public Receiver() {
}

public String receiveMessage() {
String my = "";
MapMessage message = (MapMessage) jmsTemplate.receive();
try {
my = message.getString("lastName");
} catch (JMSException e) {
throw JmsUtils.convertJmsAccessException(e);
}
return my;
}

}

消息接收客户端:ReceiverTest

package edu.sjtu.erplab.springactivemq2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ReceiverTest {
public static void main(String[] args) {
// TODO 自动生成方法存根
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Receiver receiver = (Receiver) context.getBean("receiver");
System.out.print(receiver.receiveMessage());
}
}

Spring配置文件:applicationContext.xml

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

http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!--创建连接工厂-->
<bean id="connectionFactory"
class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616"></property>
</bean>
<!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="edu.sjtu.erplab.springactivemq2"></constructor-arg>
</bean>
<!---->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="defaultDestination" ref="destination"></property>
<property name="receiveTimeout" value="600"></property>

</bean>
<bean id="sender" class="edu.sjtu.erplab.springactivemq2.Sender">
<property name="jmsTemplate" ref="jmsTemplate"></property>

</bean>
<bean id="receiver" class="edu.sjtu.erplab.springactivemq2.Receiver">
<property name="jmsTemplate" ref="jmsTemplate"></property>
</bean>
</beans>

ActiveMQ学习总结(3)——spring整合ActiveMQ的更多相关文章

  1. Spring整合ActiveMQ及多个Queue消息监听的配置

        消息队列(MQ)越来越火,在java开发的项目也属于比较常见的技术,MQ的相关使用也成java开发人员必备的技能.笔者公司采用的MQ是ActiveMQ,且消息都是用的点对点的模式.本文记录了实 ...

  2. spring整合ActiveMq

    spring整合ActiveMq: 1:依赖的jar包: 2:spring-activemq.xml    的配置: 代码: <?xml version="1.0" enco ...

  3. 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler

    spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...

  4. ActiveMQ 入门和与 Spring 整合

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

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

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

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

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

  7. spring 整合 ActiveMQ

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

  8. Java消息队列-Spring整合ActiveMq

    1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...

  9. Spring整合ActiveMq消息队列

    ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...

随机推荐

  1. Myeclipse学习总结(4)——Eclipse常用开发插件

    (1)    AmaterasUML         介绍:Eclipse的UML插件,支持UML活动图,class图,sequence图,usecase图等:支持与Java class/interf ...

  2. SCU 1095运送物资(最短路)

    SCU 1095运送物资(最短路) X国发生了内战.起义军得到了广大人民的支持.在一次战役中,反动军队结集了大量兵力,围攻起义军的主堡W城.为支援前线,后方各个供给基地城市纷纷准备将物资运往W城.各基 ...

  3. HDOJ 5411 CRB and Puzzle 矩阵高速幂

    直接构造矩阵,最上面一行加一排1.高速幂计算矩阵的m次方,统计第一行的和 CRB and Puzzle Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. [React] Make Controlled React Components with Control Props

    Sometimes users of your component want to have more control over what the internal state is. In this ...

  5. (2) 我的结果- spec2006中精确的simulation points运行点

    spec06中获取simpoints的环境说明: spec的版本号为spec2006v1.0; 使用ref input with runspec; 100millions为周期生成的simpoints ...

  6. 怎样给UINavigationBar加入button?

    Mads Mobæk:给UINavigationBar加入button的演示样例代码 1 2 3 4 5 6 7 8 UIBarButtonItem *rightButton = [[UIBarBut ...

  7. (WPF)XAML 过程式代码

    当代码编译.x:Code 元素的内容将被放到.g.cs文件. <Window x:Class="WpfApplication7.MainWindow" xmlns=" ...

  8. java command not found

    INSTALLATION PROCEDURE ON RASPBERRY PI The remaining steps should be performed directly on the conso ...

  9. js sort()函数 排序问题 var arr =['A-1-5-1','A-1-10-2','A-1-5-5','B-2-3-1','C-4-10-1'], 对这个数组进行排序,想达到的效果是["A-1-5-1", "A-1-5-5", "A-4-10-1", "A-1-10-2", "A-2-3-1"]

    先介绍个方法 charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. stringObject.charCodeAt(index) ...

  10. 快速安装Nginx及配置详解(未完待续)

    导读: Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,从2007年被德国人开发出来后可以说在市场的占有率一路飙升,因为它支持高并 ...