Skip to content

Commit a0398ac

Browse files
authored
GH-2425: Fix NPE in ACFactory.shutdownCompleted
Resolves #2425 Assume connection problem when no cause. **cherry-pick to 2.4.x**
1 parent b27a18f commit a0398ac

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;
@@ -660,15 +661,18 @@ protected final String getDefaultHostName() {
660661

661662
@Override
662663
public void shutdownCompleted(ShutdownSignalException cause) {
663-
int protocolClassId = cause.getReason().protocolClassId();
664+
Method reason = cause.getReason();
665+
int protocolClassId = RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10;
666+
if (reason != null) {
667+
protocolClassId = reason.protocolClassId();
668+
}
664669
if (protocolClassId == RabbitUtils.CHANNEL_PROTOCOL_CLASS_ID_20) {
665670
this.closeExceptionLogger.log(this.logger, "Shutdown Signal", cause);
666671
getChannelListener().onShutDown(cause);
667672
}
668673
else if (protocolClassId == RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10) {
669674
getConnectionListener().onShutDown(cause);
670675
}
671-
672676
}
673677

674678
@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-2022 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
@@ -1932,4 +1932,39 @@ void testResolver() throws Exception {
19321932
verify(mockConnectionFactory).newConnection(any(ExecutorService.class), eq(resolver), anyString());
19331933
}
19341934

1935+
@Test
1936+
void nullShutdownCause() {
1937+
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
1938+
AbstractConnectionFactory cf = createConnectionFactory(mockConnectionFactory);
1939+
AtomicBoolean connShutDown = new AtomicBoolean();
1940+
cf.addConnectionListener(new ConnectionListener() {
1941+
1942+
@Override
1943+
public void onCreate(Connection connection) {
1944+
}
1945+
1946+
@Override
1947+
public void onShutDown(ShutdownSignalException signal) {
1948+
connShutDown.set(true);
1949+
}
1950+
1951+
});
1952+
AtomicBoolean chanShutDown = new AtomicBoolean();
1953+
cf.addChannelListener(new ChannelListener() {
1954+
1955+
@Override
1956+
public void onCreate(Channel channel, boolean transactional) {
1957+
}
1958+
1959+
@Override
1960+
public void onShutDown(ShutdownSignalException signal) {
1961+
chanShutDown.set(true);
1962+
}
1963+
1964+
});
1965+
cf.shutdownCompleted(new ShutdownSignalException(false, false, null, chanShutDown));
1966+
assertThat(connShutDown.get()).isTrue();
1967+
assertThat(chanShutDown.get()).isFalse();
1968+
}
1969+
19351970
}

0 commit comments

Comments
 (0)