栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot整合activemq

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot整合activemq

目录

步骤

1 加坐标 

2 配置

3 activemq使用

业务层和表现层完整代码

测试

4 设置消息存放位置名

5 消息监听

 6 消费后再发送到别的队列

 7 使用发布订阅模型

 总结


​​​​​​​关于activemq的安装在这里

步骤


1 加坐标 
		
			org.springframework.boot
			spring-boot-starter-activemq
		


2 配置
spring:
  activemq:
#    tcp协议 activemq服务端口:61616
    broker-url: tcp://localhost:61616
#    默认保存位置
  jms:
    template:
      default-destination: qing


3 activemq使用
package com.qing.service.impl;

import com.qing.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;

@Service
public class MessageServiceActivemqImpl implements MessageService {

    //使用JMS技术
    @Autowired
    private JmsMessagingTemplate messagingTemplate;

    //使消息进入消息队列
    @Override
    public void sendMessage(String id) {
        System.out.println(id + "的message已加入到消息队列");
        //类型转换and send
        messagingTemplate.convertAndSend(id);


    }

    @Override
    public String doMessage() {
        //先接收再类型转换
        //做完了就remove出去了
        String id = messagingTemplate.receiveAndConvert(String.class);
        System.out.println("已完成短信发送业务" + id);
        return id;
    }
}

业务层和表现层完整代码
package com.qing.service.impl;

import com.qing.service.MessageService;
import com.qing.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private MessageService messageService;

    //发消息
    @Override
    public void order(String id) {
        System.out.println("订单处理开始");
        messageService.sendMessage(id);
        System.out.println("订单处理结束");
        System.out.println("-------------------");
    }
}

 OrderController 用来生产消息

package com.qing.controller;

import com.qing.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/orders")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("{id}")
    public void order(@PathVariable String id){

        //发消息
        orderService.order(id);
    }


}

MessageController用来消费消息
package com.qing.controller;

import com.qing.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/msgs")
public class MessageController {
    @Autowired
    private MessageService messageService;

    @GetMapping
    public String doMessage(){
        //处理消息
        String id = messageService.doMessage();
        return id;
    }
}

测试

 

 

 id 为7的消息也被消费了

 

4 设置消息存放位置名

设置上就不用配置里默认的名字了

5 消息监听

 监听到生产了消息,自动马上消费

package com.qing.service.impl;

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

@Component
public class ActivemqListener {

    //参数是消息放的位置
    @JmsListener(destination = "order.queueId")
    public void receive(String id) {
        System.out.println("自动消费" + id);

    }

}

 6 消费后再发送到别的队列
package com.qing.service.impl;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

@Component
public class ActivemqListener {

    //参数是消息放的位置
    @JmsListener(destination = "order.queueId")
    //将该方法的返回值 消费后 再发送到其他队列order.otherQueue
    @SendTo("order.otherQueue")
    public String receive(String id) {
        System.out.println("自动消费" + id);
        return "new" + id;

    }

}

 

 

 

 备注:以上使用的是点对点模型

点对点模型:队列的消息只能被1个消费者消费

 7 使用发布订阅模型

server:
  port: 80
spring:
  activemq:
#    tcp协议 activemq服务端口:61616
    broker-url: tcp://localhost:61616
#    默认保存位置
  jms:
    template:
      default-destination: qing
#    使用发布订阅模型 (不写下面的默认是点对点模型)
    pub-sub-domain: true

 点对点模型:队列的消息只能被1个消费者消费
 发布订阅模型:消息可以被多个消费者消费,生产者和消费者完全独立,不需要感知对方的存在

 测试前

 使用发布订阅模型后

发送5次消息后 (这里的数据不变)

 看这里

 总结

 生产消息用下面红框的

 消费消息用下面的

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002879.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号