Search code examples
spring-bootrabbitmqspring-amqp

How to get Channel object in Spring boot AMQP and create a Exchange of type "x-consistent-hash"


The examples mentioned to create a Consistent hashing exchange in the RabbitMQ Consistent Hasing Github uses Channel to create exchanges :

private static String CONSISTENT_HASH_EXCHANGE_TYPE = "x-consistent-hash";
...
Channel ch = conn.createChannel();
...
ch.exchangeDeclare("e1", CONSISTENT_HASH_EXCHANGE_TYPE, true, false, null);

Exchange details



I tried creating consistent hash exchange using arguments but it was not working :
@Configuration
@EnableAutoConfiguration
public class AMQPConfig {

    public static final String QUEUENAME = "consistentHashing-Q1";
    public static final String EXCHANGENAME = "consistentHashing-DE1";
    public static final String RK = "consistentHashing-RK1";


    @Bean
    public Queue queue() {
        return QueueBuilder.nonDurable(QUEUENAME).autoDelete().build();
    }

    @Bean
    public DirectExchange directExchange()  {
        return ExchangeBuilder.directExchange(EXCHANGENAME).autoDelete().withArgument("Type", "x-consistent-hash").build();
    }

    @Bean
    public Binding binding(Queue queue, Exchange exchange)    {
        return BindingBuilder.bind(queue).to(exchange).with(RK).noargs();
    }
}

Exchange not working


  1. Would like to know how to get Channel object in the Spring boot
  2. AMQP? How to create a Consistent hash exchange in Spring boot AMQP?

Solution

  • Was able to create Consistent Hashing Exchange in Spring boot AMQP using the below code :

    @Bean
    public CustomExchange customExchange()  {
        CustomExchange customExchange = new CustomExchange(EXCHANGENAME, "x-consistent-hash", false, true);
        customExchange.addArgument("hash-header", "client-id");
        return customExchange;
    }