在这种情况下,我需要在运行时动态地将队列注册到SimpleMessageListenerContainer。我所遇到的问题是一个僵局,因为这是因为:
Thread: [52] Thread1 wants the lock java.lang.Object@5537e0df
org.springframework.amqp.rabbit.connection.CachingConnectionFactory.getDeferredCloseExecutor(CachingConnectionFactory.java:907)
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.restart(SimpleMessageListenerContainer.java:739)
Thread: [183] Thread2 wants the lock java.lang.Object@556fa9d6
org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.queuesChanged(SimpleMessageListenerContainer.java:689)
org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createConnection(CachingConnectionFactory.java:634)
org.springframework.amqp.rabbit.connection.CachingConnectionFactory.createBareChannel(CachingConnectionFactory.java:578)

这就是有问题的代码--在这里,我尝试在onCreate回调中设置connectionListener中的客户端队列。
connectionFactory
.addConnectionListener(
new ConnectionListener() {
@Override
public void onCreate(Connection connection) {
setupClientQueues(); ----> will call container.setQueueNames which will result in calling queuesChanged
}
@Override
public void onClose(Connection connection) {
// nothing to do
}
});是否有什么标准(适当)方法可以方便地注册和动态创建队列,而不会导致这种死锁?
更新
根据Garry的建议,我现在就是这样处理它的:
@Bean
public SmartLifecycle containerQueueSetter(){
return new SmartLifecycle(){
private boolean running;
@Override
public int getPhase() {
return 0;
}
@Override
public void start() {
//CREATE QUEUES HERE - since I create and register them as beans,
//it will work even when rabbit is reconnected
//REGISTER QUEUES TO SIMPLE_MESSAGE_LISTENER_CONTAINER
running = true;
}
@Override
public void stop() {
log.info("Stopping dynamic queue registerer.");
running = false;
}
@Override
public boolean isRunning() {
return running;
}
@Override
public boolean isAutoStartup() {
return true;
}
@Override
public void stop(Runnable callback) {
stop();
callback.run();
}
};
}发布于 2019-08-27 12:36:03
最好是实现SmartLifecycle并在start()中进行设置。
侦听器容器默认的phase是Integer.MAX_VALUE,因此容器是应用程序上下文启动的最后一个。
将您的SmartLifecycle放在一个较早的阶段(例如0),以便在启动之前设置容器。
https://stackoverflow.com/questions/57670878
复制相似问题