Skip to content

Commit c1f9dd4

Browse files
garyrussellartembilan
authored andcommitted
GH-2425: Fix NPE in ACFactory.shutdownCompleted
Resolves #2425 Assume connection problem when no cause. **cherry-pick to 2.4.x** # Conflicts: # spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java
1 parent 5553102 commit c1f9dd4

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactory.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.rabbitmq.client.Address;
5656
import com.rabbitmq.client.AddressResolver;
5757
import com.rabbitmq.client.BlockedListener;
58+
import com.rabbitmq.client.Method;
5859
import com.rabbitmq.client.Recoverable;
5960
import com.rabbitmq.client.RecoveryListener;
6061
import com.rabbitmq.client.ShutdownListener;
@@ -669,15 +670,18 @@ protected final String getDefaultHostName() {
669670

670671
@Override
671672
public void shutdownCompleted(ShutdownSignalException cause) {
672-
int protocolClassId = cause.getReason().protocolClassId();
673+
Method reason = cause.getReason();
674+
int protocolClassId = RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10;
675+
if (reason != null) {
676+
protocolClassId = reason.protocolClassId();
677+
}
673678
if (protocolClassId == RabbitUtils.CHANNEL_PROTOCOL_CLASS_ID_20) {
674679
this.closeExceptionLogger.log(this.logger, "Shutdown Signal", cause);
675680
getChannelListener().onShutDown(cause);
676681
}
677682
else if (protocolClassId == RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10) {
678683
getConnectionListener().onShutDown(cause);
679684
}
680-
681685
}
682686

683687
@Override

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/AbstractConnectionFactoryTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2010-2020 the original author or authors.
2+
* Copyright 2010-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,4 +1907,39 @@ void testResolver() throws Exception {
19071907
verify(mockConnectionFactory).newConnection(any(ExecutorService.class), eq(resolver), anyString());
19081908
}
19091909

1910+
@Test
1911+
void nullShutdownCause() {
1912+
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
1913+
AbstractConnectionFactory cf = createConnectionFactory(mockConnectionFactory);
1914+
AtomicBoolean connShutDown = new AtomicBoolean();
1915+
cf.addConnectionListener(new ConnectionListener() {
1916+
1917+
@Override
1918+
public void onCreate(Connection connection) {
1919+
}
1920+
1921+
@Override
1922+
public void onShutDown(ShutdownSignalException signal) {
1923+
connShutDown.set(true);
1924+
}
1925+
1926+
});
1927+
AtomicBoolean chanShutDown = new AtomicBoolean();
1928+
cf.addChannelListener(new ChannelListener() {
1929+
1930+
@Override
1931+
public void onCreate(Channel channel, boolean transactional) {
1932+
}
1933+
1934+
@Override
1935+
public void onShutDown(ShutdownSignalException signal) {
1936+
chanShutDown.set(true);
1937+
}
1938+
1939+
});
1940+
cf.shutdownCompleted(new ShutdownSignalException(false, false, null, chanShutDown));
1941+
assertThat(connShutDown.get()).isTrue();
1942+
assertThat(chanShutDown.get()).isFalse();
1943+
}
1944+
19101945
}

0 commit comments

Comments
 (0)