springboot~rabbitmq的队列初始化和绑定

配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系

  1. 代码里初始化exchange
  2. 代码里初始化queue
  3. 代码里绑定exchange,queue和routekey
  4. 配置文件,直接声明vhost

代码里初始化exchange

   /**
   * rabbitMq里初始化exchange.
   *
   * @return
   */
  @Bean
  public TopicExchange crmExchange() {
    return new TopicExchange(EXCHANGE);
  }

代码里初始化queue

  /**
   * rabbitMq里初始化队列crm.hello.
   *
   * @return
   */
  @Bean
  public Queue helloQueue() {
    return new Queue(HELLO);
  }

代码里绑定exchange,queue和routekey

  /**
   * 绑定exchange & queue & routekey.
   *
   * @param queueMessage 队列
   * @param exchange     交换机
   * @param routekey     路由
   * @return
   */
  public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
    return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
  }

配置文件

spring:
    rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: lind

完整代码

package com.lind.microservice.productCenter.mq;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * amqp配置.
 */
@Configuration
public class AmqpConfig {

  /**
   * 交换机.
   */
  public final static String EXCHANGE = "crm";
  /**
   * hello队列.
   */
  public final static String HELLO = "crm.hello";
  /**
   * 建立订单队列.
   */
  public final static String LIND_GENERATE_ORDER = "crm.generate.order";

  /**
   * 绑定exchange & queue & routekey.
   *
   * @param queueMessage 队列
   * @param exchange     交换机
   * @param routekey     路由
   * @return
   */
  public Binding bindingExchange(Queue queueMessage, TopicExchange exchange, String routekey) {
    return BindingBuilder.bind(queueMessage).to(exchange).with(routekey);
  }

  /**
   * rabbitMq里初始化exchange.
   *
   * @return
   */
  @Bean
  public TopicExchange crmExchange() {
    return new TopicExchange(EXCHANGE);
  }

  /**
   * rabbitMq里初始化队列crm.hello.
   *
   * @return
   */
  @Bean
  public Queue helloQueue() {
    return new Queue(HELLO);
  }

  /**
   * rabbitMq里初始化队列crm.generate.order.
   *
   * @return
   */
  @Bean
  public Queue orderQueue() {
    return new Queue(LIND_GENERATE_ORDER);
  }

}

队列发布者

package com.lind.microservice.productCenter.mq;

import java.util.Date;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloPublisher {
  @Autowired
  AmqpTemplate rabbitTemplate;
  @Autowired
  AmqpConfig amqpConfig;

  public void hello() {
    String context = "hello " + new Date();
    System.out.println("HelloPublisher : " + context);
    amqpConfig.bindingExchange(
        amqpConfig.helloQueue(),
        amqpConfig.crmExchange(),
        "crm.hello.#"
    );
    this.rabbitTemplate.convertAndSend(AmqpConfig.EXCHANGE, AmqpConfig.HELLO, context);
  }

}

队列订阅者

package com.lind.microservice.productCenter.mq;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = AmqpConfig.HELLO)
public class HelloSubscriber {
  @RabbitHandler
  public void process(String hello) {
    System.out.println("HelloSubscriber  : " + hello);
  }

}

原文地址:https://www.cnblogs.com/lori/p/9739130.html

时间: 2024-08-01 09:45:00

springboot~rabbitmq的队列初始化和绑定的相关文章

SpringBoot RabbitMQ 延迟队列代码实现

场景 用户下单后,如果30min未支付,则删除该订单,这时候就要可以用延迟队列 准备 利用rabbitmq_delayed_message_exchange插件: 首先下载该插件:https://www.rabbitmq.com/community-plugins.html 然后把该插件放到rabbitmq安装目录plugins下: 进入到sbin目录下,执行"rabbitmq-plugins.bat enable rabbitmq_delayed_message_exchange";

SpringBoot:初探 RabbitMQ 消息队列

SpringBoot?是为了简化?Spring?应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 MQ全称(Message Queue)又名消息队列,是一种异步通讯的中间件.可以将它理解成邮局,发送者将消息传递到邮局,然后由邮局帮我们发送给具体的消息接收者(消费者),具体发送过程与时间我们无需关心,它也不会干扰我进行其它事情.常见的MQ有kafka.acti

SpringBoot:RabbitMQ 延迟队列

SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖就可以轻易的搭建出一个 WEB 工程 初探RabbitMQ消息队列中介绍了RabbitMQ的简单用法,顺带提及了下延迟队列的作用.所谓延时消息就是指当消息被发送以后,并不想让消费者立即拿到消息,而是等待指定时间后,消费者才拿到这个消息进行消费. 延迟队列 延迟队列能做什么? 订单业务: 在电商/点餐

springboot+rabbitmq整合示例程

关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbitmq 二.引入springboot和rabbitmq的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quo

springboot RabbitMQ 配置

引用自 http://www.cnblogs.com/ityouknow/p/6120544.html 自己留一份 记录一下 RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲,消息分发的作用. 消息中间件在互联网公司的使用中越来越多,刚才还看到新闻阿里将RocketMQ捐献给了apache,当然了今天的主角还是讲RabbitMQ.消息中间件最主要的作用是解耦,中间件最标准的用法是生产者生产消息传送到队列,消费者从队列中拿取消息并处理,生产者不用关心是谁来

springBoot+RabbitMQ例子

demo目录 贴代码 1.ProducerConfig.java package com.test.config; import org.springframework.amqp.core.BindingBuilder; import org.springframework.amqp.core.Queue; import org.springframework.amqp.core.TopicExchange; import org.springframework.amqp.rabbit.conn

带着新人学springboot的应用07(springboot+RabbitMQ 下)

说一两句废话,强烈推荐各位小伙伴空闲时候也可以写写自己的博客!不管水平高低,不管写的怎么样,不要觉得写不好或者水平不够就不写了(咳,我以前就是这样的想法...自我反省!). 但是开始写博客之后,你会发现很多你以为自己会的东西其实你并不会,然后你会经常在头脑中不断的搜索有关的片段,或者去别的大神博客里到处找有关的资料,最后领悟了属于自己的东西!然后再写出来和别人分享,别人也会给你点意见,你也会慢慢的改进.这不就是学习+复习+巩固+创新+分享+改进的这么的一个过程吗? 以前看过曹雪芹的红楼梦,让我印

(转)RabbitMQ消息队列(六):使用主题进行消息分发

在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity的log.但是,这也是它之所以叫做简单日志系统的原因,因为是仅仅能够通过severity设定.不支持更多的标准. 比如syslog unix的日志工具,它可以通过severity (info/warn/crit...) 和模块(auth/cron/kern...).这可能更是我们想要的:我们可以仅仅需要cron模块的log. 为了实现类似的功能,我们需

RabbitMQ消息队列(六):使用主题进行消息分发

在上篇文章RabbitMQ消息队列(五):Routing 消息路由 中,我们实现了一个简单的日志系统.Consumer可以监听不同severity的log.但是,这也是它之所以叫做简单日志系统的原因,因为是仅仅能够通过severity设定.不支持更多的标准. 比如syslog unix的日志工具,它可以通过severity (info/warn/crit...) 和模块(auth/cron/kern...).这可能更是我们想要的:我们可以仅仅需要cron模块的log. 为了实现类似的功能,我们需