我知道之前已经问过这个问题,但我找不到任何合适的问题解决方案,因此再次发布。
我正在尝试使用spring-amqp编写一个rabbitmq生成器,以在直接队列上发布我的消息。由于它是一个生产者应用程序,我必须创建我的交换和队列。另外,我没有定义任何监听器bean,因为我在生产者端不需要它。 以下是我的 rabbitmq-config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.5.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Spring AMQP Admin -->
<rabbit:admin id="amqpAdmin"
connection-factory="rabbitmqConnectionFactory"
auto-startup="true" />
<!-- Spring AMQP Template -->
<rabbit:template id="amqpTemplate"
connection-factory="rabbitmqConnectionFactory"
routing-key="my.key" exchange="my.exchange"
channel-transacted="true"/>
<!-- Connection Factory -->
<rabbit:connection-factory id="rabbitmqConnectionFactory"
host="${rabbitmq.host:localhost}"
port="${rabbitmq.port:5672}"
username="${rabbitmq.username}"
password="${rabbitmq.password}"
virtual-host="${rabbitmq.vhost:/}"
cache-mode="CONNECTION"
channel-cache-size="${rabbitmq.channel-cache-size:25}" />
<!-- Queue and Exchange -->
<rabbit:queue id="my.queue" durable="false"
auto-declare="true" auto-delete="true" />
<rabbit:direct-exchange name="my.exchange" durable="false"
auto-declare="true" auto-delete="true" declared-by="amqpAdmin">
<rabbit:bindings>
<rabbit:binding queue="my.queue" key="my.key"/>
</rabbit:bindings>
</rabbit:direct-exchange>
</beans>
我只有一个简单的生成器类,它使用上面的 amqpTemplate 来发送消息。 但是当我尝试运行我的生产者应用程序时,我得到的只是: no exchange&#39; my.exchange&#39;在vhost&#39; /&#39;错误
ERROR CachingConnectionFactory:1089 - Channel shutdown: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchange 'my.exchange' in vhost '/', class-id=60, method-id=40) DEBUG CachingConnectionFactory:842 - Detected closed channel on exception. Re-initializing: AMQChannel(amqp://s3conn@127.0.0.1:5672/,1) DEBUG RabbitResourceHolder:189 - Rolling back messages to channel: Cached Rabbit Channel: AMQChannel(amqp://s3conn@127.0.0.1:5672/,1), conn: Proxy@17a7f733 Dedicated Rabbit Connection: SimpleConnection@2118cddf [delegate=amqp://s3conn@127.0.0.1:5672/] TRACE CachingConnectionFactory:906 - Returning cached Channel: AMQChannel(amqp://s3conn@127.0.0.1:5672/,1) Exception in thread "main" DEBUG CachingConnectionFactory:1008 - Returning connection 'Proxy@17a7f733 Dedicated Rabbit Connection: SimpleConnection@2118cddf [delegate=amqp://s3conn@127.0.0.1:5672/]' to cache org.springframework.amqp.AmqpIOException: java.io.IOException at org.springframework.amqp.rabbit.connection.RabbitUtils.commitIfNecessary(RabbitUtils.java:105) at org.springframework.amqp.rabbit.core.RabbitTemplate.doSend(RabbitTemplate.java:1354) at org.springframework.amqp.rabbit.core.RabbitTemplate$3.doInRabbit(RabbitTemplate.java:623) at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1298) at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1271) at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:619) at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:613) at org.springframework.amqp.rabbit.core.RabbitTemplate.send(RabbitTemplate.java:603) at com.symantec.spe.cloud.amqpclient.JsonMessagePublisherImpl.sendMessage(JsonMessagePublisherImpl.java:53) at com.symantec.spe.cloud.amqpclient.test.TestProducer.main(TestProducer.java:50) Caused by: java.io.IOException at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:106) at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:102) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:124) at com.rabbitmq.client.impl.ChannelN.txCommit(ChannelN.java:1163) at com.rabbitmq.client.impl.ChannelN.txCommit(ChannelN.java:61) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler.invoke(CachingConnectionFactory.java:835) at com.sun.proxy.$Proxy7.txCommit(Unknown Source) at org.springframework.amqp.rabbit.connection.RabbitUtils.commitIfNecessary(RabbitUtils.java:103) ... 9 more Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchang e 'my.exchange' in vhost '/', class-id=60, method-id=40) at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:67) at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:33) at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:361) at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:226) at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:118) ... 18 more Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no exchang e 'my.exchange' in vhost '/', class-id=60, method-id=40) at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:484) at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:321) at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:144) at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:91) at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:554) at java.lang.Thread.run(Unknown Source)
对此的任何帮助都将非常感激。感谢。
答案 0 :(得分:3)
你的问题是:
cache-mode="CONNECTION"
请参阅Reference Manual:
仅在
CachingConnectionFactory
缓存模式为CHANNEL
(默认值)时执行自动声明。存在此限制是因为exlusive和自动删除队列绑定到连接。
(哎呀!错字就在那里。很快就会解决:-))