配置文件,在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);
} }

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

  1. springboot rabbitmq 死信队列应用场景和完整demo

    何为死信队列? 死信队列实际上就是,当我们的业务队列处理失败(比如抛异常并且达到了retry的上限),就会将消息重新投递到另一个Exchange(Dead Letter Exchanges),该Exc ...

  2. SpringBoot RabbitMQ 延迟队列代码实现

    场景 用户下单后,如果30min未支付,则删除该订单,这时候就要可以用延迟队列 准备 利用rabbitmq_delayed_message_exchange插件: 首先下载该插件:https://ww ...

  3. RabbitMQ镜像队列初始化连接时的“优化”

    之前发过一篇帖子应用.Net+Consul维护RabbitMq的高可用性,然后最近老大问我当初我这么搞是抽的什么想法- -然后顺便贴了两行C#代码: var factory = new Connect ...

  4. SpringBoot使用消息队列RabbitMQ

    RabbitMQ 即一个消息队列,主要是用来实现应用程序的异步和解耦,同时也能起到消息缓冲.消息分发的作用.RabbitMQ是实现AMQP(高级消息队列协议)的消息中间件的一种,AMQP,即Advan ...

  5. RabbitMQ镜像队列集群搭建、与SpringBoot整合

    镜像模式 集群模式非常经典的就是Mirror镜像模式,保证100%数据不丢失,在实际工作中也是用的最多的,并且实现集群比较的简单. Mirror镜像队列,目的是为了保证 RabbitMQ 数据的高可靠 ...

  6. springboot~rabbitmq自己通过UI手动发布队列需要注意的地方

    springboot里发布队列消息 为了兼容性和可读性更好,我们一般使用json字符串做为数据载体. public void decreaseCallMonitor(CallMonitorInfo c ...

  7. springboot+rabbitmq整合示例程

    关于什么是rabbitmq,请看另一篇文: http://www.cnblogs.com/boshen-hzb/p/6840064.html 一.新建maven工程:springboot-rabbit ...

  8. springboot + rabbitmq 整合示例

    几个概念说明:Broker:简单来说就是消息队列服务器实体.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列.Queue:消息队列载体,每个消息都会被投入到一个或多个队列.Bindi ...

  9. springboot+rabbitmq例子

    demo目录 贴代码 1.ProducerConfig.java package com.test.config; import org.springframework.amqp.core.Bindi ...

随机推荐

  1. profile.go

    )         }()     }     return &prof }

  2. stop_token.go

    package engine import (     "bufio"     "log"     "os" ) type StopToke ...

  3. disk.go

    package disk import "syscall" //空间使用结构体 type DiskStatus struct {     Size uint64     Used ...

  4. Hadoop问题:DataNode进程不见了

      DataNode进程不见了 问题描述 最近配置Hadoop的时候出现了这么一个现象,启动之后,使用jps命令之后是这样的: 看不到DataNode进程,但是能够正常的工作,是不是很神奇啊? 在一番 ...

  5. 【BZOJ 2850】巧克力王国

    复习了下KDtree,贴一下新板子233. #include "bits/stdc++.h" using namespace std; inline int read(){ ,k= ...

  6. 咸鱼Chen

    关于我 网名:咸鱼Chen 英文:nick chen 签名:I'm nothing but I must be everything. 标签:Python爱好(ma)者(nong),干过后端开发.算法 ...

  7. WebP 在减少图片体积和流量上的效果如何?MIP技术实践分享

    作者 | Jackson 编辑 | 尾尾 不论是 PC 还是移动端,图片一直占据着页面流量的大头,在图片的大小和质量之间如何权衡,成为了长期困扰开发者们的问题.而 WebP 技术的出现,为解决该问题提 ...

  8. 【STM32H7教程】第5章 STM32H7下载和调试方法(MDK5)

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第5章   STM32H7下载和调试方法(MDK5) 本 ...

  9. kolla 多节点部署 openstack

    kolla 介绍 简介 kolla 的使命是为 openstack 云平台提供生产级别的.开箱即用的交付能力.kolla 的基本思想是一切皆容器,将所有服务基于 Docker 运行,并且保证一个容器只 ...

  10. jdk源码阅读笔记-AbstractStringBuilder

    AbstractStringBuilder 在java.lang 包中,是一个抽象类,实现 Appendable 接口和 CharSequence 接口,这个类的诞生是为了解决 String 类在创建 ...