SpringBoot整合ActiveMQ


SpringBoot版本号2.5.4

一、添加依赖

	<dependency>
		 <groupId>org.springframework.boot</groupId>
		 <artifactId>spring-boot-starter-activemq</artifactId>
	</dependency>
	<dependency>
	     <groupId>org.messaginghub</groupId>
		 <artifactId>pooled-jms</artifactId>
	</dependency>

二、配置application.yml文件

spring: 
  activemq: 
    broker-url: tcp://localhost:61316
    close-timeout: 5000
    in-memory: false
    user: admin
    password: admin
    queue-name: my-queue
    topic-name: my-topic
    pool: 
      enabled: true
      max-connections: 10

三、创建ActiveMQ配置类

代码如下(示例):

package com.springboot.activemq;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ProviderController {

    @Autowired
    private Destination          topic;
    @Autowired
    private Destination          queue;
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    @RequestMapping("/sendQueue")
    public void sendQueue(String msg) {
        System.out.println("发送队列消息");
        jmsMessagingTemplate.convertAndSend(queue, msg);
    }

    @RequestMapping("/sendTopic")
    public void sendTopic(String msg) {
        System.out.println("发送主题消息:");
        jmsMessagingTemplate.convertAndSend(topic, msg);
    }

}

四、创建生产者

package com.springboot.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListenerContainerFactory")
    public void receiveActiveQueue(String message) {
        System.out.println("接受到:" + message);
    }

    @JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "topicListenerContainerFactory")
    public void receiveTopic(String message) {
        System.out.println("接受到:" + message);
    }

    @JmsListener(destination = "${spring.activemq.topic-name}", containerFactory = "topicListenerContainerFactory")
    public void receiveTopic2(String message) {
        System.out.println("接受到:" + message);
    }

}

五、创建消费者

package com.springboot.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {

    @JmsListener(destination = "${spring.activemq.queue-name}", containerFactory = "queueListenerContainerFactory")
    //或  @JmsListener(destination = "my-queue", containerFactory = "queueListenerContainerFactory")
    public void reciveActiveQueue(String message) {
        System.out.println("接受到:" + message);
    }

    @JmsListener(destination = "my-topic", containerFactory = "topicListenerContainerFactory")
    public void receiveTopic(String message) {
        System.out.println("接受到:" + message);
    }

    @JmsListener(destination = "my-topic", containerFactory = "topicListenerContainerFactory")
    public void receiveTopic2(String message) {
        System.out.println("接受到:" + message);
    }

}

Queue模式测试:http://localhost:8888/sendQueue?msg=queueTest

在这里插入图片描述

Topic模式测试:http://localhost:8888/sendTopic?msg=topicTest

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值