Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions src/main/java/com/rabbitmq/client/ConnectionFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ public class ConnectionFactory implements Cloneable {
private ObservationCollector observationCollector = ObservationCollector.NO_OP;

private boolean netty = false;
private FrameHandlerFactory frameHandlerFactory;
// lazily created, so the Netty classes it references are loaded only when Netty is activated
private NettyConfiguration nettyConf;

Expand Down Expand Up @@ -1117,25 +1116,21 @@ public ConnectionFactory setCredentialsRefreshService(

protected synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
if (netty) {
if (this.frameHandlerFactory == null) {
Predicate<ShutdownSignalException> recoveryCondition =
this.connectionRecoveryTriggeringCondition == null
? AutorecoveringConnection.DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION
: this.connectionRecoveryTriggeringCondition;
this.frameHandlerFactory =
new NettyFrameHandlerFactory(
this.nettyConf.eventLoopGroup,
this.nettyConf.channelCustomizer,
this.nettyConf.bootstrapCustomizer,
this.nettyConf.sslContextFactory,
this.nettyConf.enqueuingTimeout,
connectionTimeout,
socketConf,
AMQP.FRAME_MIN_SIZE,
this.automaticRecovery,
recoveryCondition);
}
return this.frameHandlerFactory;
Predicate<ShutdownSignalException> recoveryCondition =
this.connectionRecoveryTriggeringCondition == null
? AutorecoveringConnection.DEFAULT_CONNECTION_RECOVERY_TRIGGERING_CONDITION
: this.connectionRecoveryTriggeringCondition;
return new NettyFrameHandlerFactory(
this.nettyConf.eventLoopGroup,
this.nettyConf.channelCustomizer,
this.nettyConf.bootstrapCustomizer,
this.nettyConf.sslContextFactory,
this.nettyConf.enqueuingTimeout,
connectionTimeout,
socketConf,
AMQP.FRAME_MIN_SIZE,
this.automaticRecovery,
recoveryCondition);
} else {
return new SocketFrameHandlerFactory(
connectionTimeout,
Expand Down Expand Up @@ -1544,6 +1539,9 @@ protected AddressResolver createAddressResolver(List<Address> addresses) {
public ConnectionFactory clone() {
try {
ConnectionFactory clone = (ConnectionFactory) super.clone();
if (this.nettyConf != null) {
clone.nettyConf = this.nettyConf.copy(clone);
}
return clone;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
Expand Down Expand Up @@ -1908,6 +1906,16 @@ private NettyConfiguration(ConnectionFactory cf) {
this.cf = cf;
}

private NettyConfiguration copy(ConnectionFactory cf) {
NettyConfiguration copy = new NettyConfiguration(cf);
copy.eventLoopGroup = this.eventLoopGroup;
copy.channelCustomizer = this.channelCustomizer;
copy.bootstrapCustomizer = this.bootstrapCustomizer;
copy.sslContextFactory = this.sslContextFactory;
copy.enqueuingTimeout = this.enqueuingTimeout;
return copy;
}

/**
* Set the {@link io.netty.channel.EventLoopGroup} shared by all the connections created with
* this connection factory.
Expand Down
45 changes: 45 additions & 0 deletions src/test/java/com/rabbitmq/client/test/ConnectionFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,49 @@ void setUriThrowsIllegalArgumentExceptionForIncorrectUri() {
.hasMessageContaining("Invalid AMQP URI syntax (Illegal character in authority at index");
}

@Test
void nettyConfigurationChangesShouldApplyToSubsequentConnections() throws Exception {
TestConnectionFactory cf = new TestConnectionFactory();
cf.netty();
FrameHandlerFactory plainFhf = cf.createFrameHandlerFactory();
cf.netty().sslContextFactory(name -> { throw new IllegalStateException("first"); });
FrameHandlerFactory firstFhf = cf.createFrameHandlerFactory();
cf.netty().sslContextFactory(name -> { throw new IllegalStateException("second"); });
FrameHandlerFactory secondFhf = cf.createFrameHandlerFactory();

assertThat(firstFhf).isNotSameAs(plainFhf);
assertThat(secondFhf).isNotSameAs(firstFhf);
assertThatThrownBy(() -> firstFhf.create(new Address("localhost"), "c"))
.hasMessage("first");
assertThatThrownBy(() -> secondFhf.create(new Address("localhost"), "c"))
.hasMessage("second");
}

@Test
void cloneShouldNotShareNettyConfiguration() throws Exception {
TestConnectionFactory cf = new TestConnectionFactory();
cf.netty().sslContextFactory(name -> { throw new IllegalStateException("original"); });
TestConnectionFactory clone = (TestConnectionFactory) cf.clone();

assertThat(clone.netty()).isNotSameAs(cf.netty());
assertThat(clone.netty().connectionFactory()).isSameAs(clone);
assertThat(clone.isSSL()).isTrue();
assertThatThrownBy(() -> clone.createFrameHandlerFactory().create(new Address("localhost"), "c"))
.hasMessage("original");

clone.netty().sslContextFactory(name -> { throw new IllegalStateException("clone"); });
assertThatThrownBy(() -> cf.createFrameHandlerFactory().create(new Address("localhost"), "c"))
.hasMessage("original");
assertThatThrownBy(() -> clone.createFrameHandlerFactory().create(new Address("localhost"), "c"))
.hasMessage("clone");
}

private static class TestConnectionFactory extends ConnectionFactory {

@Override
public synchronized FrameHandlerFactory createFrameHandlerFactory() throws IOException {
return super.createFrameHandlerFactory();
}
}

}
Loading