• springboot整合ActiveMQ


    一:安装ActiveMQ

    官网下载,解压

    二:创建springboot项目,引入依赖   

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

    <!--消息队列连接池-->
    <dependency>
    <groupId>org.apache.activemq</groupId>
    <artifactId>activemq-pool</artifactId>
    <version>5.15.0</version>
    </dependency>

    三:yml配置

    spring:
    activemq:
    broker-url: tcp://localhost:61616
    in-memory: true
    pool:
    enabled: false

    四:生产者发送消息

    package com.lv.learning.jms;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Service;

    import javax.jms.Destination;

    /**
    * Producer
    *
    * @author lvjijun
    * @version 1.0
    * @Date 2020-10-19
    */
    @Service("producer")
    public class Producer {
    /**
    * 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    */
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /***
    * description: 发送消息,destination是发送到的队列,message是待发送的消息
    * @param destination
    * @param message
    * @return void
    * @author lvjijun
    * @time 2020-10-19 10:52
    */
    public void sendMessage(Destination destination, final String message) {
    jmsMessagingTemplate.convertAndSend(destination, message);
    }
    }


    五:消费者接收消息

    package com.lv.learning.jms;

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

    /**
    * Consumer
    *
    * @author lvjijun
    * @version 1.0
    * @Date 2020-10-19
    */
    @Component
    public class Consumer {

    /***
    * description: 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
    * @param text
    * @return
    * @author lvjijun
    * @time 2020-10-19 10:53
    */
    @JmsListener(destination = "mytest.queue")
    public void receiveQueue(String text) {
    System.out.println("Consumer收到的报文为:"+text);
    }

    }


    六:消费者接收并发送消息

    package com.lv.learning.jms;

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

    /**
    * Consumer2
    *
    * @author lvjijun
    * @version 1.0
    * @Date 2020-10-19
    */
    @Component
    public class Consumer2 {

    @JmsListener(destination = "mytest.queue")
    @SendTo("out.queue")
    public String receiveQueue(String text) {
    System.out.println("Consumer2收到的报文为:"+text);
    return "return message"+text;
    }

    }

    在receiveQueue方法上面加上注解@SendTo("out.queue"),意思是将return回的值,再发送的"out.queue"队列中

    七:生产者发送并接收消息

    package com.lv.learning.jms;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.annotation.JmsListener;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.stereotype.Service;

    import javax.jms.Destination;

    /**
    * Producer
    *
    * @author lvjijun
    * @version 1.0
    * @Date 2020-10-19
    */
    @Service("producer")
    public class Producer {
    /**
    * 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装
    */
    @Autowired
    private JmsMessagingTemplate jmsMessagingTemplate;

    /***
    * description: 发送消息,destination是发送到的队列,message是待发送的消息
    * @param destination
    * @param message
    * @return void
    * @author lvjijun
    * @time 2020-10-19 10:52
    */
    public void sendMessage(Destination destination, final String message) {
    jmsMessagingTemplate.convertAndSend(destination, message);
    }

    /***
    * description: 监听队列接收消息
    * @param text
    * @return void
    * @author lvjijun
    * @time 2020-10-19 16:06
    */
    @JmsListener(destination = "out.queue")
    public void consumerMessage(String text) {
    System.out.println("从out.queue队列收到的回复报文为:" + text);
    }

    }


  • 相关阅读:
    CF1464C Poman Numbers
    CF1458F Range Diameter Sum
    Codeforces Round #691 (Div. 1) A~F题解&总结
    CF1458E Nim Shortcuts
    python读取当前以pdf结尾的文件并保存到excel文件里面
    vmware迁移到华三(H3C CAS 虚拟化平台)云平台
    vue+axois 封装请求+拦截器(请求锁+统一处理错误码)
    除了MD5加密,试一下签名吧
    C#---? ?? ?. ?: 好几种问号符号的使用方法
    C#---值类型为什么不能为空null
  • 原文地址:https://www.cnblogs.com/lvjijun/p/13840775.html
Copyright © 2020-2023  润新知