83°

ActiveMQ学习之Spring整合ActiveMQ------>消息主题

一、pom

<dependencies>
    <!--activemq-->
    <dependency>
      <groupId>org.apache.activemq</groupId>
      <artifactId>activemq-all</artifactId>
      <version>5.15.9</version>
    </dependency>
&lt;!--activeMQ对jms的支持,整合spring和activemq--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-jms&lt;/artifactId&gt;
  &lt;version&gt;4.3.23.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--activeMQ所需要的pool--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.activemq&lt;/groupId&gt;
  &lt;artifactId&gt;activemq-pool&lt;/artifactId&gt;
  &lt;version&gt;5.15.9&lt;/version&gt;
&lt;/dependency&gt;

&lt;!--fastjson--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
  &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
  &lt;version&gt;2.9.9&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--javabean--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.xbean&lt;/groupId&gt;
  &lt;artifactId&gt;xbean-spring&lt;/artifactId&gt;
  &lt;version&gt;3.16&lt;/version&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
  &lt;version&gt;4.3.23.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
  &lt;version&gt;4.3.23.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--spring 切面--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-aop&lt;/artifactId&gt;
  &lt;version&gt;4.3.23.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--spring 对持久化框架支持--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.springframework&lt;/groupId&gt;
  &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
  &lt;version&gt;4.3.23.RELEASE&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--aop注解--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
  &lt;artifactId&gt;aspectjrt&lt;/artifactId&gt;
  &lt;version&gt;1.6.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;!--aop切入点--&gt;
&lt;dependency&gt;
  &lt;groupId&gt;org.aspectj&lt;/groupId&gt;
  &lt;artifactId&gt;aspectjweaver&lt;/artifactId&gt;
  &lt;version&gt;1.8.13&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
  &lt;groupId&gt;cglib&lt;/groupId&gt;
  &lt;artifactId&gt;cglib&lt;/artifactId&gt;
  &lt;version&gt;2.1_2&lt;/version&gt;
&lt;/dependency&gt;

</dependencies>

二、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.spring"></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>
&lt;!--spring提供的jms工具类,他可以进行消息发送、接受等--&gt;
&lt;bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"&gt;
    &lt;property name="connectionFactory" ref="jmsFactory"&gt;&lt;/property&gt;
    &lt;!--此处配置为队列--&gt;
   &lt;!-- &lt;property name="defaultDestination" ref="destinationQueue"&gt;&lt;/property&gt;--&gt;
    &lt;!--此处配置为主题--&gt;
     &lt;property name="defaultDestination" ref="destinationTopic"&gt;&lt;/property&gt;
    &lt;property name="messageConverter"&gt;
        &lt;bean class="org.springframework.jms.support.converter.SimpleMessageConverter"&gt;&lt;/bean&gt;
    &lt;/property&gt;
&lt;/bean&gt;

</beans>

三、生产者

package cn.**.spring;

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的demo"); return textMessage; })); System.out.println("发送完毕"); }

}

四、消费者

package cn.**.spring;

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);

    }

}

 

本文由【冥焱】发布于开源中国,原文链接:https://my.oschina.net/u/3535099/blog/3160485

全部评论: 0

    我有话说: