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

 更新时间:2018年10月03日 09:59:47   作者:张占岭(仓储大叔,Lind)  
这篇文章主要介绍了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);
 }
}

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

相关文章

  • Java多线程并发编程和锁原理解析

    Java多线程并发编程和锁原理解析

    这篇文章主要介绍了Java多线程并发编程和锁原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-12-12
  • Core Java 简单谈谈HashSet(推荐)

    Core Java 简单谈谈HashSet(推荐)

    下面小编就为大家带来一篇Core Java 简单谈谈HashSet(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-09-09
  • Java关于JDK1.8中的Optional类

    Java关于JDK1.8中的Optional类

    本文主要介绍了Optional类的一些常用方法,以及其应用场景,其主要是为了规避空指针异常(NPE)。熟练的运用Optional类可以很大的简化我们的代码,使代码简洁明了。,需要的朋友可以参考下面文章内容
    2021-09-09
  • Spring中ApplicationContextAware的使用方法详解

    Spring中ApplicationContextAware的使用方法详解

    ApplicationContextAware 通过它Spring容器会自动把上下文环境对象调用ApplicationContextAware接口中的setApplicationContext方法,这篇文章主要介绍了Spring中ApplicationContextAware的作用,需要的朋友可以参考下
    2023-03-03
  • Spring计时器stopwatch使用详解

    Spring计时器stopwatch使用详解

    这篇文章主要介绍了Spring计时器stopwatch使用详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-08-08
  • Java面试synchronized偏向锁后hashcode存址

    Java面试synchronized偏向锁后hashcode存址

    这篇文章主要为大家介绍了Java面试中synchronized偏向锁后hashcode存址详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Java并发编程之关键字volatile的深入解析

    Java并发编程之关键字volatile的深入解析

    提高java的并发编程,就不得不提volatile关键字,不管是在面试还是实际开发中volatile都是一个应该掌握的技能,这篇文章主要给大家介绍了关于Java并发编程之关键字volatile的相关资料,需要的朋友可以参考下
    2021-09-09
  • 浅谈Spring中的循环依赖问题与解决方案

    浅谈Spring中的循环依赖问题与解决方案

    这篇文章主要介绍了浅谈Spring中的循环依赖问题与解决方案,循环依赖就是两个或则两个以上的bean互相持有对方,最终形成闭环,比如A依赖于B,B依赖于C,C又依赖于A,需要的朋友可以参考下
    2023-12-12
  • 仿京东平台框架开发开放平台(包含需求,服务端代码,SDK代码)

    仿京东平台框架开发开放平台(包含需求,服务端代码,SDK代码)

    现在开放平台越来越多了,下面针对仿京东开放平台框架,封装自己的开放平台,分享给大家。先感谢一下京东开放平台的技术大佬们,下面从开放平台需求,服务端代码,SDK代码三大块进行分享
    2021-06-06
  • java 数学计算的具体使用

    java 数学计算的具体使用

    这篇文章主要介绍了java 数学计算的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-01-01

最新评论