本文章的完整代码可从我的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. nginx环境下配置nagios-关于perl-fcgi.pl

    配置文件如下: 请注意,网上提供的官方文档在运行时可能会出现问题,此文中保证无问题. ; ; ; ;  ); ;  ); ; ;          my $pidnumber = $$;        ...

  2. spi_flash

    http://blog.chinaunix.net/uid-27406766-id-3384699.html

  3. encache学习教程

    http://www.cnblogs.com/hoojo/archive/2012/07/12/2587556.html

  4. WSGI

    [WSGI] WSGI:Web Server Gateway Interface. WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求.我们来看一个最简单的Web版本的 ...

  5. PostgreSQL用户角色及其属性介绍

    1.CREATE ROLE创建的用户默认不带LOGIN属性,而CREATE USER创建的用户默认带有LOGIN属性,如下: postgres=# CREATE ROLE pg_test_user_1 ...

  6. 【Python全栈笔记】00 12-14 Oct Linux 和 Python 基础

    Linux 基础认识 更加稳定,安全,开源 设置好ssh协议后可以通过windows系统连接Linux,基于ssh协议进行通信 '/' 为根目录 cd / -> 切换到根目录 ls -lh 列出 ...

  7. alert/confirm/prompt 处理

    webdriver 中处理JavaScript 所生成的alert.confirm 以及prompt 是很简单的.具体思路是使用switch_to_alert()方法定位到alert/confirm/ ...

  8. udp-->socket通信原理

    UDP数据通讯原理     UDP数据通讯分服务端(软件)和客户端端:     服务端(软件)(服务器)先运行,服务端,不需要事先知道客户端IP和port     客户端(软件)(客户端机器)后运行, ...

  9. SQL语句-批量插入表(表数据插表)

    批量插入表(表数据插表) ****1.INSERT INTO SELECT语句语句形式为:Insert into Table2(field1,field2,...) select value1,val ...

  10. 连通图模板(HDU 1269)

    http://acm.hdu.edu.cn/showproblem.php?pid=1269 题目大意:给定一个图,判断该图是否是强连通图.(强连通图为从任意一点出发,可到达其他所有点).深搜的Tar ...