消息隊列學習-ActiveMQ(三)

6 ActiveMQ的Broker

6.1 是什麼

相當於一個ActiveMQ服務器實例

說白了,Broker其實就是實現了用代碼的形式啓動ActiveMQ將MQ嵌入到Java代碼中,以便隨時用隨時啓動,在用的時候再去啓動這樣能節省了資源,也保證了可用性。

6.2 使用不同的配置文件啓動實例

./activemq start xbean:file:/myactiveMQ/apache-activemq-5.15.12/conf/activemq02.xml
在這裏插入圖片描述

6.3 嵌入式Broker

  1. pom.xml添加
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
	<version>2.9.5</version>
</dependency>
  1. EmbedBroker.java
public class EmbedBroker {
    public static void main(String[] args) throws Exception {
        BrokerService brokerService = new BrokerService();
        brokerService.setUseJmx(true);
        brokerService.addConnector("tcp://localhost:61616");
        brokerService.start();
    }
}

執行結果:
在這裏插入圖片描述
啓動成功。

7 Spring整合ActiveMQ

7.1 Maven修改,需要添加Spring支持JMS的包

7.2 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">


<!-- 開啓包掃描 -->
    <context:component-scan base-package="com.sky.activemq" />

    <!-- 配置生產者 -->
    <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://192.168.188.131:61616" />
            </bean>
        </property>
        <property name="maxConnections" value="100" />
    </bean>

    <!-- 隊列的目的地,點對點的 -->
    <bean id="destinationQueue" 
    		class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-active-queue" />
    </bean>

    <!-- Spring提供的JMS工具類,它可以進行消息發送、接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="defaultDestination" ref="destinationQueue" />
        <property name="messageConverter">
            <bean class="org.springframework.jms.
            	support.converter.SimpleMessageConverter"/>
        </property>
    </bean>
</beans>

7.3 隊列(Queue)

7.3.1 生產者

@Service
public class SpringMQ_Produce {
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext 
        	= new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringMQ_Produce springMQ_producer = 
        (SpringMQ_Produce) applicationContext.getBean("springMQ_Produce");
        springMQ_producer.jmsTemplate.send((session) -> 
        	session.createTextMessage("*******Spring and ActiveMQ******"));
        System.out.println("send task over!");
    }
}

在這裏插入圖片描述

7.3.2 消費者

@Service
public class SpringMQ_Consumer {
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext = 
        	new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringMQ_Consumer springMQ_consumer = 
        (SpringMQ_Consumer) applicationContext.getBean("springMQ_Consumer");
        String retString  = 
        	(String) springMQ_consumer.jmsTemplate.receiveAndConvert();
        System.out.println("消費者受到消息:" + retString);
    }
}

在這裏插入圖片描述

7.4 主題(Topic)

7.4.1 修改Spring配置文件

  • 改動1:增加下面的代碼
<!-- 主題的目的地 -->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
    <constructor-arg index="0" value="spring-active-topic" />
</bean>
  • 改動2:修改jmsTemplate的defaultDestination
    <!-- Spring提供的JMS工具類,它可以進行消息發送、接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="defaultDestination" ref="destinationTopic" />
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
    </bean>

7.4.2 生產者

//代碼不變

7.4.3 消費者

//代碼不變

注意:主題topic要先啓動消費者。
結果類似7.3,不再截圖。

7.5 在Spring裏面實現消費者不啓動,直接通過配置監聽完成

7.5.1 說明

類似於前面setMessageListenner實時間提供消息

7.5.2 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       https://www.springframework.org/schema/context/spring-context.xsd">


<!-- 開啓包掃描 -->
    <context:component-scan base-package="com.sky.activemq" />

    <!-- 配置生產者 -->
    <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://192.168.188.131:61616" />
            </bean>
        </property>
        <property name="maxConnections" value="100" />
    </bean>

    <!-- 隊列的目的地,點對點的 -->
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg index="0" value="spring-active-queue" />
    </bean>

    <!-- 主題的目的地 -->
    <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg index="0" value="spring-active-topic" />
    </bean>

    <!-- Spring提供的JMS工具類,它可以進行消息發送、接收等 -->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory" />
        <property name="defaultDestination" ref="destinationTopic" />
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
        </property>
    </bean>

    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"/>
        <property name="destination" ref="destinationTopic"/>
        <property name="messageListener" ref="myMessageListener"/>
    </bean>
</beans>

7.5.3 編寫監聽類

@Component
public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        if (message instanceof TextMessage) {
            TextMessage textMessage = (TextMessage) message;
            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

消費者配置了自動監聽,就相當於在spring裏面後臺運行,有消息就運行我們實現監聽類裏面的方法。

注意:只需要執行生產者代碼即可
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章