springboot实现rabbitmq的队列初始化和绑定

所属分类: 软件编程 / java 阅读数: 23
收藏 0 赞 0 分享

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

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

更多精彩内容其他人还在看

Spring Boot 配置 IDEA和DevTools 热部署的方法

这篇文章主要介绍了Spring Boot 配置 IDEA和DevTools 热部署的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot使用Redis缓存的实现方法

这篇文章主要介绍了SpringBoot使用Redis缓存的实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

SpringBoot中自定义参数绑定步骤详解

这篇文章主要介绍了SpringBoot中自定义参数绑定步骤详解,非常不错,具有参考借鉴价值 ,需要的朋友可以参考下
收藏 0 赞 0 分享

Java实现abc字符串排列组合

这篇文章主要为大家详细介绍了JAVA实现abc字符串的排列组合,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java中后台线程实例解析

这篇文章主要介绍了Java中后台线程实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

ehcache模糊批量移除缓存的方法

本篇文章主要介绍了ehcache模糊批量移除缓存的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Java多线程join方法实例代码

这篇文章主要介绍了Java多线程join方法实例代码,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现字符串排列组合问题

这篇文章主要为大家详细介绍了java实现字符串排列组合问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java排列组合字符串的方法

这篇文章主要介绍了Java排列组合字符串的方法
收藏 0 赞 0 分享

Java语言中的自定义类加载器实例解析

这篇文章主要介绍了Java语言中的自定义类加载器实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多