Ritesh Kaushik
Ritesh Kaushik

Reputation: 735

ShutdownSignalException while using Spring Cloud + Spring ampq + Spring Boot + RabbitMQ

I am working on a spring cloud based application using Spring Boot and RabbitMQ. I am trying to use FanoutExchange to create an exchange in RabbitMQ. My requirement is to send same message to multiple queues, so i am using FanoutExchange But getting the exception as :

Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; 
protocol method: #method<channel.close>(reply-code=406, reply-text=PRECONDITION_FAILED
 - inequivalent arg 'type' for exchange 'rkk_exchange1' in vhost  
'': received 'fanout' but current is 'topic', 
class-id=40, method-id=10)

Below is the code (configuration file):

@Configuration
@Profile("cloud")
public class MultiListenerRabbitConfig extends AbstractCloudConfig{

private static final String QUEUE_NAME1="rk.rkk_queue1";
private static final String QUEUE_NAME2="rk.rkk_queue2";
private static final String EXCHANGE_NAME="rkk_exchange1";
private static final int PORT = 1;


@Bean
public CachingConnectionFactory cachingConnectionFactory(){

    CloudFactory cloudFactory = new CloudFactory();
    Cloud cloud = cloudFactory.getCloud();
    List<ServiceInfo> listServices = cloud.getServiceInfos();
    AmqpServiceInfo serviceInfo = (AmqpServiceInfo) cloud.getServiceInfo(cloud.getServiceInfos().get(0).getId());


    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setUsername(serviceInfo.getUserName());
    cachingConnectionFactory.setPassword(serviceInfo.getPassword());
    cachingConnectionFactory.setVirtualHost(serviceInfo.getVirtualHost());
    cachingConnectionFactory.setHost(serviceInfo.getHost());
    cachingConnectionFactory.setPort(PORT);
    cachingConnectionFactory.setRequestedHeartBeat(30);
    cachingConnectionFactory.setConnectionTimeout(30000);
    return cachingConnectionFactory;
}

@Bean
public RabbitTemplate rabbitTemplate(){
    System.out.println("-----------------**************------------------ Create Rabbit Template");
    RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory());

    return template;
}


@Bean
public RabbitAdmin rabbitAdmin(){
    //Set up queue, exchanges and bindings
    RabbitAdmin admin = new RabbitAdmin(cachingConnectionFactory());
    Queue queue1 = new Queue(QUEUE_NAME1);
    admin.declareQueue(queue1);
    //For a Fanout Exchange, any Queue bound to that Exchange will receive any message sent to that Exchange
    FanoutExchange exchange = new FanoutExchange(EXCHANGE_NAME);
    admin.declareExchange(exchange);
    Binding binding = BindingBuilder.bind(queue1).to(exchange);
    admin.declareBinding(binding);


    return admin;
}
}

Putting the stack trace:

ERR Caused by: java.io.IOException 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:693) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:662) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:61) 2016-09-14T19:42:02.68+0530 [App/0] ERR at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 2016-09-14T19:42:02.68+0530 [App/0] ERR at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 2016-09-14T19:42:02.68+0530 [App/0] ERR at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 2016-09-14T19:42:02.68+0530 [App/0] ERR at java.lang.reflect.Method.invoke(Method.java:498) 2016-09-14T19:42:02.68+0530 [App/0] ERR at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:625) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.sun.proxy.$Proxy50.exchangeDeclare(Unknown Source) 2016-09-14T19:42:02.68+0530 [App/0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin.declareExchanges(RabbitAdmin.java:422) 2016-09-14T19:42:02.68+0530 [App/0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin.access$000(RabbitAdmin.java:54) 2016-09-14T19:42:02.68+0530 [App/0] ERR at org.springframework.amqp.rabbit.core.RabbitAdmin$1.doInRabbit(RabbitAdmin.java:111) 2016-09-14T19:42:02.68+0530 [App/0] ERR at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1045) 2016-09-14T19:42:02.68+0530 [App/0] ERR ... 38 more 2016-09-14T19:42:02.68+0530 [App/0] ERR Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'rkk_exchange1' in vhost '': received 'fanout' but current is 'topic', class-id=40, method-id=10) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:343) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:216) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) 2016-09-14T19:42:02.68+0530 [App/0] ERR ... 51 more 2016-09-14T19:42:02.68+0530 [App/0] ERR Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method(reply-code=406, reply-text=PRECONDITION_FAILED - inequivalent arg 'type' for exchange 'rkk_exchange1' in vhost '': received 'fanout' but current is 'topic', class-id=40, method-id=10) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:478) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) 2016-09-14T19:42:02.68+0530 [App/0] ERR at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:552) 2016-09-14T19:42:02.68+0530 [App/0] ERR ... 1 more 2016-09-14T19:42:02.73+0530 [DEA/19] ERR Instance (index 0) failed to start accepting connections

Note: This app is working with TopicExchange , but my requirement is to use FanoutExchange

Upvotes: 0

Views: 1670

Answers (1)

Gary Russell
Gary Russell

Reputation: 174574

You can't change an exchange type (it's currently a topic exchange); you have to delete it and re-add it.

Also, you should not be doing work (declarations) in a @Bean definition - the application context is not yet built and will likely cause problems. Instead, define the bean then get a reference to it later to do the declarations.

@Bean
public RabbitAdmin rabbitAdmin(){
    return new RabbitAdmin(cachingConnectionFactory());
}

You need to do the declarations after the context is completely built.

Upvotes: 3

Related Questions