SpringBoot+ActiveMQ(整合) demo

1、导入所需jar包 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>org.example</groupId>
    <artifactId>java-activemq-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <!--spring-test测试=-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring-JMS插件相关jar包依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jms</artifactId>
            <version>4.3.16.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <!--activemq依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <!--消息队列连接池-->
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

2、创建消息队列配置类ActiveMqConfig

package com.activemqhq.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;
import org.springframework.stereotype.Component;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;


/**
 * @author author
 * @version 1.0
 * @description: 消息队列配置类
 * @date 2022/04/18 14:26
 */
@Configuration
@Component
public class ActiveMqConfig {
    //===================================队列模式=============================
    /**
     * 1、队列模式
     * @return
     */
    @Bean
    public Queue queue() {
        return new ActiveMQQueue("demo.queue");
    }
    //===================================订阅模式=============================
    /**
     * 2、订阅模式
     * @return
     */
    @Bean
    public Topic topic() {
        return new ActiveMQTopic("demo.topic");
    }

    //===================================同时支持两种形式=============================
    /**
     * 3、同时支持队列、订阅模式:队列
     * @param connectionFactory
     * @return
     */
    @Bean("queueListenerFactory")
    public JmsListenerContainerFactory<?> queueListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setPubSubDomain(false);
        return factory;
    }
    /**
     * 3、同时支持队列、订阅模式:订阅
     * @param connectionFactory
     * @return
     */
    @Bean("topicListenerFactory")
    public JmsListenerContainerFactory<?> topicListenerFactory(ConnectionFactory connectionFactory) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        factory.setPubSubDomain(true);
        return factory;
    }
}

3、创建消息队列消费者Consumer

package com.activemqhq.business;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

/**
 * @author author 
 * @version 1.0
 * @description: 消息队列消费者
 * @date 2022/04/18 14:36
 */
@Component
public class Consumer {
    //===================================队列模式=============================
    /**
     * 队列模式消费者
     * @param text
     */
    @JmsListener(destination = "demo.queue")
    public void receiveMsg(String text) {
        System.out.println("queue接收到消息 : "+text);
    }
    //===================================订阅模式=============================
    /**
     * 订阅模式消费者1
     * @param text
     */
    /*@JmsListener(destination = "demo.topic")
    public void receiveTopic1(String text) {
        System.out.println("receiveTopic1接收到Topic消息 : " + text);
    }*/
    /**
     * 订阅模式消费者2
     * @param text
     */
    /*@JmsListener(destination = "demo.topic")
    public void receiveTopic2(String text) {
        System.out.println("receiveTopic2接收到Topic消息 : " + text);
    }*/

    //===================================同时支持两种形式=============================

    @JmsListener(destination = "demo.queue", containerFactory = "queueListenerFactory")
    public void receiveMsgFactory(String text) {
        System.out.println("receiveMsgFactory接收到消息 : " + text);
    }

    @JmsListener(destination = "demo.topic", containerFactory = "topicListenerFactory")
    public void receiveTopicFactory1(String text) {
        System.out.println("receiveTopicFactory1接收到Topic消息 : " + text);
    }
}

4、创建消息队列生产者Producer

package com.activemqhq.business;

import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * @author author 
 * @version 1.0
 * @description: 消息队列生产者
 * @date 2022/04/18 14:36
 */
@Component
public class Producer {
    @Resource
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Resource
    private Queue queue;
    @Resource
    private Topic topic;
    /**
     * 队列模式:发送消息到activemq
     * @param msg
     */
    public void sendQueue(String msg) {
        System.out.println("发送Queue消息内容 :" + msg);
        this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }

    /**
     * 订阅模式:发送消息到activemq
     * @param msg
     */
    public void sendTopic(String msg) {
        System.out.println("发送Topic消息内容 :"+msg);
        this.jmsMessagingTemplate.convertAndSend(this.topic, msg);
    }
}

5、创建配置文件application.yml

spring:
  # 基于内存的ActiveMQ
  #  activemq:
  #    in-memory : true
  # 不使用连接池,如果使用连接池还需在pom中添加activemq-pool的依赖
  #    pool:
  #      enabled : false
  # 独立安装的ActiveMQ
  activemq:
    broker-url : tcp://127.0.0.1:61616
    user: admin
    password: admin
    pool:
      max-connections: 10
      idle-timeout: 30000
      enabled : true
    packages:
      trust-all: true
  #activemq广播模式,此时队列模式不可正常工作
  jms:
    pub-sub-domain: true

6、数据测试类

package com.activemqhq.test;

import com.activemqhq.ActiveMqApplication;
import com.activemqhq.business.Producer;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

/**
 * @author author 
 * @version 1.0
 * @description: 数据测试类
 * @date 2021/04/18 14:56
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ActiveMqApplication.class)
public class ActiveMqTest {
    @Resource
    private Producer producer;

    @Test
    public void sendQueue(){
        String msg = "hello Queue!";
        producer.sendQueue(msg);
    }

    @Test
    public void sendTopic(){
        String msg = "hello Topic!";
        producer.sendTopic(msg);
    }

    @Test
    public void sendMsg(){
        String msg = "hello Queue and Topic!";
        producer.sendQueue(msg);
        producer.sendTopic(msg);
    }
}

注意…

在这里插入图片描述
产生以上异常信息需修改broker-url地址 改成自己的测试地址即可
在这里插入图片描述
个人拙见仅供参考!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值