activemq详细使用教程以及为什么要使用activemq

mq可以发送多种数据格式,其中就可以发送数据流,那么可以吧文件转换成数据流,发送到mq这种java消息中间件,其他消费者,可以以主题或者点对点的模式来接受消息,接受数据流然后转换成文件,也是可以实现的。
activemq典型的使用,就是金融软件,我们需要得到实时变化的股价,activemq还有高可用的特性,在网络方面性能也很好
下面介绍activemq的使用方法
1.导入jar包

 <!-- activemq -->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-all</artifactId>
            <version>5.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.1.4.RELEASE</version>
        </dependency>

2.activemq配置文件,其实就是在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.xsd">

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

    <!-- 定义消息队列(Queue) -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <!-- 设置消息队列的名字 -->
        <constructor-arg>
            <value>queue1</value>
        </constructor-arg>
    </bean>

    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="defaultDestination" ref="queueDestination" />
        <property name="receiveTimeout" value="10000" />
    </bean>

    <!--queue消息生产者 -->
    <bean id="activemqImpl" class="study.ActivemqImpl">
    </bean>
    <!-- 消息队列监听者(Queue) -->
    <bean id="queueMessageListener" class="study.CustomerMessageListener" />

    <!-- 消息监听容器(Queue) -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="queueDestination" />
        <property name="messageListener" ref="queueMessageListener" />
    </bean>
    <!-- 主题目的地 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg>
            <value>topic_test</value>
        </constructor-arg>
    </bean>
    <!--主题jmsTemplate  -->
    <bean id="topicJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <property name="defaultDestination" ref="topicDestination" />
        <property name="pubSubDomain" value="true" />
        <property name="receiveTimeout" value="10000" />
    </bean>
    <!-- 主题监听器 -->
    <bean id="topicListener" class="study.TopicListener">
    </bean>
    <!-- 主题监听器容器 -->
    <bean id="topicContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="topicListener" />
    </bean>
    <!-- 主题监听器 2-->
    <bean id="topicListener2" class="study.TopicListener2">
    </bean>
    <!-- 主题监听器容器2 -->
    <bean id="topicContainer2" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory" />
        <property name="destination" ref="topicDestination" />
        <property name="messageListener" ref="topicListener2" />
    </bean>
</beans>

3.发送点对点消息(queue)

    public void ProviderQueue(Destination destination, final String msg) {
        jmsTemplate.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        });
    }

4.主动接受消息

public void accept(Destination destination) {
        TextMessage textMessage=(TextMessage) jmsTemplate.receive(destination);
        try {
            System.out.println("主动接受消息"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }

5.监听器监听消息

/**
 * 监听器
 * @author qbm
 *
 */
public class TopicListener implements MessageListener {

    public void onMessage(Message arg0) {
        TextMessage textMessage=(TextMessage)arg0;
        try {
            System.out.println("主题消息监听器监听到:"+textMessage.getText());
        } catch (JMSException e) {
            e.printStackTrace();
        }

    }

}

总结:
1.发送主题消息和发送点对点消息的不同其实就是目的地不同(destination),jmstemplate不同
2.监听器接受消息不管是点对点消息和主题消息的写法一直,只是目的地不同
3.点对点模式一个消息,只能有一个消费者去接受
4.主题模式一个消息可以有多个消费者去接受
5每个监听器需要在spring中配置监听容器

demo码云地址:https://git.oschina.net/hujunna/activemqjinjie.git


版权声明:本文为hujun_123456原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。