- 前言
- 第一部分 核心实现
- 第 1 章 Spring 整体架构和环境搭建
- 第 2 章 容器的基本实现
- 第 3 章 默认标签的解析
- 第 4 章 自定义标签的解析
- 第 5 章 bean 的加载
- 第 6 章 容器的功能扩展
- 第 7 章 AOP
- 第二部分 企业应用
- 第 8 章 数据库连接 JDBC
- 第 9 章 整合 MyBatis
- 第 10 章 事务
- 第 11 章 SpringMVC
- 第 12 章 远程服务
- 第 13 章 Spring 消息
13.2 Spring 整合 ActiveMQ
整个消息的发送与接收过程非常简单,但是其中却参杂着大量的冗余代码,比如 Connection 的创建与关闭,Session 的创建与关闭等,为了消除这一冗余工作量,Spring 进行了进一步的封装。Spring 下的 ActiveMQ 使用方式如下。
(1)Spring 配置文件。
配置文件是 Spring 的核心,Spring 整合消息服务的使用也从配置文件配置开始。类似于数据库操作,Spring 也将 ActiveMQ 中的操作统一封装至 JmsTemplate 中,以方便我们统一使用。所以,在 Spring 的核心配置文件中首先要注册 JmsTemplate 类型的 bean。当然,ActiveMQConnection Factory 用于连接消息服务器,是消息服务的基础,也要注册。ActiveMQQueue 则用于指定消息的目的地。
<beans>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.Springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="connectionFactory" />
</property>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0">
<value>HelloWorldQueue</value>
</constructor-arg>
</bean>
</beans>
(2)发送端。
有了以上的配置,Spring 就可以根据配置信息简化我们的工作量。Spring 中使用发送消息到消息服务器,省去了冗余的 Connection 以及 Session 等的创建与销毁过程,简化了工作量。
public class HelloWorldSender {
public static void main(String args[]) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "test/activeMQ/Spring/applicationContext.xml" });
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
Destination destination = (Destination) context.getBean("destination");
jmsTemplate.send(destination, new MessageCreator() {
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage("大家好这个是测试!");
}
});
}
}
(3)接收端。
同样,在 Spring 中接收消息也非常方便,Spring 中连接服务器接收消息的示例如下:
public class HelloWorldReciver {
public static void main(String args[]) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext(
new String[] { "test/activeMQ/Spring/applicationContext.xml" });
JmsTemplate jmsTemplate = (JmsTemplate) context.getBean("jmsTemplate");
Destination destination = (Destination) context.getBean("destination");
TextMessage msg = (TextMessage) jmsTemplate.receive(destination);
System.out.println("reviced msg is:" + msg.getText());
}
}
到这里我们已经完成了 Spring 消息的发送与接收操作。但是,如 HelloWorldReciver 中所示的代码,使用 jmsTemplate.receive(destination) 方法只能接收一次消息,如果未接收到消息,则会一直等待,当然用户可以通过设置 timeout 属性来控制等待时间,但是一旦接收到消息本次接收任务就会结束,虽然用户可以通过 while(true) 的方式来实现循环监听消息服务器上的消息,还有一种更好的解决办法:创建消息监听器。消息监听器的使用方式如下。
(1)创建消息监听器。
用于监听消息,一旦有新消息 Spring 会将消息引导至消息监听器以方便用户进行相应的逻辑处理。
public class MyMessageListener implements MessageListener{
@Override
public void onMessage(Message arg0) {
TextMessage msg = (TextMessage) arg0;
try {
System.out.println(msg.getText());
} catch (JMSException e) {
e.printStackTrace();
}
}
}
(2)修改配置文件。
为了使用消息监听器,需要在配置文件中注册消息容器,并将消息监听器注入到容器中。
<beans>
<bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL">
<value>tcp://localhost:61616</value>
</property>
</bean>
<bean id="jmsTemplate" class="org.Springframework.jms.core.JmsTemplate">
<property name="connectionFactory">
<ref bean="connectionFactory" />
</property>
</bean>
<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg index="0">
<value>HelloWorldQueue</value>
</constructor-arg>
</bean>
<bean id="myTextListener" class="test.activeMQ.Spring.MyMessageListener" />
<bean id="javaConsumer"
class="org.Springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="destination" />
<property name="messageListener" ref="myTextListener" />
</bean>
</beans>
通过以上的修改便可以进行消息监听的功能了,一旦有消息传入至消息服务器,则会被消息监听器监听到,并由 Spring 将消息内容引导至消息监听器的处理函数中等待用户的进一步逻辑处理。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论