ActiveMQ实例教程

Spring+Maven实现ActiveMQ实例。

一.先贴出pom.xml,了解需要的类库

<?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>org.jstudoframework</groupId>
    <artifactId>jstudo-activemq-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 使用最近较新的spring版本 -->
        <spring.version>4.3.7.RELEASE</spring.version>
        <activemq.version>5.13.0</activemq.version>
        <slf4j.version>1.6.6</slf4j.version>
    </properties>

    <dependencies>
        <!-- 日志处理 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <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>
        <!-- spring相关 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</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-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>${spring.version}</version>
        </dependency>
    </dependencies>
</project>


二.  消息生产者

1. 建立一个消息的生产类,采用spring的JmsTemplate 发送消息

package org.jstudoframework.activemq.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

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

/**
 * ActiveMQ 消息生产类
 */

public class MessageSender {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private String defaultQueueName = "default_queue";   //默认消息队列名称
    private String integralQueueName = "integral_queue"; //积分消息队列名称

    private JmsTemplate jmsTemplate;

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    /**
     * 发送到默认消息队列
     * @param message
     */
    public void defaultMessage(String message) {
        System.out.println("发送了一条消息到默认队列.......");
            send(message, 0);
    }

    /**
     * 发送到积分消息队列
     * @param message
     */
    public void integraMessage(String message) {
        System.out.println("发送了一条消息到积分队列.......");
        send(message, 1);
    }

    /**
     * 发送消息
     * @param message
     * @param type 0:默认队列 1:积分队列
     */
    public void send(final String message, int type) {
        try {
            String destination = type==0? defaultQueueName:integralQueueName;
            jmsTemplate.send(destination, new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    TextMessage textMessage = session.createTextMessage(message);
                    return textMessage;
                }
            });
        } catch (Exception e) {
            logger.error("发送消息失败", e);
        }
    }
}

程序里写了2个方法,分别向2个队列里发消息


2.新建ActiveMQTransportListener.java消息传输监听类,继承于TransportListener

package org.jstudoframework.activemq.demo;

import org.apache.activemq.transport.TransportListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 消息传输监听类
 * 这里就可以做消息传输过程中的一些监控及处理,比如常见的异常处理等
 */
public class ActiveMQTransportListener implements TransportListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 对消息传输命令进行监控
     * @param o
     */
    @Override
    public void onCommand(Object o) {

    }

    /**
     * 监控到异常时触发
     * @param e
     */
    @Override
    public void onException(IOException e) {
        logger.error("onException -> 消息服务器连接错误......", e);
    }

    /**
     * 当failover时触发
     */
    @Override
    public void transportInterupted() {
        logger.warn("transportInterupted -> 消息服务器连接发生中断...");
    }

    /**
     * 监控到failover恢复后进行触发
     */
    @Override
    public void transportResumed() {
        logger.info("transportResumed -> 消息服务器连接已恢复...");
    }
}


3. 生产者测试类

import org.jstudoframework.activemq.demo.MessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * ActiveMQ生产者
 */
public class MQProducerTest {
    private static Logger logger = LoggerFactory.getLogger(MQProducerTest.class);

    public static void main(String[] args) {
        try {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-producer.xml");
            context.start();
            MessageSender sender = (MessageSender) context.getBean("messageSender");
            sender.defaultMessage("jstudio");
            sender.integraMessage("how money");
            context.stop();
        } catch (Exception e) {
            logger.error("==>MQ context start error:", e);
            System.exit(0);
        } finally {
            logger.info("===>System.exit");
            System.exit(0);
        }
    }
}

4. spring 配置spring-producer.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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

       <!--队列 -->
       <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
              <constructor-arg>
                     <value>default_queue,integral_queue</value>
              </constructor-arg>
       </bean>

       <!-- 真正可以产生Connection的ConnectionFactory,由ActiveMQ提供 -->
       <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
              <!-- ActiveMQ服务地址 -->
              <property name="brokerURL" value="failover:tcp://192.168.43.131:61616" />
              <property name="userName" value="system"/>
              <property name="password" value="manager"/>
              <!-- 消息传输监听器 处理网络及服务器异常 -->
              <property name="transportListener">
                     <bean class="org.jstudoframework.activemq.demo.ActiveMQTransportListener" />
              </property>
       </bean>

       <!--
           ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory
           可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。
           要依赖于 activemq-pool包
        -->
       <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
              <property name="connectionFactory" ref="targetConnectionFactory" />
              <property name="maxConnections" value="10" />
       </bean>

       <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
       <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
              <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
              <property name="targetConnectionFactory" ref="pooledConnectionFactory" />
       </bean>

       <!-- 队列模板,Spring提供的JMS工具类,它可以进行消息发送、接收等  -->
       <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
              <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
              <property name="connectionFactory" ref="connectionFactory"/>
              <property name="defaultDestination" ref="queueDestination"/>
       </bean>

        <bean id="messageSender" class="org.jstudoframework.activemq.demo.MessageSender">
            <property name="jmsTemplate" ref="jmsTemplate"/>
        </bean>
</beans>

5. 运行MQProducerTest

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-log4j12/1.6.6/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/apache/activemq/activemq-all/5.13.0/activemq-all-5.13.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-07-30 20:46:20,240  INFO [AbstractApplicationContext.java:511] : Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@36718c9c: startup date [Sun Jul 30 20:46:20 CST 2017]; root of context hierarchy
2017-07-30 20:46:20,371  INFO [XmlBeanDefinitionReader.java:317] : Loading XML bean definitions from class path resource [spring/spring-producer.xml]
发送了一条消息到默认队列.......
2017-07-30 20:46:21,097  INFO [SingleConnectionFactory.java:311] : Established shared JMS Connection: PooledConnection { ConnectionPool[ActiveMQConnection {id=ID:UPAN-20170313NB-64383-1501418780983-1:1,clientId=null,started=false}] }
2017-07-30 20:46:21,166  INFO [ActiveMQTransportListener.java:47] : transportResumed -> 消息服务器连接已恢复...
2017-07-30 20:46:21,167  INFO [FailoverTransport.java:1074] : Successfully connected to tcp://192.168.43.131:61616
发送了一条消息到积分队列.......
2017-07-30 20:46:21,425  INFO [MQProducerTest.java:24] : ===>System.exit

向2个队列分别发送了1条消息。
打开activemq的管理后台,可以看到2个队列都有1条消息

 


三. 消息消费者

1. 消费者监听类,用于接收消息

package org.jstudoframework.activemq.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * 消费者监听类
 * 具体的消息消费类,可以在这实现具体的业务逻辑处理
 */
public class ConsumerMessageListener implements MessageListener {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 监听发送到消息队列的文本消息
     * @param message
     */
    @Override
    public void onMessage(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            System.out.println("接收到的消息内容是:" + textMessage.getText());
        } catch (JMSException e) {
            logger.error("消息解析失败", e);
        }
    }

}

2. 消费者测试类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * ActiveMQ消费者
 */
public class MQConsumerTest {
    private static Logger logger = LoggerFactory.getLogger(MQConsumerTest.class);

    public static void main(String[] args) {
        try {
            ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/spring-consumer.xml");
            context.start();
        } catch (Exception e) {
            logger.error("==>MQ context start error:", e);
            System.exit(0);
        }
    }
}

3. spring配置spring-consumer.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" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

       <!--队列 -->
       <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
              <constructor-arg>
                     <value>default_queue,integral_queue</value>
              </constructor-arg>
       </bean>

       <!-- 真正可以产生Connection的ConnectionFactory,由ActiveMQ提供 -->
       <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
              <!-- ActiveMQ服务地址 -->
              <property name="brokerURL" value="failover:tcp://192.168.43.131:61616" />
              <property name="userName" value="system"/>
              <property name="password" value="manager"/>
              <!-- 消息传输监听器 处理网络及服务器异常 -->
              <property name="transportListener">
                     <bean class="org.jstudoframework.activemq.demo.ActiveMQTransportListener" />
              </property>
       </bean>

       <!--
           ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory
           可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗。
           要依赖于 activemq-pool包
        -->
       <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
              <property name="connectionFactory" ref="targetConnectionFactory" />
              <property name="maxConnections" value="10" />
       </bean>

       <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
       <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
              <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
              <property name="targetConnectionFactory" ref="pooledConnectionFactory" />
       </bean>

       <!-- 消息监听器 -->
       <bean id="consumerMessageListener" class="org.jstudoframework.activemq.demo.ConsumerMessageListener" />

       <!-- 消息监听容器 -->
       <bean id="jmsContainer"
             class="org.springframework.jms.listener.DefaultMessageListenerContainer">
              <property name="connectionFactory" ref="connectionFactory" />
              <property name="destination" ref="queueDestination" />
              <property name="messageListener" ref="consumerMessageListener" />
              <!-- 设置固定的线程数 -->
              <property name="concurrentConsumers" value="2"></property>
              <!-- 设置动态的线程数 -->
              <property name="concurrency" value="2-5"></property>
       </bean>
</beans>

4. 运行MQConsumerTest

"D:\Program Files\Java\jdk1.7.0_55\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\Java\jdk1.7.0_55\jre\lib\charsets.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\deploy.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\javaws.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\jce.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\jfr.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\jfxrt.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\jsse.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\management-agent.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\plugin.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\resources.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\rt.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\access-bridge-64.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\dnsns.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\jaccess.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\localedata.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\sunec.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\sunjce_provider.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\sunmscapi.jar;D:\Program Files\Java\jdk1.7.0_55\jre\lib\ext\zipfs.jar;E:\JstudioProject1\jstudo-activemq-demo\target\test-classes;E:\JstudioProject1\jstudo-activemq-demo\target\classes;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-log4j12\1.6.6\slf4j-log4j12-1.6.6.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.6.6\slf4j-api-1.6.6.jar;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;C:\Users\Administrator\.m2\repository\org\apache\activemq\activemq-all\5.13.0\activemq-all-5.13.0.jar;C:\Users\Administrator\.m2\repository\org\apache\activemq\activemq-pool\5.13.0\activemq-pool-5.13.0.jar;C:\Users\Administrator\.m2\repository\org\apache\activemq\activemq-jms-pool\5.13.0\activemq-jms-pool-5.13.0.jar;C:\Users\Administrator\.m2\repository\org\apache\geronimo\specs\geronimo-jms_1.1_spec\1.1.1\geronimo-jms_1.1_spec-1.1.1.jar;C:\Users\Administrator\.m2\repository\org\apache\activemq\activemq-client\5.13.0\activemq-client-5.13.0.jar;C:\Users\Administrator\.m2\repository\org\fusesource\hawtbuf\hawtbuf\1.11\hawtbuf-1.11.jar;C:\Users\Administrator\.m2\repository\org\apache\geronimo\specs\geronimo-j2ee-management_1.1_spec\1.0.1\geronimo-j2ee-management_1.1_spec-1.0.1.jar;C:\Users\Administrator\.m2\repository\org\apache\geronimo\specs\geronimo-jta_1.0.1B_spec\1.0.1\geronimo-jta_1.0.1B_spec-1.0.1.jar;C:\Users\Administrator\.m2\repository\org\apache\commons\commons-pool2\2.4.2\commons-pool2-2.4.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.3.7.RELEASE\spring-core-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.3.7.RELEASE\spring-beans-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\4.3.7.RELEASE\spring-context-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.3.7.RELEASE\spring-expression-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-jdbc\4.3.7.RELEASE\spring-jdbc-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-web\4.3.7.RELEASE\spring-web-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-webmvc\4.3.7.RELEASE\spring-webmvc-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.3.7.RELEASE\spring-aop-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-tx\4.3.7.RELEASE\spring-tx-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-orm\4.3.7.RELEASE\spring-orm-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context-support\4.3.7.RELEASE\spring-context-support-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-test\4.3.7.RELEASE\spring-test-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-jms\4.3.7.RELEASE\spring-jms-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-messaging\4.3.7.RELEASE\spring-messaging-4.3.7.RELEASE.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-databind\2.5.0\jackson-databind-2.5.0.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-annotations\2.5.0\jackson-annotations-2.5.0.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.5.0\jackson-core-2.5.0.jar;D:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain MQConsumerTest
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/slf4j/slf4j-log4j12/1.6.6/slf4j-log4j12-1.6.6.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/C:/Users/Administrator/.m2/repository/org/apache/activemq/activemq-all/5.13.0/activemq-all-5.13.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2017-07-30 20:55:08,441  INFO [AbstractApplicationContext.java:511] : Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7bfdc405: startup date [Sun Jul 30 20:55:08 CST 2017]; root of context hierarchy
2017-07-30 20:55:08,548  INFO [XmlBeanDefinitionReader.java:317] : Loading XML bean definitions from class path resource [spring/spring-consumer.xml]
2017-07-30 20:55:09,114  INFO [DefaultLifecycleProcessor.java:341] : Starting beans in phase 2147483647
2017-07-30 20:55:09,275  INFO [SingleConnectionFactory.java:311] : Established shared JMS Connection: PooledConnection { ConnectionPool[ActiveMQConnection {id=ID:UPAN-20170313NB-64418-1501419309161-1:1,clientId=null,started=false}] }
2017-07-30 20:55:09,352  INFO [ActiveMQTransportListener.java:47] : transportResumed -> 消息服务器连接已恢复...
2017-07-30 20:55:09,352  INFO [FailoverTransport.java:1074] : Successfully connected to tcp://192.168.43.131:61616
2017-07-30 20:55:09,394  INFO [DefaultLifecycleProcessor.java:341] : Starting beans in phase 2147483647
接收到的消息内容是:jstudio
接收到的消息内容是:how money

收到了生产者发送的2条消息。
查看activemq的管理后台,发现刚才的2条消息已经被消费掉了。

 
整个测试就完成了,代码和配置都已经全部贴出。
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值