Sping整合ActiveMQ(三.简单Queue通信)

Sping整合ActiveMQ(一.简单Queue通信)

简单配置:只作JMSTemplate和Destination的配置,进行简单的消息传送

1.Pom文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>ActiveMQ-Spring2</groupId>
    <artifactId>ActiveMQ-Spring2</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <junit-version>4.10</junit-version>
        <slf4j-version>1.7.5</slf4j-version>
        <spring-version>3.2.3.RELEASE</spring-version>
        <jms-version>2.0</jms-version>
        <activemq-version>5.6.0</activemq-version>
        <geronimo-version>1.0.1</geronimo-version>
    </properties>

    <dependencies>
        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit-version}</version>
        </dependency>
        <!--slf4j-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>${slf4j-version}</version>
        </dependency>
        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--Spring-JMS-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring-version}</version>
        </dependency>
        <!--Javax-JMS-->
        <dependency>
            <groupId>javax.jms</groupId>
            <artifactId>javax.jms-api</artifactId>
            <version>${jms-version}</version>
        </dependency>
        <!--ActiveMQ-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>${activemq-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
            <version>${activemq-version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.geronimo.specs</groupId>
            <artifactId>geronimo-j2ee-management_1.1_spec</artifactId>
            <version>${geronimo-version}</version>
        </dependency>
    </dependencies>

</project>

 2.Spring配置文件ApplicationContext.xml

<?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">

    <!--创建JMS连接工厂-->
    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://localhost:61616"/>
    </bean>

    <!--声明ActiveMQ消息目标,目标可以是一个队列Queue,也可以是一个主题Topic-->
    <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="FirstQueue"/>
    </bean>

    <!--配置JMS模板-->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultDestination" ref="destination"/>  <!--此处可省-->
    </bean>

</beans>

 3.发送消息测试类Sender

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

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

public class Sender {
    public static void main(String[] args) {
        /*1.加载配置文件*/
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] { "classpath:/applicationContext.xml" });
        /*2.获取JmsTemplate实例对象*/
        JmsTemplate template = (JmsTemplate) ctx.getBean("jmsTemplate");
        /*当配置文件中的JmsTemplate的Bean中没有配置Destination的property的时候,
          使用如下注释的方式*/
        //Destination destination = (Destination) ctx.getBean("destination");
        //template.send(destination, new MessageCreator() {......}
        template.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("发送消息:Hello ActiveMQ Text Message!");
            }
        });
        System.out.println("成功发送了一条JMS消息");
    }
}
 

 4.接收消息测试类Receiver

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

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

public class Receive {
    public static void main(String[] args) throws JMSException {
        /*1.加载配置文件*/
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                new String[] { "classpath:/applicationContext.xml" });
        /*2.获取JMSTemplate实例*/
        JmsTemplate jmsTemplate = (JmsTemplate) ctx.getBean("jmsTemplate");
        //Destination destination = (Destination) ctx.getBean("destination");
        /*开始接收消息*/
        while (true) {        
            /*当配置文件中的JmsTemplate的Bean中没有配置Destination的property的时候,
            使用如下注释的方式*/            
            //TextMessage txtmsg = (TextMessage) template.receive(destination);
            TextMessage message = (TextMessage) jmsTemplate.receive();
            if (null != message) {
                System.out.println("收到消息为:" + message.getText());
            }
            else
                break;
        }
    }
}

 

你可能感兴趣的:(java,spring,activemq,MQ)