我正在使用spring和RabbitMQ开发一个基于Spring云的应用程序。我试图使用FanoutExchange在RabbitMQ中创建一个交换。我的要求是向多个队列发送相同的消息,因此我使用的是FanoutExchange (),但获得的异常如下:
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)下面是代码(配置文件):
@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;
}
}放置堆栈跟踪:
错误原因: com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) 2016-09-14T19:42:02.68+0530 App/0 ERR 2016-09-14T19:42:02.68+0530 App/0 com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) 2016-09-14T19:42:02.68+0530 App/0(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在com.rabbitmq.client.impl.ChannelN.exchangeDeclare(ChannelN.java:61) 2016- 09-14T19:42:02.68+0530 App/0 ERR09-14T19:42:02.68+0530 App/0 ERR在sun.reflect.NativeMethodAccessorImpl.invoke0(Native方法中) 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。org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:625) 2016-09-14T19:42:02.68+0530 App/0 reflect.Method.invoke at org.springframework.amqp.rabbit.core.RabbitAdmin.declareExchanges(RabbitAdmin 2016-09-14T19:42:02.68+0530 App/0 ERRorg.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.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多个2016-09-14T19:42:02.68+0530 App/0 ERR : com.rabbitmq.client.ShutdownSignalException:信道错误;协议方法:#方法(回-代码=406,回-文本=前提_FAILED不等效的arg 'type‘交换'rkk_exchange1’在vhost‘:接收’扇出‘但当前是’主题‘,类-id=40,方法-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-14T19com.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-09-14T19 :42:02.68+0530 App/0 ERR : com.rabbitmq.client.ShutdownSignalException:信道错误;协议方法:#方法(回-代码=406,回-文本=前提_FAILED不等效的arg 'type‘交换'rkk_exchange1’在vhost‘:接收’扇出‘但当前是’主题‘,类-id=40,方法-id=10) com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:478) 2016-09-14T19:42:02.68+0530 App/0 ERR 2016-09-14T19:42:02.68+0530 App/0 at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:315) 2016-09-14T19:42:02.68+0530 App/0) 2016-09-14T19:42:02.68+0530 App/0 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
注意:这个应用程序使用的是TopicExchange,但我的要求是使用FanoutExchange
发布于 2016-09-14 15:06:42
您不能更改exchange类型(它目前是一个主题交换);您必须删除它并重新添加它。
另外,您应该不做@Bean定义中的(声明)工作--应用程序上下文尚未构建,可能会导致问题。相反,定义bean,然后获得对它的引用,以执行声明。
@Bean
public RabbitAdmin rabbitAdmin(){
return new RabbitAdmin(cachingConnectionFactory());
}您需要在上下文完全构建之后执行声明。
https://stackoverflow.com/questions/39493117
复制相似问题