ActiveMQ学习总结(3)——spring整合ActiveMQ

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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>
复制代码

转载于:https://my.oschina.net/zhanghaiyang/blog/606519

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/536377.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

堆树

一、堆树的定义 堆树的定义如下&#xff1a; &#xff08;1&#xff09;堆树是一颗完全二叉树&#xff1b; &#xff08;2&#xff09;堆树中某个节点的值总是不大于或不小于其孩子节点的值&#xff1b; &#xff08;3&#xff09;堆树中每个节点的子树都是堆树。 当父节点的键…

问题 G: 区间权值

问题 G: 区间权值 时间限制: 1 Sec 内存限制: 128 MB 提交: 112 解决: 49 [提交] [状态] [讨论版] [命题人:admin] 题目描述 小Bo有n个正整数a1..an&#xff0c;以及一个权值序列w1…wn&#xff0c;现在他定义 现在他想知道的值&#xff0c;需要你来帮帮他 你只需要输出答案…

步步为营 SharePoint 开发学习笔记系列 七、SharePoint Timer Job 开发

概要 项目需求要求我们每天晚上同步员工的一些信息到sharepoint 的user List &#xff0c;我们决定定制开发sharepoint timer Job,Sharepoint timer Job是sharePoint的定时作业Job,需要安装、布曙到服务器上,而这里我只是介绍下Job开发的例子&#xff0c;以供大家学习用。 开发…

windows下jenkins常见问题填坑

没有什么高深的东西&#xff0c;1 2天的时间大多数人都能自己摸索出来&#xff0c;这里将自己遇到过的问题分享出来避免其他同学再一次挖坑. 目录 1. 主从节点 2. Nuget自动包还原 3. powershell部署 4. 内网机器实现基于变化的构建 5. Github私有项目pull时限 所谓主从&#x…

HTTP首部(1)

1、报文首部 HTTP协议的请求和响应必定包含HTTP首部&#xff0c;它包括了客户端和服务端分别处理请求和响应提供所需要的信息。报文主体字儿是所需要的用户和资源的信息都在这边。  HTTP请求报文组成 方法&#xff0c;URL&#xff0c;HTTP版本&#xff0c;HTTP首部字段 HTTP响…

XMLHttpRequest+WebForm模式(接口IHttpHandler)实现ajax

首先引入ajax.js文件 创建xmlhttpRequest对象 Code//创建XMLHttpRequest对象var xmlHttp;function newXMLHttpRequest() { if (window.XMLHttpRequest) { xmlHttp new XMLHttpRequest(); } else if (window.ActiveXObject) { try { xmlHttp …

IIS 5 与IIS 6 原理介绍

[ 转] ASP.NET Process Model之一&#xff1a;IIS 和 ASP.NET ISAPI 前几天有一个朋友在MSN上问我“ASP.NET 从最初的接收到Http request到最终生成Response的整个流程到底是怎样的&#xff1f;”我觉得这个问题涉及到IIS和ASP.NETASP.NET Runtime的处理模型的问题&#xff0c;…

SharePoint v3:忘掉模拟用户Impersonate,SPSecurity.RunWithElevatedPrivileges来了

回顾&#xff1a; 在SharePoint V2 大家应该都用过模拟用户Impersonate这个功能&#xff0c; 这个功能用来暂时提升某个用户的权限&#xff0c;比如某个普通用户的本来不能修改某个列表的值&#xff0c;但是我们功能需要在修改。 缺点&#xff1a; 我们使用这个模拟用户功能…

螺旋方阵问题【数组】

输入n&#xff0c;输出n阶螺旋方阵&#xff0c;下面为5阶螺旋方阵&#xff1a;1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 下面为我的代码&#xff1a; #include <cstdio> #include <iostream> #include &…

马鞍点问题【数组】

如果在一矩阵中元素A[i][j]满足A[i][j]为第i行的最小值&#xff0c;第j行的最大值&#xff0c;则称这个元素为这个矩阵的马鞍点&#xff0c;求m*n矩阵所有的马鞍点。若需求一个矩阵的所有马鞍点&#xff0c;其实只需将矩阵的每行的最小值与每列的最大值分别求出存在相应的数组中…

求二叉树节点个数、叶子节点、节点层次与宽度

需实现&#xff1a;&#xff08;1&#xff09;输出二叉树b的节点个数 &#xff08;2&#xff09;输出二叉树b的叶子节点个数 &#xff08;3&#xff09;求二叉树b中指定节点值&#xff08;假设所有节点值不同&#xff09;的节点的层次。 &#xff08;4&#xff09;利用层次遍历…

【数学】Chaarshanbegaan at Cafebazaar

题目描述 Chaarshanbegaan is a gathering event at Cafebazaar similar to TGIF events at Google. Some entertainment programs like pantomime, foosball, Xbox/PS4, and several board games are part of the event. You are going to set up a dart game in Chaarshanbe…

【思维】Congestion Charging Zone

题目描述 Tehran municipality has set up a new charging method for the Congestion Charging Zone (CCZ) which controls the passage of vehicles in Tehran’s high-congestion areas in the congestion period (CP) from 6:30 to 19:00. There are plate detection came…

【二分】LED

题目描述 A Light-Emitting Diode (LED) is a semiconductor light source, which emits light when an electric current of voltage higher than a threshhold is applied to its leads. ACM R&D recently reported that they have succesfully developed a new LED, na…

【模拟】Thanks, TuSimple!

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId5979 Thanks, TuSimple! Time Limit: 1 Second Memory Limit: 65536 KB In the very first sentence of the very first problem, we would like to give our sincere thanks to TuSimple,…

【二维差分】Monitor

Monitor 题目&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid6514 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others) Total Submission(s): 600 Accepted Submission(s): 190 Problem Description Xiaoteng has a la…

【二分+二维前缀和】Largest Allowed Area

Largest Allowed Area 时间限制: 1 Sec 内存限制: 128 MB 提交: 146 解决: 54 [提交] [状态] [命题人:admin] 题目描述 A company is looking for land to build its headquarters. It has a lot of money and can buy as many land patches as it needs. Its goal, howev…

【数学】Floating-Point Hazard

Floating-Point Hazard 时间限制: 1 Sec 内存限制: 128 MB 提交: 106 解决: 42 [提交] [状态] [命题人:admin] 题目描述 Given the value of low, high you will have to find the value of the following expression: If you try to find the value of the above express…

【线段树】Segment Tree

Segment Tree 时间限制: 1 Sec 内存限制: 512 MB 提交: 107 解决: 23 [提交] [状态] [命题人:admin] 题目描述 Mcginn opens the code which he wrote 25 years ago. Clever Mcginn wants to know how many positive interger n satisfied that the maximum c can reach w…

扶桑号战列舰【RMQ+分治】

扶桑号战列舰 时间限制: 1 Sec 内存限制: 128 MB Special Judge 提交: 197 解决: 63 [提交] [状态] [命题人:admin] 题目描述 众所周知&#xff0c;一战过后&#xff0c;在世界列强建造超无畏级战列舰的竞争之中&#xff0c;旧日本海军根据“个舰优越主义”&#xff0c;建造了扶…