在Spring下集成ActiveMQ

1.参考文献

  1. Spring集成ActiveMQ配置
  2. Spring JMS异步发收消息 ActiveMQ

2.环境

在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spring环境下集成ActiveMQ。如果要在spring下集成ActiveMQ,那么就需要将如下jar包导入项目:

在Spring下集成ActiveMQ

本文有两篇参考文献,因此有两个实例,项目结构如下图所示:

在Spring下集成ActiveMQ

3.实例1

信息发送者:HelloSender.java

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq;

import javax.jms.JMSException;
import javax.jms.Session;

import javax.jms.Destination;
import javax.jms.Message;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class HelloSender {

    /**
     * @param args
     * jmsTemplate和destination都是在spring配置文件中进行配制的
     * Sender只使用了配置文件中的jmsFactory,jmsTemplate,还有destination这三个属性
     */
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        JmsTemplate template = (JmsTemplate) applicationContext.getBean("jmsTemplate");
        Destination destination = (Destination) applicationContext.getBean("destination");
        template.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("发送消息:Hello ActiveMQ Text Message2!");
            }
        });
        System.out.println("成功发送了一条JMS消息");
    }
}
在Spring下集成ActiveMQ

信息接受者:ProxyJMSConsumer.java

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;

/**
 * JMS消费者
 * 消息题的内容定义
 * 消息对象 接收消息对象后: 接收到的消息体* <p> 
 */
public class ProxyJMSConsumer {

    public ProxyJMSConsumer() {

    }
    private JmsTemplate jmsTemplate;

    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    /**
     * 监听到消息目的有消息后自动调用onMessage(Message message)方法
     */
    public void recive() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        Destination destination = (Destination) applicationContext.getBean("destination");
        while (true) {
            try {
                TextMessage txtmsg = (TextMessage) jmsTemplate
                        .receive(destination);
                if (null != txtmsg) {
                    System.out.println("[DB Proxy] " + txtmsg);
                    System.out.println("[DB Proxy] 收到消息内容为: "
                            + txtmsg.getText());
                } else
                    break;
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}
在Spring下集成ActiveMQ

客户端:JMSTest.java

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JMSTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-jms.xml");
        ProxyJMSConsumer proxyJMSConsumer = (ProxyJMSConsumer) applicationContext.getBean("messageReceiver");
        proxyJMSConsumer.recive();
        
        System.out.println("初始化消息消费者");
    }

}
在Spring下集成ActiveMQ

Spring配置文件:applicationContext-jms.xml

在Spring下集成ActiveMQ
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-2.5.xsd"
    default-autowire="byName">


    <!-- 配置connectionFactory -->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory"
        destroy-method="stop">
        <property name="connectionFactory">
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL">
                    <value>tcp://127.0.0.1:61616</value>
                </property>
            </bean>
        </property>
        <property name="maxConnections" value="100"></property>
    </bean>

    <!-- Spring JMS Template -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory">
            <ref local="jmsFactory" />
        </property>
        <property name="defaultDestinationName" value="subject" />
        <!-- 区别它采用的模式为false是p2p为true是订阅 -->
        <property name="pubSubDomain" value="true" />
    </bean>

    <!-- 发送消息的目的地(一个队列) -->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQTopic">
        <!-- 设置消息队列的名字 -->
        <constructor-arg index="0" value="subject" />
    </bean>




    <bean id="messageReceiver" class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer">
        <!--class="edu.sjtu.erplab.springactivemq.ProxyJMSConsumer">-->
        <property name="jmsTemplate" ref="jmsTemplate"></property>
    </bean>
    
    
    
</beans>
在Spring下集成ActiveMQ

测试方法:首先运行JMSTest,然后运行HelloSender。

4.实例2

消息发送者: Sender

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class Sender {
    private JmsTemplate jmsTemplate;
    //getter and setter
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    public void sendInfo() {
        jmsTemplate.send(new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                MapMessage message = session.createMapMessage();
                message.setString("lastName", "ppp");
                return message;
            }

        });
    }
}
在Spring下集成ActiveMQ

消息发送客户端:SenderTest

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SenderTest {
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sender sender = (Sender) context.getBean("sender");
        sender.sendInfo();
    }
}
在Spring下集成ActiveMQ

消息接收者:Receiver

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq2;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.support.JmsUtils;

public class Receiver {
    private JmsTemplate jmsTemplate;
    //getter and setter
    public JmsTemplate getJmsTemplate() {
        return jmsTemplate;
    }
    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }
    
    /**
     * 构造函数
     */
    public Receiver() {
    }

    public String receiveMessage() {
        String my = "";
        MapMessage message = (MapMessage) jmsTemplate.receive();
        try {
            my = message.getString("lastName");
        } catch (JMSException e) {
            throw JmsUtils.convertJmsAccessException(e);
        }
        return my;
    }


}
在Spring下集成ActiveMQ

消息接收客户端:ReceiverTest

在Spring下集成ActiveMQ
package edu.sjtu.erplab.springactivemq2;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ReceiverTest {
    public static void main(String[] args) {
        // TODO 自动生成方法存根
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Receiver receiver = (Receiver) context.getBean("receiver");
        System.out.print(receiver.receiveMessage());
    }
}
在Spring下集成ActiveMQ

Spring配置文件:applicationContext.xml

在Spring下集成ActiveMQ
<?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.0.xsd">
    <!--创建连接工厂-->
    <bean id="connectionFactory"
        class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"></property>
    </bean>
    <!-- 声明ActiveMQ消息目标,目标可以是一个队列,也可以是一个主题ActiveMQTopic-->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="edu.sjtu.erplab.springactivemq2"></constructor-arg>
    </bean>
    <!---->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="defaultDestination" ref="destination"></property>
        <property name="receiveTimeout" value="600"></property>

    </bean>
    <bean id="sender" class="edu.sjtu.erplab.springactivemq2.Sender">
        <property name="jmsTemplate" ref="jmsTemplate"></property>

    </bean>
    <bean id="receiver" class="edu.sjtu.erplab.springactivemq2.Receiver">
        <property name="jmsTemplate" ref="jmsTemplate"></property>
    </bean>
</beans>
在Spring下集成ActiveMQ

相关推荐