springboot整合activemq

ActiveMQ简介
1、ActiveMQ简介
Apache ActiveMQ是Apache软件基金会所研发的开放源代码消息中间件;由于ActiveMQ是一个纯Java程序,因此只需要操作系统支持Java虚拟机,ActiveMQ便可执行。

2、ActiveMQ下载
下载地址:http://activemq.apache.org/components/classic/download/
在这里插入图片描述

下载完成后解压双击activemq.bat文件打开(不用安装,直接使用),目录和打开后效果如下:
在这里插入图片描述

运行后,浏览器访问http://localhost:8161/地址进入一下界面。
在这里插入图片描述

点击Manage ActiveMQ broker登录到ActiveMQ管理页面,默认账号和密码都是admin。管理页面如下:

1、新建SpringBoot项目
新建Springboot项目,添加对应的依赖。项目完整的pom.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>


4.0.0

org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE


com.mcy
springboot-mq
0.0.1-SNAPSHOT
springboot-mq
Demo project for Spring Boot

org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-activemq org.springframework.boot spring-boot-starter-test test org.junit.vintage junit-vintage-engine org.springframework.boot spring-boot-maven-plugin

2、相关配置信息
在application.properties类中添加ActiveMQ相关的配置信息

server.port=8080
server.servlet.context-path=/mq
#MQ服务器地址
spring.activemq.broker-url=tcp://localhost:61616
#用户名
spring.activemq.user=admin
#密码
spring.activemq.password=admin
#设置是Queue队列还是Topic,false为Queue,true为Topic,默认false-Queue
spring.jms.pub-sub-domain=false
#spring.jms.pub-sub-domain=true
#变量,定义队列和topic的名称
myqueue: activemq-queue
mytopic: activemq-topic

3、ActiveMQ配置类
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.Topic;

/**

  1. MQ配置类
    */
    @Component
    @EnableJms
    public class ConfigBean {
    @Value(" m y q u e u e " ) p r i v a t e S t r i n g m y Q u e u e ; @ V a l u e ( " {myqueue}") private String myQueue; @Value(" myqueue")privateStringmyQueue;@Value("{mytopic}")
    private String topicName;

//队列
@Bean
public ActiveMQQueue queue(){
return new ActiveMQQueue(myQueue);
}

//topic
@Bean
public Topic topic(){
return new ActiveMQTopic(topicName);
}
}

、队列生产者
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.jms.Queue;

/*

  • 队列消息生产者
    */
    @RestController
    public class QueueProducerController {
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

@Autowired
private Queue queue;

/*

  • 消息生产者
    */
    @RequestMapping("/sendmsg")
    public void sendmsg(String msg) {
    System.out.println(“发送消息到队列:” + msg);
    // 指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.queue, msg);
    }
    }

队列消费者
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;

/*

  1. 队列queue消费者控制器
    /
    @RestController
    public class QueueConsumerController {
    /
  • 消费者接收消息
    */
    @JmsListener(destination="${myqueue}")
    public void readActiveQueue(String message) {
    System.out.println(“接受到:” + message);
    }
    }
    测试效果
    运行项目在浏览器中访问http://localhost:8080/mq/sendmsg?msg=123。向消息队列中发送123。控制台输出效果:

在这里插入图片描述