返回介绍

13.2 Spring 整合 ActiveMQ

发布于 2025-04-22 22:09:18 字数 4666 浏览 0 评论 0 收藏

整个消息的发送与接收过程非常简单,但是其中却参杂着大量的冗余代码,比如 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 将消息内容引导至消息监听器的处理函数中等待用户的进一步逻辑处理。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。