Skip to content

Commit 8dea23b

Browse files
garyrussellartembilan
authored andcommitted
GH-1524: Fix ThreadChannelCF with Transactional
Resolves #1524 Channel was always physically closed. **cherry-pick to 2.4.x**
1 parent 9242967 commit 8dea23b

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,7 @@ private Channel createProxy(Channel channel, boolean transactional) {
307307
}
308308

309309
private void handleClose(Channel channel, boolean transactional) {
310-
311-
if (transactional && this.txChannels.get() == null ? true : this.channels.get() == null) {
310+
if ((transactional && this.txChannels.get() == null) || (!transactional && this.channels.get() == null)) {
312311
physicalClose(channel);
313312
}
314313
else {

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020-2021 the original author or authors.
2+
* Copyright 2020-2022 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.
@@ -110,6 +110,36 @@ void testBasic() throws Exception {
110110
.isFalse();
111111
}
112112

113+
@Test
114+
void testClose() throws Exception {
115+
ConnectionFactory rabbitConnectionFactory = new ConnectionFactory();
116+
rabbitConnectionFactory.setHost("localhost");
117+
ThreadChannelConnectionFactory tccf = new ThreadChannelConnectionFactory(rabbitConnectionFactory);
118+
Connection conn = tccf.createConnection();
119+
Channel chann1 = conn.createChannel(false);
120+
Channel targetChannel1 = ((ChannelProxy) chann1).getTargetChannel();
121+
chann1.close();
122+
Channel chann2 = conn.createChannel(false);
123+
Channel targetChannel2 = ((ChannelProxy) chann2).getTargetChannel();
124+
assertThat(chann2).isSameAs(chann1);
125+
assertThat(targetChannel2).isSameAs(targetChannel1);
126+
}
127+
128+
@Test
129+
void testTxClose() throws Exception {
130+
ConnectionFactory rabbitConnectionFactory = new ConnectionFactory();
131+
rabbitConnectionFactory.setHost("localhost");
132+
ThreadChannelConnectionFactory tccf = new ThreadChannelConnectionFactory(rabbitConnectionFactory);
133+
Connection conn = tccf.createConnection();
134+
Channel chann1 = conn.createChannel(true);
135+
Channel targetChannel1 = ((ChannelProxy) chann1).getTargetChannel();
136+
chann1.close();
137+
Channel chann2 = conn.createChannel(true);
138+
Channel targetChannel2 = ((ChannelProxy) chann2).getTargetChannel();
139+
assertThat(chann2).isSameAs(chann1);
140+
assertThat(targetChannel2).isSameAs(targetChannel1);
141+
}
142+
113143
@Test
114144
void queueDeclared(@Autowired RabbitAdmin admin, @Autowired Config config,
115145
@Autowired ThreadChannelConnectionFactory tccf) throws Exception {

0 commit comments

Comments
 (0)