SpringBoot整合ActiveMQ

队列模式

application.properties配置如下:

#连接地址
spring.activemq.broker-url=tcp://127.0.0.1:61616
#如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true
spring.jms.pub-sub-domain=false

ActivemqConfig.java配置:

/**
 * 点对点
 */
@Bean
public Queue queue() {
    return new ActiveMQQueue("active.queue");
}

消息生产者SendController.java发送代码如下:

/*
 * 发送 队列消息
 */
@RequestMapping("/sendQueue")
public String sendQueue() {
    String message = UUID.randomUUID().toString();
    // 指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.queue, message);
    return "消息发送成功!message=" + message;
}

消息消费者QueueCustomerController.java发送代码如下:

@RestController
public class QueueCustomerController {
/*
 * 监听和接收  队列消息
 */
@JmsListener(destination="active.queue")
public void readActiveQueue(String message) {
    System.out.println("接受到:" + message);
}
}

主题模式

application.properties配置如下:

spring.jms.pub-sub-domain=true

ActivemqConfig.java配置:

/**
 * 发布/订阅
 */
@Bean
public Topic topic() {
    return new ActiveMQTopic("active.topic");
}

消息生产者SendController.java发送代码如下:

/*
 * 发送 主题消息
 */
@RequestMapping("/sendTopic")
public String sendTopic() {
    String message = UUID.randomUUID().toString();
    // 指定消息发送的目的地及内容
    this.jmsMessagingTemplate.convertAndSend(this.topic, message);
    return "消息发送成功!message=" + message;

添加两个消息消费者,TopicCustomerController.java代码如下:

/*
 * 监听和接收  主题消息1
 */
@JmsListener(destination = "active.topic")
public void readActiveTopic1(String message) {
    System.out.println("Customer1接受到:" + message);
}

/*
 * 监听和接收  主题消息2
 */
@JmsListener(destination = "active.topic")
public void readActiveTopic2(String message) {
    System.out.println("Customer2接受到:" + message);
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值