ActiveMQ 7.SpringBoot整合ActiveMQ

本文详细介绍如何在SpringBoot项目中整合ActiveMQ,包括配置、生产者与消费者的实现,以及如何通过注解实现定时消息发送。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ActiveMQ

@Author:hanguixian

@Email:hn_hanguixian@163.com

七 SpringBoot整合ActiveMQ

1 代码

  • 项目结构

springboot整合ActiveMQ01.png

  • 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 https://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.2.1.RELEASE</version>
        <relativePath/>
    </parent>
    <groupId>com.hgx</groupId>
    <artifactId>springboot-activemq</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot-activemq</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
  • 配置:application.yml
server:
  port: 8888

spring:
  activemq:
    broker-url: tcp://106.14.217.80:61616
    user: admin
    password: admin
  jms:
    pub-sub-domain: true   # false=Queue true=Topic

#自定义队列名称
myQueue: boot-active-queue
#自定义主题名
myTopic: boot-active-Topic
  • 配置类:ConfigBean.java
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import javax.jms.Topic;
import javax.validation.Valid;

@Component
@EnableJms
public class ConfigBean {

    @Value("${myQueue}")
    private String myQueue ;

    @Value("${myTopic}")
    private String myTopic;

    @Bean
    public Queue queue(){
        return new ActiveMQQueue(myQueue) ;
    }

    @Bean
    public Topic topic(){
        return new ActiveMQTopic(myTopic) ;
    }
}
  • springboot启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class SpringbootActivemqApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootActivemqApplication.class, args);
    }

}
  • 生产者-队列:QueueProduce.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import java.util.UUID;

@Component
public class QueueProduce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Queue queue;

    public void produceMsg() {
        jmsMessagingTemplate.convertAndSend(queue, "kkkkk" + UUID.randomUUID().toString());
    }

    /**
     * 3秒一次
     */
//    @Scheduled(fixedDelay = 3000)
    public void produceMSgScheduled() {

        jmsMessagingTemplate.convertAndSend(queue, "produceMSgScheduled" + UUID.randomUUID().toString());

        System.out.println("produceMSgScheduled.................");

    }

}
  • 消费者-队列:QueueConsumer.java
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@Component
public class QueueConsumer {

    @JmsListener(destination = "${myQueue}")
    public void receive(TextMessage textMessage) throws JMSException {
        System.out.println(".....消费者收到消息:" + textMessage.getText());
    }
}

  • 生产者-主题:TopicProduce.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.jms.Queue;
import javax.jms.Topic;
import java.util.UUID;

@Component
public class TopicProduce {

    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private Topic topic;

    public void produceMsg() {
        jmsMessagingTemplate.convertAndSend(topic, "topic" + UUID.randomUUID().toString());
    }

    /**
     * 3秒一次
     */
   // @Scheduled(fixedDelay = 3000)
    public void produceMSgScheduled() {

        jmsMessagingTemplate.convertAndSend(topic, "produceMSgScheduled topic" + UUID.randomUUID().toString());

        System.out.println("produceMSgScheduled topic.................");

    }

}
  • 消费者-主题:TopicConsumer.java
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

@Component
public class TopicConsumer {

    @JmsListener(destination = "${myTopic}")
    public void receive(TextMessage textMessage) throws JMSException {
        System.out.println(".....消费者收到消息:" + textMessage.getText());
    }

}
  • 测试类:
import com.hgx.activemq.produce.QueueProduce;
import com.hgx.activemq.produce.TopicProduce;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

@SpringBootTest(classes = SpringbootActivemqApplication.class)
@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
class SpringbootActivemqApplicationTests {

    @Autowired
    private QueueProduce queueProduce ;
    @Autowired
    private TopicProduce topicProduce ;

    @Test
    public void testSend(){
        queueProduce.produceMsg();
    }

    @Test
    public void testSendSch(){
        queueProduce.produceMSgScheduled();
    }

    @Test
    public void testTopicSend(){
        topicProduce.produceMsg();
    }

    @Test
    public void testTopicSendSch(){
        topicProduce.produceMSgScheduled();
    }
}

  • 说明:测试主题,修改配置jms.pub-sub-domain:true;测试队列,修改jms.pub-sub-domain:false。如果是使用间隔投递,方法上使用@Scheduled(fixedDelay = xxx)注解,不需要启动测试类,启动springboot启动类即可看到效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值