• activeMQ(二、与springboot集成)


    POOM文件

    首先导入activemq所需的jar包

    <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-activemq</artifactId>
            </dependency>
        </dependencies>

    生产者

    application.yml

    server:
      port: 8090
      servlet:
        context-path: /activemqProducer
    spring:
      activemq:
        broker-url: tcp://127.0.0.1:61616
        user: 123
        password: 123

    ActiveMqConfig

    package com.example.activemqtest.config;
    
    import org.apache.activemq.command.ActiveMQQueue;
    import org.apache.activemq.command.ActiveMQTopic;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import javax.jms.Queue;
    import javax.jms.Topic;
    
    @Configuration
    public class ActiveMQConfig {
    
        @Bean
        public Queue queue(){
            return new ActiveMQQueue("queue2");
        }
    
        @Bean
        public Topic topic(){
            return new ActiveMQTopic("topic2");
        }
    
    }

    配置生产者要发送消息的队列或者主题,注入spring中

    ProducterController

    package com.example.activemqtest.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jms.core.JmsMessagingTemplate;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.jms.Queue;
    import javax.jms.Topic;
    
    
    @RestController
    public class ProducterController {
    
        @Autowired
        private JmsMessagingTemplate jmsMessagingTemplate;
    
        @Autowired
        private Queue queue;
    
        @Autowired
        private Topic topic;
    
    
        @GetMapping("/sendMsgByQueue")
        public String sendMsgByQueue(String msg){
            jmsMessagingTemplate.convertAndSend(queue,msg);
            return msg;
        }
    
        @GetMapping("/sendMsgByTopic")
        public String sendMsgByTopic(String msg){
            jmsMessagingTemplate.convertAndSend(topic,msg);
            return msg;
        }
    }

    消费者

    @JmsListener(destination = "queue2")
        //方法返回发送给queue3
        @SendTo("queue3")
        public String handleMessage(String msg){
            System.out.println("接收到的返回是:" + msg);
            return "返回:"+ msg;
        }

    以上是消费者接收队列queue2的方式

    若要接收topic,需要增加配置

    server:
      port: 8091
      servlet:
        context-path: /activemqConsumer
    spring:
      activemq:
        broker-url: tcp://127.0.0.1:61616
        user: 123
        password: 123
      jms:
        pub-sub-domain: true
    @JmsListener(destination = "topic2")
        public void topicConsumer1(String msg){
            System.out.println("消费者1接收为:"+msg);
        }
    
        @JmsListener(destination = "topic2")
        public void topicConsumer2(String msg){
            System.out.println("消费者2接收为:"+msg);
        }
  • 相关阅读:
    为App签名(为apk签名)
    Android如何获取网络连接状态(3G/Wifi)及怎样调用网络配置界面
    android textview改变部分文字的颜色和string.xml中文字的替换及部分内容设置颜色、字体、超链接、图片
    Toast和Looper。Handler消息循环机制
    android 创建DateTime类型的数据库
    Android中dp和px之间进行转换
    Java的ThreadPoolExecutor使用几点建议
    ListView中设置item点击状态的背景色
    android 让一个控件按钮居于底部的几种方法
    sqlite3数据类型和函数
  • 原文地址:https://www.cnblogs.com/Unlimited-Blade-Works/p/11315904.html
Copyright © 2020-2023  润新知