欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 11:40:16
配置文件,在rabbit中自动建立exchange,queue和绑定它们的关系 代码里初始化exchange 代码里初始化queue 代码里绑定e...

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

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