Spring 和ActionMQ整合JMS开发应用

本文介绍如何使用Spring框架整合ActiveMQ实现JMS消息传递。通过示例代码展示了服务端发送消息到队列以及客户端接收消息的过程,并提供了XML配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Spring 和ActionMQ整合JMS开发应用,实现异步的消息的应用.

 使用Apache的ActiveMQ发送消息,activemq-all-5.2.jar

spring2.5 jar;jms.jar 等

 

 

服务段代码:

package com.unutrip.activemq.jms;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

/**
 * 消息发送者
 *
 * @author longgangbai
 *
 */
public class SpringJMSProductor {
 private JmsTemplate template;
 private Destination destination;
    /**
     * 发送消息
     * @param message
     */
 public void createMessage(final String message) {
  template.send(destination, new MessageCreator() {
   public Message createMessage(Session session) throws JMSException {
    return session.createTextMessage(message);
   }
  });
  System.out.println(message);
 }

 public JmsTemplate getTemplate() {
  return template;
 }

 public void setTemplate(JmsTemplate template) {
  this.template = template;
 }

 public Destination getDestination() {
  return destination;
 }

 public void setDestination(Destination destination) {
  this.destination = destination;
 }
}

 

客户端代码:

package com.unutrip.activemq.jms;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.TextMessage;

import org.springframework.jms.core.JmsTemplate;

/**
 * 消息接收者
 *
 * @
 */
public class SpringJMSReceiver {
 private JmsTemplate template;
 private Destination destination;

 public JmsTemplate getTemplate() {
  return template;
 }

 public void setTemplate(JmsTemplate template) {
  this.template = template;
 }

 public Destination getDestination() {
  return destination;
 }

 public void setDestination(Destination destination) {
  this.destination = destination;
 }

 public void receive() throws JMSException {
  while (true) {
   TextMessage txtmsg = (TextMessage) template.receive(destination);
   if (null != txtmsg)
    System.out.println("收到消息内容为: " + txtmsg.getText());
   else
    break;
  }
 }
}

 

xml配置如下:

本实例中客户端和服务端在一台电脑上,共用一個 配置JMS模版 和发送消息的目的地(一个队列)

JMS连接工厂和,jMS目标类

<?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.5.xsd">
     
        <!--
        <bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
          <property name="config" value="classpath:org/apache/activemq/xbean/activemq.xml" />
          <property name="start" value="true" />
        </bean>
         -->
        <!-- 配置JMS连接工厂 -->
        <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="vm://localhost" />
        </bean>

        <!-- 配置JMS模版 -->
        <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
                <property name="connectionFactory" ref="connectionFactory"/>
        </bean>

        <!-- 发送消息的目的地(一个队列) -->
        <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
                <!-- 设置消息队列的名字 -->
                <constructor-arg index="0" value="HelloWorldQueue"/>
        </bean>
       
        <!--服务端用于发送JMS消息 -->
        <bean id="jmsproductor" class="com.unutrip.activemq.jms.SpringJMSProductor">
           <property name="template">
             <ref bean="jmsTemplate"/>
           </property>
           <property name="destination">
               <ref bean="destination"/>
           </property>
        </bean>
       
        <!-- 客户端用于接受JMS消息 -->
        <bean id="jmsreceiver" class="com.unutrip.activemq.jms.SpringJMSReceiver">
           <property name="template">
             <ref bean="jmsTemplate"/>
           </property>
           <property name="destination">
               <ref bean="destination"/>
           </property>
        </bean>
</beans>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值