Activemq detailed tutorial and why to use activemq

mq can send a variety of data formats, among which data streams can be sent, then you can convert files into data streams and send them to the java message middleware such as mq. Other consumers can receive messages in a topic or point-to-point mode. It is also possible to convert the data stream to a file.
The typical use of activemq is financial software. We need to get real-time changing stock prices. Activemq also has high availability features and good performance in network.
The following describes the use of activemq
1. Import the jar package

 <!-- 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. The activemq configuration file is actually to manage activemq in spring

<?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. Send peer-to-peer messages (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. Actively accept news

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

5. The listener listens for messages

/**
 * 监听器
 * @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();
        }

    }

}

Summary:
1. The difference between sending a topic message and sending a point-to-point message is actually a different destination (destination), jmstemplate is different
2. The listener accepts a message, whether it is a point-to-point message and a topic message, but the destination is different
3. One point-to-point mode Messages, only one consumer can accept
4. Topic mode A message can have multiple consumers to accept
5 Each listener needs to configure the listening container in spring

Demo code cloud address: https://git.oschina.net/hujunna/activemqjinjie.git

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325725927&siteId=291194637