Skip to content

Commit

Permalink
GH-2425: Fix NPE in ACFactory.shutdownCompleted
Browse files Browse the repository at this point in the history
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
  • Loading branch information
garyrussell authored and artembilan committed Mar 14, 2023
1 parent 5553102 commit c1f9dd4
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -669,15 +670,18 @@ 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);
}
else if (protocolClassId == RabbitUtils.CONNECTION_PROTOCOL_CLASS_ID_10) {
getConnectionListener().onShutDown(cause);
}

}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}

0 comments on commit c1f9dd4

Please sign in to comment.