diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java index cc0d1c67be..d778f1d486 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java @@ -55,6 +55,7 @@ import com.rabbitmq.client.Address; import com.rabbitmq.client.AddressResolver; import com.rabbitmq.client.BlockedListener; +import com.rabbitmq.client.Method; import com.rabbitmq.client.Recoverable; import com.rabbitmq.client.RecoveryListener; import com.rabbitmq.client.ShutdownListener; @@ -669,7 +670,11 @@ protected final String getDefaultHostName() { @Override public void shutdownCompleted(ShutdownSignalException cause) { - int protocolClassId = cause.getReason().protocolClassId(); + Method reason = cause.getReason(); + int protocolClassId = RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10; + if (reason != null) { + protocolClassId = reason.protocolClassId(); + } if (protocolClassId == RabbitUtils.CHANNEL_PROTOCOL_CLASS_ID_20) { this.closeExceptionLogger.log(this.logger, "Shutdown Signal", cause); getChannelListener().onShutDown(cause); @@ -677,7 +682,6 @@ public void shutdownCompleted(ShutdownSignalException cause) { else if (protocolClassId == RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10) { getConnectionListener().onShutDown(cause); } - } @Override diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java index 3fc935d5cb..9a9f243f05 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2010-2020 the original author or authors. + * Copyright 2010-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java index 380994f869..2e7fd8faa3 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java @@ -1907,4 +1907,39 @@ void testResolver() throws Exception { verify(mockConnectionFactory).newConnection(any(ExecutorService.class), eq(resolver), anyString()); } + @Test + void nullShutdownCause() { + com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); + AbstractConnectionFactory cf = createConnectionFactory(mockConnectionFactory); + AtomicBoolean connShutDown = new AtomicBoolean(); + cf.addConnectionListener(new ConnectionListener() { + + @Override + public void onCreate(Connection connection) { + } + + @Override + public void onShutDown(ShutdownSignalException signal) { + connShutDown.set(true); + } + + }); + AtomicBoolean chanShutDown = new AtomicBoolean(); + cf.addChannelListener(new ChannelListener() { + + @Override + public void onCreate(Channel channel, boolean transactional) { + } + + @Override + public void onShutDown(ShutdownSignalException signal) { + chanShutDown.set(true); + } + + }); + cf.shutdownCompleted(new ShutdownSignalException(false, false, null, chanShutDown)); + assertThat(connShutDown.get()).isTrue(); + assertThat(chanShutDown.get()).isFalse(); + } + }