zoukankan      html  css  js  c++  java
  • SpringBoot整合ActiveMQ

    一、创建项目并导入依赖

       

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-activemq</artifactId>

    </dependency>

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-web</artifactId>

    </dependency>

       

    二、相关配置和代码

       

    2.1)application.properties

       

    spring.activemq.broker-url=tcp://192.168.21.136:61616

    #发消息的时候可以是字符串也可以是对象,如果是对象就需要设置为true

    spring.activemq.packages.trust-all=true

    spring.activemq.user=admin

    spring.activemq.password=admin

       

    2.2)创建ActiveMQConfig配置

       

    @Component

    public class ActiveMQConfig{

    @Bean

    Queuequeue(){

    //消息服务的名字|方便根据名字接受

    return new ActiveMQQueue("hello.fernfei");

    }

    }

       

    2.3)创建JmsComponet用于收发消息

    注:我这里为了方便收发写在一起,真实业务收发各一个项目

       

    @Component

    public class JmsComponent{

    @Autowired

    JmsMessagingTemplate jmsMessagingTemplate;//SpringBoot提供的操作activemq模板

    @Autowired

    Queuequeue;

       

    public void send(Message msg){

    //第一个参数是目标,第二个是信息

    jmsMessagingTemplate.convertAndSend(queue,msg);

       

    }

    //根据这个目标去监听

    @JmsListener(destination="hello.fernfei")

    public void receive(Messagemsg){

    System.out.println(msg);

    }

    }

       

    2.4)bean Message存储信息的类

       

       

    2.5)测试

       

       

  • 相关阅读:
    python3学习之匿名函数
    python3学习之装饰器
    Linux服务器管理神器-IPython
    Linux 安装python3.4
    Linux一些常用操作命令
    Java并发知识分享
    LINUX 学习笔记 账号与群组的管理
    用JAVA写查询一个字符串中是否包含另外一个字符串以及出现的次数
    jQuery性能优化
    jQuery实用工具函数
  • 原文地址:https://www.cnblogs.com/fernfei/p/12210926.html
Copyright © 2011-2022 走看看