ActiveMQ學習之Spring整合ActiveMQ------>監聽配置

1、pom

<dependencies>
    <!--activemq-->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.9</version>
    </dependency>

    <!--activeMQ對jms的支持,整合spring和activemq-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jms</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <!--activeMQ所須要的pool-->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-pool</artifactId>
      <version>5.15.9</version>
    </dependency>

    <!--fastjson-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.9</version>
    </dependency>
    <!--javabean-->
    <dependency>
      <groupId>org.apache.xbean</groupId>
      <artifactId>xbean-spring</artifactId>
      <version>3.16</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <!--spring 切面-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <!--spring 對持久化框架支持-->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>4.3.23.RELEASE</version>
    </dependency>
    <!--aop註解-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjrt</artifactId>
      <version>1.6.1</version>
    </dependency>
    <!--aop切入點-->
    <dependency>
      <groupId>org.aspectj</groupId>
      <artifactId>aspectjweaver</artifactId>
      <version>1.8.13</version>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>2.1_2</version>
    </dependency>
  </dependencies>

2、spring配置文件(application.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: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-3.2.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!--開啓包的自動掃面-->
    <context:component-scan base-package="cn.bdqn"></context:component-scan>
    <!--配置生產者-->
    <bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
        <property name="connectionFactory">
           <!--真正生產Connection和ConnectionFactory,由對應的jms服務廠商提供-->
            <bean class="org.apache.activemq.ActiveMQConnectionFactory">
                <property name="brokerURL" value="tcp://192.168.44.135:61616"></property>
            </bean>
        </property>
    <property name="maxConnections" value="100"></property>
    </bean>
    <!--隊列目的地,點對點-->
    <bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
        <!--value 值是消息的名稱-->
        <constructor-arg index="0" value="spring-active-queue"></constructor-arg>
    </bean>
    <!--主題目的地-->
    <bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
        <!--value 值是消息的名稱-->
        <constructor-arg index="0" value="spring-active-topic"></constructor-arg>
    </bean>

    <!--spring提供的jms工具類,他能夠進行消息發送、接受等-->
    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="jmsFactory"></property>
        <!--此處配置爲隊列-->
       <!-- <property name="defaultDestination" ref="destinationQueue"></property>-->
        <!--此處配置爲主題-->
         <property name="defaultDestination" ref="destinationTopic"></property>
        <property name="messageConverter">
            <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"></bean>
        </property>
    </bean>
    <!--配置監聽程序-->
    <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="jmsFactory"></property>
        <property name="destination" ref="destinationTopic"></property>
        <property name="messageListener" ref="myMessageListener"></property>
    </bean>
    <!--配置監聽bean  此處註釋掉是由於使用的註解-->
   <!-- <bean id="myMessageListener" class="cn.**.listener.MyMessageListener"></bean>-->
</beans>

3、監聽器

import org.springframework.stereotype.Component;

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

/**
 * @ProjectName: springActiveMQ
 * @Package: cn.**.listener
 * @Author: huat
 * @Date: 2020/1/21 14:39
 * @Version: 1.0
 */
@Component
public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        if(null != message&& message instanceof TextMessage){
            TextMessage textMessage=(TextMessage)message;
            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

4、消息生產者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import javax.jms.TextMessage;

/**
 * @ProjectName: springActiveMQ
 * @Package: cn.**.spring
 * @Author: huat
 * @Date: 2020/1/21 9:30
 * @Version: 1.0
 * 主題生產者
 */
@Service
public class SpringMQTopicProduce {
    //JmsTemplate 和配置文件中JmsTemplate 對應
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        SpringMQTopicProduce produce=(SpringMQTopicProduce)applicationContext.getBean("springMQTopicProduce");
        //傳統寫法
        /*produce.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage("*****spring整合activemq的demo*****");
                return textMessage;
            }
        });*/
        //lamda 表達式寫法
        produce.jmsTemplate.send((session -> {
            TextMessage textMessage = session.createTextMessage("*****spring整合activemq的demo111*****");
            return textMessage;
        }));
        System.out.println("發送完畢");
    }

}

5、消息消費者

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

/**
 * @ProjectName: springActiveMQ
 * @Package: cn.**.spring
 * @Author: huat
 * @Date: 2020/1/21 9:30
 * @Version: 1.0
 * 主題消費者
 */
@Service
public class SpringMQTopicConsumer {
    //JmsTemplate 和配置文件中JmsTemplate 對應
    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
        SpringMQTopicConsumer springMQTopicConsumer=(SpringMQTopicConsumer)applicationContext.getBean("springMQTopicConsumer");
        String result=(String)springMQTopicConsumer.jmsTemplate.receiveAndConvert();
        System.out.println(result);

    }

}

注意:這裏不用啓動消費者,僅運行消息生產者就能夠獲取到消息java

相關文章
相關標籤/搜索