ActiveMQ之SpringBoot整合

queue生产者

源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_produce

  1. 新建项目
  2. 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.1.5.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.at.boot.activemq</groupId>
   <artifactId>boot_mq_produce</artifactId>
   <version>1.0-SNAPSHOT</version>
   <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      <maven.compiler.source>1.8</maven.compiler.source>
      <maven.compiler.target>1.8</maven.compiler.target>
   </properties>
   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter</artifactId>
      </dependency>
      <!--spring boot整合activemq的jar包-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-activemq</artifactId>
         <version>2.1.5.RELEASE</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>
</project>
  1. application.yml
#web占用的端口
server:
  port: 7777

spring:
  activemq:
    #activemq的broker的url
    broker-url: tcp://192.168.17.3:61616
    #连接activemq的broker所需的账号和密码
    user: admin
    password: admin
  jms:
    #目的地是queue还是topic,false(默认) = queue  true =  topic
    pub-sub-domain: false

#自定义队列名称。这只是个常量
myqueue: boot-activemq-queue
  1. 配置目的地的bean
/*
 * 配置目的地的bean和开启springboot的jms功能
 */
package com.at.boot.activemq.config;

import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jms.annotation.EnableJms;
import org.springframework.stereotype.Component;

//让spring管理的注解,相当于spring中在xml 中写了个bean
@Component
//开启jms适配
@EnableJms
public class ConfigBean {

        //注入配置文件中的myqueue
        @Value("${myqueue}")
        private String myQueue ;

        @Bean   //bean id="" class=""
        public ActiveMQQueue queue(){
             return  new ActiveMQQueue(myQueue);
        }
}
  1. 队列生产者代码
/*
 * 队列生产者代码
 * 发送消息
 */
package com.at.boot.activemq.produce;

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 Queue_Produce {

    //JMS模板
    @Autowired
    private JmsMessagingTemplate  jmsMessagingTemplate ;

    //这个是我们配置的队列目的地
    @Autowired
    private Queue queue ;

    //发送消息
    public void produceMessage(){
        //一参是目的地,二参是消息的内容
        jmsMessagingTemplate.convertAndSend(queue,"****"+ UUID.randomUUID().toString().substring(0,6));
    }

    //定时任务,每3秒执行一次。非必须代码,仅为演示。
    @Scheduled(fixedDelay = 3000)
    public void produceMessageScheduled(){
        produceMessage();
    }
}
  1. 主启动类(非必须)
package com.at.boot.activemq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
//是否开启定时任务调度功能
@EnableScheduling
public class MainApp_Produce {
    public static void main(String[] args) {
        SpringApplication.run(MainApp_Produce.class,args);
    }
}
  1. 单元测试(非必须)
package com.at.boot.activemq;

import com.at.boot.activemq.produce.Queue_Produce;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import javax.annotation.Resource;

//加载主类
@SpringBootTest(classes = MainApp_Produce.class)
//加载spring的junit
@RunWith(SpringJUnit4ClassRunner.class)
//加载web
@WebAppConfiguration
public class TestActiveMQ {

    @Resource    //这个是java的注解,而Autowried是spring的
    private Queue_Produce  queue_produce ;

	//这个是java的注解,而Autowried是spring的
    @Test
    public  void testSend() throws Exception{
        queue_produce.produceMessage();
    }
}

queue消费者

源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_consummer

pom.xml和application.yml文件和前面一样。唯一不同就是下面代码:

/*
 * 注册一个消息监听器
 * 项目开启后监听某个主题的消息
 */
package com.at.boot.activemq.consummer;

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

@Component
public class Queue_consummer {

    //注册一个监听器,destination指定监听的主题。
    @JmsListener(destination = "${myqueue}")
    public void receive(TextMessage textMessage) throws  Exception{
        System.out.println(" ***  消费者收到消息  ***"+textMessage.getText());
    }
}

topic生产者

源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_topic_produce

  1. pom.xml(同上)
  2. application.yml
server:
  port: 6666
spring:
  activemq:
    broker-url: tcp://192.168.17.3:61616
    user: admin
    password: admin
  jms:
    #目的地是queue还是topic,false(默认) = queue  true =  topic
    pub-sub-domain: true

 #自定义主题名称
mytopic: boot-activemq-topic
  1. 配置目的地的bean和开启JMS功能
package com.at.boot.activemq.topic.config;

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.Topic;

@Component
@EnableJms
public class ConfigBean {

    @Value("${mytopic}")
    private String  topicName ;

    @Bean
    public Topic topic() {
        return new ActiveMQTopic(topicName);
    }
}
  1. 生产者代码
package com.at.boot.activemq.topic.produce;

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.Topic;
import java.util.UUID;

@Component
public class Topic_Produce {

    @Autowired
    private JmsMessagingTemplate  jmsMessagingTemplate ;

    @Autowired
    private Topic  topic ;

    @Scheduled(fixedDelay = 3000)
    public void produceTopic(){
        jmsMessagingTemplate.convertAndSend(topic,"主题消息"+ UUID.randomUUID().toString().substring(0,6));
    }
}

topic消费者

源码参考:https://github.com/elstic/ActiveMQ/tree/master/boot_mq_topic_produce

  1. pom.xml(同上)
  2. application.yml(同上)
  3. 消费者代码
package com.at.boot.activemq.consummer;

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

@Component
public class Topic_Consummer {

    @JmsListener(destination = "${mytopic}")
    public void receive(TextMessage textMessage) throws  Exception{
        System.out.println("消费者受到订阅的主题:"+textMessage.getText());
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要将ActiveMQSpring Boot整合,需要进行以下步骤: 1. 首先,在项目的pom.xml文件中添加ActiveMQSpring Boot整合依赖。引用 2. 安装并启动ActiveMQ。在本地下载好ActiveMQ,并进入其bin目录,执行./activemq start命令来启动ActiveMQ。确保本地已经安装了可用的JRE环境。 3. 访问ActiveMQ的管理端界面来验证ActiveMQ是否正常工作。在浏览器中输入localhost:8161,初始的用户名和密码都为admin。通过管理端界面,你可以创建队列、管理消息等操作。 4. 在Spring Boot项目中实现消息队列的生产和消费功能。你可以使用Spring Boot提供的注解和配置来实现ActiveMQ的集成。具体的实现细节可以参考Spring Boot官方文档或者其他相关教程。 通过以上步骤,你就可以成功地将ActiveMQSpring Boot进行整合,实现消息队列的生产和消费功能了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringBoot整合ActiveMq](https://blog.csdn.net/qq_35220073/article/details/131139177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [SpringBoot 整合实现ActiveMQ](https://blog.csdn.net/qq_26383975/article/details/124943780)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值