本文章的完整代码可从我的github中下载:https://github.com/huangbowen521/SpringJMSSample.git

上一篇文章中介绍了如何安装和运行ActiveMQ。这一章主要讲述如何使用Spring JMS向ActiveMQ的Message Queue中发消息和读消息。

首先需要在项目中引入依赖库。

  • spring-core: 用于启动Spring容器,加载bean。

  • spring-jms:使用Spring JMS提供的API。

  • activemq-all:使用ActiveMQ提供的API。

在本示例中我使用maven来导入相应的依赖库。

pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
  <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.0.2.RELEASE</version>
</dependency>
</dependencies>

接下来配置与ActiveMQ的连接,以及一个自定义的MessageSender。

springJMSConfiguration.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?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.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>application.properties</value>
</property>
</bean> <!-- Activemq connection factory -->
<bean id="amqConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<constructor-arg index="0" value="${jms.broker.url}"/>
</bean> <!-- ConnectionFactory Definition -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory"/>
</bean> <!-- Default Destination Queue Definition-->
<bean id="defaultDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0" value="${jms.queue.name}"/>
</bean> <!-- JmsTemplate Definition -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="defaultDestination" ref="defaultDestination"/>
</bean> <!-- Message Sender Definition -->
<bean id="messageSender" class="huangbowen.net.jms.MessageSender">
<constructor-arg index="0" ref="jmsTemplate"/>
</bean>
</beans>

在此配置文件中,我们配置了一个ActiveMQ的connection factory,使用的是ActiveMQ提供的ActiveMQConnectionFactory类。然后又配置了一个Spring JMS提供的CachingConnectionFactory。我们定义了一个ActiveMQQueue作为消息的接收Queue。并创建了一个JmsTemplate,使用了之前创建的ConnectionFactory和Message Queue作为参数。最后自定义了一个MessageSender,使用该JmsTemplate进行消息发送。

以下MessageSender的实现。

MessageSender.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package huangbowen.net.jms;

import org.springframework.jms.core.JmsTemplate;

public class MessageSender {

    private final JmsTemplate jmsTemplate;

    public MessageSender(final JmsTemplate jmsTemplate) {
this.jmsTemplate = jmsTemplate;
} public void send(final String text) {
jmsTemplate.convertAndSend(text);
}
}

这个MessageSender很简单,就是通过jmsTemplate发送一个字符串信息。

我们还需要配置一个Listener来监听和处理当前的Message Queue。

springJMSReceiver.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.xsd"> <!-- Message Receiver Definition -->
<bean id="messageReceiver" class="huangbowen.net.jms.MessageReceiver">
</bean>
<bean class="org.springframework.jms.listener.SimpleMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"/>
<property name="destinationName" value="${jms.queue.name}"/>
<property name="messageListener" ref="messageReceiver"/>
</bean> </beans>

在上述xml文件中,我们自定义了一个MessageListener,并且使用Spring提供的SimpleMessageListenerContainer作为Container。

以下是MessageLinser的具体实现。

MessageReceiver.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package huangbowen.net.jms;

import javax.jms.*;

public class MessageReceiver implements MessageListener {

    public void onMessage(Message message) {
if(message instanceof TextMessage) {
TextMessage textMessage = (TextMessage) message;
try {
String text = textMessage.getText();
System.out.println(String.format("Received: %s",text));
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}

这个MessageListener也相当的简单,就是从Queue中读取出消息以后输出到当前控制台中。

另外有关ActiveMQ的url和所使用的Message Queue的配置在application.properties文件中。

application.properties
1
2
jms.broker.url=tcp://localhost:61616
jms.queue.name=bar

好了,配置大功告成。如何演示那?我创建了两个Main方法,一个用于发送消息到ActiveMQ的MessageQueue中,一个用于从MessageQueue中读取消息。

SenderApp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package huangbowen.net;

import huangbowen.net.jms.MessageSender;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.util.StringUtils; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; public class SenderApp
{
public static void main( String[] args ) throws IOException {
MessageSender sender = getMessageSender();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String text = br.readLine(); while (!StringUtils.isEmpty(text)) {
System.out.println(String.format("send message: %s", text));
sender.send(text);
text = br.readLine();
}
} public static MessageSender getMessageSender() {
ApplicationContext context = new ClassPathXmlApplicationContext("springJMSConfiguration.xml");
return (MessageSender) context.getBean("messageSender");
}
}
ReceiverApp.java
1
2
3
4
5
6
7
8
9
10
package huangbowen.net;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ReceiverApp {
public static void main( String[] args )
{
new ClassPathXmlApplicationContext("springJMSConfiguration.xml", "springJMSReceiver.xml");
}
}

OK,如果运行的话要先将ActiveMQ服务启动起来(更多启动方式参见我上篇文章)。

1
$:/usr/local/Cellar/activemq/5.8.0/libexec$ activemq start xbean:./conf/activemq-demo.xml

然后运行SenderApp中的Main方法,就可以在控制台中输入消息发送到ActiveMQ的Message Queue中了。运行ReceiverApp中的Main方法,则会从Queue中将消息读出来,打印到控制台。

这就是使用Spring JMS与ActiveMQ交互的一个简单例子了。完整代码可从https://github.com/huangbowen521/SpringJMSSample下载。

ActiveMQ第二弹:使用Spring JMS与ActiveMQ通讯的更多相关文章

  1. 【ActiveMQ】Spring Jms集成ActiveMQ学习记录

    Spring Jms集成ActiveMQ学习记录. 引入依赖包 无论生产者还是消费者均引入这些包: <properties> <spring.version>3.0.5.REL ...

  2. 消费者端的Spring JMS 连接ActiveMQ接收生产者Oozie Server发送的Oozie作业执行结果

    一,介绍 Oozie是一个Hadoop工作流服务器,接收Client提交的作业(MapReduce作业)请求,并把该作业提交给MapReduce执行.同时,Oozie还可以实现消息通知功能,只要配置好 ...

  3. 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例

    第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...

  4. JMS之——ActiveMQ时抛出的错误Could not connect to broker URL-使用线程池解决高并发连接

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/69046395 解决使用activemq时抛出的异常:javax.j ms.JMSE ...

  5. 04.ActiveMQ与Spring JMS整合

        SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...

  6. ActiveMQ学习笔记(5)——使用Spring JMS收发消息

      摘要 ActiveMQ学习笔记(四)http://my.oschina.net/xiaoxishan/blog/380446 中记录了如何使用原生的方式从ActiveMQ中收发消息.可以看出,每次 ...

  7. Spring JMS ActiveMQ整合(转)

    转载自:http://my.oschina.net/xiaoxishan/blog/381209#comment-list ActiveMQ学习笔记(四)http://my.oschina.net/x ...

  8. ActiveMQ第三弹:在Spring中使用内置的Message Broker

    在上个例子中我们演示了如何使用Spring JMS来向ActiveMQ发送消息和接收消息.但是这个例子需要先从控制台使用ActiveMQ提供的命令行功能启动一个Message Broker,然后才能运 ...

  9. spring集成JMS访问ActiveMQ

    首先我们搭建一个spring-mvc项目,项目可以参考:spring-mvc 学习笔记 步骤: 在pom.xml中加上需要的包 修改web.xml,增加IOC容器 spring配置文件applicat ...

随机推荐

  1. VC++ 控制外部程序,向外部程序发送一个消息的方法

    这里需要考虑两部分的内容: 发送端: 查找对应的窗体,找到CWnd的值 向窗体发送消息 举例: CWnd* wnd = FindWindow(NULL, _T("选择题做题过程中" ...

  2. VC++ 在控件上写字时 字体的设置技巧

    //人物照片下方的文字 CFont* nFont = &afxGlobalData.fontRegular; CFont* oFont = pDc->SelectObject(nFont ...

  3. okhttp3的使用

    http://www.qingpingshan.com/rjbc/az/110232.html 请求队列,post? 待定 http://www.07net01.com/2015/12/1017279 ...

  4. jquery mobile开发笔记之Ajax提交数据(转)

    http://my.oschina.net/xiahuawuyu/blog/81763 这两天学习了下,jquery mobile(以下简称jqm)的开发相关的内容.可能之前有过web的开发基础,相对 ...

  5. css样式表 格式与布局

    1 样式表  内联样式表  内嵌样式表  外部样式表 2 选择器 标签选择器 <style type="text\css" class 选择器  以.开头 ID选择器 以#开 ...

  6. winform-全局异常捕获作用

    using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;using Jxs ...

  7. detection reading

    1512.07729v1 G-CNN an Iterative Grid Based Object Detector,先基于空间金字塔生成很多矩形框,然后把这些矩形框作为regions,进行fast ...

  8. 1.Java内存区域

    Java虚拟机在执行java程序的过程中会把他管理的内存划分为若干个不同的数据区域各自用途.创建以及销毁时间各不相同.有的随着虚拟机进行的启动而存在,有的区域依赖于线程的启动和结束而建立以及销毁.如图 ...

  9. NC WebService接口开发流程

    一.定义类: 接口类 包定义在public下,接口类名为I开头,Service结尾 实现类 包定义在private下,实现类名以ServiceImpl结尾 VO类 若有VO类,也放在public下 U ...

  10. ubuntu下完全安装mcrypt

    源文章: ubuntu下安装mcrypt 1.首先要下载三个软件 0libmcrypt-2.5.8.tar.gz 下载地址:http://sourceforge.net/project/showfil ...