我有一个带有Spring AMQP的AMQP项目。 RabbitMQ服务器不是我的,所以我无法控制它。当我的应用程序启动时,它会创建一个这样的私有响应队列:
@Bean(name="myAnonymousResponseQueue")
public Queue myAnonymousResponseQueue()
{
Queue q = myAmqpAdmin().declareQueue();
return q;
}
我有一个像这样的SimpleMessageListenerContariner:
@Bean
public SimpleMessageListenerContainer myResponseMessageListenerContainer()
{
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(myConnectionFactory());
container.setQueues(myAnonymousResponseQueue());
container.setMessageListener(myRabbitTemplate());
container.setAcknowledgeMode(AcknowledgeMode.AUTO);
container.setMessageConverter(myMessageConverter());
container.setErrorHandler(myResponseErrorHandler());
container.setAutoStartup(true);
container.setRabbitAdmin(myAmqpAdmin());
return container;
}
我最近遇到了连接问题(ShutdownSignalException)。问题是我无法重新生成私有队列。 首先,这是连接错误:
com.rabbitmq.client.ShutdownSignalException: connection error
at com.rabbitmq.client.impl.AMQConnection.startShutdown(AMQConnection.java:739)
at com.rabbitmq.client.impl.AMQConnection.shutdown(AMQConnection.java:729)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:573)
at java.lang.Thread.run(Unknown Source)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at sun.security.ssl.InputRecord.readFully(Unknown Source)
at sun.security.ssl.InputRecord.read(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
at sun.security.ssl.AppInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at java.io.DataInputStream.readUnsignedByte(Unknown Source)
at com.rabbitmq.client.impl.Frame.readFrom(Frame.java:95)
at com.rabbitmq.client.impl.SocketFrameHandler.readFrame(SocketFrameHandler.java:139)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:538)
... 1 more
但是,当连接恢复后,无法重新创建专用队列:
AbstractConnectionFactory.java|291||Created new connection: SimpleConnection@289cc201 [delegate=amqp://USER@XX.XX.XX.XX:50310/sob]
RabbitAdmin.java|442||Auto-declaring a non-durable, auto-delete, or exclusive Queue (amq.gen-SLigrYFVMvllGTS5m_3AzQ) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'amq.gen-SLigrYFVMvllGTS5m_3AzQ' in vhost 'sob', class-id=50, method-id=10)
BlockingQueueConsumer.java|565||Failed to declare queue:amq.gen-SLigrYFVMvllGTS5m_3AzQ
BlockingQueueConsumer.java|479||Queue declaration failed; retries left=3
org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[amq.gen-SLigrYFVMvllGTS5m_3AzQ]
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:571)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.start(BlockingQueueConsumer.java:470)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1171)
at java.lang.Thread.run(Unknown Source)
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.queueDeclarePassive(ChannelN.java:885)
at com.rabbitmq.client.impl.ChannelN.queueDeclarePassive(ChannelN.java:61)
at org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl.queueDeclarePassive(PublisherCallbackChannelImpl.java:383)
at sun.reflect.GeneratedMethodAccessor110.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.$Proxy94.queueDeclarePassive(Unknown Source)
at org.springframework.amqp.rabbit.listener.BlockingQueueConsumer.attemptPassiveDeclarations(BlockingQueueConsumer.java:550)
... 3 more
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'amq.gen-SLigrYFVMvllGTS5m_3AzQ' in vhost 'sob', class-id=50, method-id=10)
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)
... 12 more
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'amq.gen-SLigrYFVMvllGTS5m_3AzQ' in vhost 'sob', class-id=50, method-id=10)
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:556)
... 1 more
最后我有了这个:
com.rabbitmq.client.ShutdownSignalException: clean channel shutdown; protocol method: #method<channel.close>(reply-code=200, reply-text=OK, class-id=0, method-id=0)
at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:554)
at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:509)
at com.rabbitmq.client.impl.ChannelN.close(ChannelN.java:503)
at org.springframework.amqp.rabbit.support.PublisherCallbackChannelImpl.close(PublisherCallbackChannelImpl.java:642)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$CachedChannelInvocationHandler$1.run(CachingConnectionFactory.java:946)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
因此AMQP连接正常,但私有队列不是。
我不明白为什么会这样。我如何获得更多信息?是否可以从此错误中恢复?
由于
----------------(28/11/2016)-----------
我以这种方式定义匿名队列的原因是:
@Bean(name="myAnonymousResponseQueue")
public Queue myAnonymousResponseQueue()
{
return new AnonymousQueue(new Base64UrlNamingStrategy("amq.gen-"));
}
服务器不允许我创建它,我收到以下错误:
|28-11-2016 08:43:13.203|INFO |org.springframework.amqp.rabbit.core.RabbitAdmin|initialize|RabbitAdmin.java|493||Auto-declaring a non-durable, auto-delete, or exclusive Queue (amq.gen-JUYnhLX2SpqNoJT6ioB8GA) durable:false, auto-delete:true, exclusive:true. It will be redeclared if the broker stops and is restarted while the connection factory is alive, but all messages will be lost.
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'amq.gen-JUYnhLX2SpqNoJT6ioB8GA' in vhost 'sob', class-id=50, method-id=10)
|28-11-2016 08:43:13.486|WARN |org.springframework.amqp.rabbit.listener.BlockingQueueConsumer|attemptPassiveDeclarations|BlockingQueueConsumer.java|581||Failed to declare queue:amq.gen-JUYnhLX2SpqNoJT6ioB8GA
|28-11-2016 08:43:13.486|WARN |org.springframework.amqp.rabbit.listener.BlockingQueueConsumer|start|BlockingQueueConsumer.java|495||Queue declaration failed; retries left=3
org.springframework.amqp.rabbit.listener.BlockingQueueConsumer$DeclarationException: Failed to declare queue(s):[amq.gen-JUYnhLX2SpqNoJT6ioB8GA]
|28-11-2016 08:43:28.868|WARN |org.springframework.amqp.rabbit.listener.BlockingQueueConsumer|attemptPassiveDeclarations|BlockingQueueConsumer.java|581||Failed to declare queue:amq.gen-JUYnhLX2SpqNoJT6ioB8GA
|28-11-2016 08:43:28.869|ERROR|org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer|run|SimpleMessageListenerContainer.java|1372||Consumer received fatal exception on startup
org.springframework.amqp.rabbit.listener.QueuesNotAvailableException: Cannot prepare queue for listener. Either the queue doesn't exist or the broker will not allow us to use it.
那么,AmqpAdmin()。declareQueue()和AnonymousQueue之间的区别是什么?经纪人不允许为队列命名是否可行?
现在我觉得我理解这个问题。我认为我的用户只能创建名为“amq.gen-”的队列。如果我尝试使用任何其他名称:
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue '4788a39b-fffe-4eae-b252-8d842234a018' in vhost 'sob' refused for user 'USER', class-id=50, method-id=10)
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=403, reply-text=ACCESS_REFUSED - access to queue 'amq-_zoOEt5jTcqMduGWNyJ4Zg' in vhost 'sob' refused for user 'USER', class-id=50, method-id=10)
所以,如果我只能使用经纪人生成队列,我需要在重新接收时重新声明它,我该怎么办?
再次感谢你。
修改
我正在尝试应用解决方法。我已经使用:
声明了一个ConnectionListener@Override
public void onCreate(Connection arg0)
{
myResponseMessageListenerContainer.stop();
String[] colaAnterior = myResponseMessageListenerContainer.getQueueNames();
Queue q = myAmqpAdmin.declareQueue();
q.setAdminsThatShouldDeclare(myAmqpAdmin);
q.setShouldDeclare(true);
myResponseMessageListenerContainer.addQueueNames(q.getName());
myResponseMessageListenerContainer.removeQueueNames(colaAnterior);
myResponseMessageListenerContainer.initialize();
myResponseMessageListenerContainer.start();
}
但现在我有这个错误:
|30-11-2016 10:56:17.312|ERROR|org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer|redeclareElementsIfNecessary|SimpleMessageListenerContainer.java|1116||Failed to check/redeclare auto-delete queue(s).
org.springframework.amqp.UncategorizedAmqpException: java.lang.IllegalStateException: Listener expects us to be listening on '[amq.gen-Xg5MG5n42ecpoW4-DA198A]'; our queues: [amq.gen-2qRLgfUmMxskshFCi1dzuA]
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:80)
at org.springframework.amqp.rabbit.connection.RabbitAccessor.convertRabbitAccessException(RabbitAccessor.java:113)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.start(AbstractMessageListenerContainer.java:553)
at es.omie.amqp.config.listener.XBIDConnectionListener.onCreate(XBIDConnectionListener.java:57)
at org.springframework.amqp.rabbit.connection.CompositeConnectionListener.onCreate(CompositeConnectionListener.java:33)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:553)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createBareChannel(CachingConnectionFactory.java:500)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getCachedChannelProxy(CachingConnectionFactory.java:474)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getChannel(CachingConnectionFactory.java:467)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory.access$1500(CachingConnectionFactory.java:97)
at org.springframework.amqp.rabbit.connection.CachingConnectionFactory$ChannelCachingConnectionProxy.createChannel(CachingConnectionFactory.java:1084)
at org.springframework.amqp.rabbit.core.RabbitTemplate.doExecute(RabbitTemplate.java:1394)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1370)
at org.springframework.amqp.rabbit.core.RabbitTemplate.execute(RabbitTemplate.java:1346)
at org.springframework.amqp.rabbit.core.RabbitAdmin.getQueueProperties(RabbitAdmin.java:335)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.redeclareElementsIfNecessary(SimpleMessageListenerContainer.java:1102)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$700(SimpleMessageListenerContainer.java:95)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1278)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalStateException: Listener expects us to be listening on '[amq.gen-Xg5MG5n42ecpoW4-DA198A]'; our queues: [amq.gen-2qRLgfUmMxskshFCi1dzuA]
at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doStart(SimpleMessageListenerContainer.java:770)
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.start(AbstractMessageListenerContainer.java:550)
... 16 more
我一直在努力,但有关如何修复它的任何想法?
由于
编辑2
现在我有了这个:
@Override
public void onCreate(Connection arg0)
{
myResponseMessageListenerContainer.stop();
String[] colaAnterior = myResponseMessageListenerContainer.getQueueNames();
Queue q = myAmqpAdmin.declareQueue();
log.info(" ------ RESPONSE QUEUE -> OLD NAME: " + Arrays.asList(colaAnterior) + " NEW NAME: " + q.getName());
myResponseMessageListenerContainer.addQueueNames(q.getName());
myRabbitTemplate.setReplyAddress(q.getName());
myRabbitTemplate.setQueue(q.getName());
myResponseMessageListenerContainer.removeQueueNames(colaAnterior);
myResponseMessageListenerContainer.shutdown();
myResponseMessageListenerContainer.initialize();
myResponseMessageListenerContainer.start();
}
但是容器不会停留在最后定义的队列中:
|30-11-2016 16:32:19.996|INFO |es.omie.amqp.config.listener.XBIDConnectionListener|onCreate|XBIDConnectionListener.java|42|| ------ RESPONSE QUEUE -> OLD NAME: [amq.gen-uTp6TCP66x2AlXUmQzqz8g] NEW NAME: amq.gen-DOwEn8WKz_9ymCBQFiMDNg
com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - no queue 'amq.gen-uTp6TCP66x2AlXUmQzqz8g' in vhost 'sob', class-id=50, method-id=10)
我看不到新的队列名称。
我可以在日志中看到在创建连接之前重启容器:
Restarting Consumer: tags=[{amq.ctag-KYCURTkS4EevXHQtrpYV9Q=amq.gen-uTp6TCP66x2AlXUmQzqz8g}]
但不是在启动方法之后。
答案 0 :(得分:1)
由于这个原因,Spring AMQP提供了自己的AnonymousQueue
;它的随机名称将被保留,因此,当重新建立连接时,将重新创建声明。您的代码允许代理命名队列。
但是,您应该让RabbitAdmin
自动处理声明,而不是自己处理。
@Bean
public Queue myAnonymousResponseQueue() {
return new AnonymousQueue();
}
当admin(也必须是@Bean
)检测到初始连接(或重新连接)时,它将声明所有此类队列。
修改强>
如果您的管理员不允许您为队列命名(您不能使用amq.gen-
作为前缀),则必须重新声明代理生成的队列并使用新的队列更新容器队列中。
将ConnectionListener
添加到连接工厂;当调用onCreate()
时(因为建立了新连接),停止容器,重新声明队列,将容器的队列更新为新名称;启动容器。
请记住,删除连接后,临时队列中的所有邮件都将丢失。
由于可能在容器线程上调用侦听器,因此您应该将此工作移交给另一个线程;否则会有延迟。
<强> EDIT2 强>
@SpringBootApplication
@EnableRabbit
public class So40802855Application {
public static void main(String[] args) throws Exception {
ConfigurableApplicationContext context = SpringApplication.run(So40802855Application.class, args);
Thread.sleep(2000); // wait for reply queue setup
RabbitTemplate template = context.getBean(RabbitTemplate.class);
System.out.println(template.convertSendAndReceive("test.x", "foo"));
context.getBean(CachingConnectionFactory.class).resetConnection();
Thread.sleep(2000); // wait for reply queue setup
System.out.println(template.convertSendAndReceive("test.x", "bar"));
context.getBean(RabbitAdmin.class).deleteQueue("test.x");
context.close();
System.exit(0);
}
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setReplyTimeout(30000);
return rabbitTemplate;
}
@Bean
public SimpleMessageListenerContainer replyContainer(ConnectionFactory connectionFactory) {
connectionFactory.addConnectionListener(listener());
SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
container.setMessageListener(rabbitTemplate(connectionFactory));
container.setAutoStartup(false);
container.setDeclarationRetries(0);
container.setFailedDeclarationRetryInterval(500);
return container;
}
@Bean
public Queue testX() {
return new Queue("test.x");
}
@Bean
public ConnectionListener listener() {
return new MyConnectionListener();
}
@RabbitListener(queues = "test.x")
public String listen(String in) {
return in.toUpperCase();
}
public static class MyConnectionListener implements ConnectionListener {
private static final Log logger = LogFactory.getLog(MyConnectionListener.class);
@Autowired
private RabbitTemplate template;
@Autowired
private AmqpAdmin admin;
@Autowired
private ApplicationContext applicationContext;
@Override
public void onCreate(Connection connection) {
SimpleMessageListenerContainer replyContainer = applicationContext.getBean("replyContainer",
SimpleMessageListenerContainer.class);
// need to stop/start asynchronously to avoid deadlock
Executors.newSingleThreadExecutor().execute(() -> {
if (replyContainer.isRunning()) {
logger.info("Waiting for the container to stop itself because of missing queue");
while (replyContainer.isRunning()) {
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
logger.info("Container stopped itself because of missing queue");
}
Queue queue = this.admin.declareQueue();
logger.info("Changing queue from " + Arrays.asList(replyContainer.getQueueNames()) + " to "
+ queue.getName());
this.template.setReplyAddress(queue.getName());
replyContainer.setQueues(queue);
logger.info("Starting container");
replyContainer.start();
});
}
@Override
public void onClose(Connection connection) {
}
}
}