Skip to content

Commit fe5b196

Browse files
Merge pull request #347 from rabbitmq/rabbitmq-java-client-344-too-long-closing-message
Avoid too long closing message in StrictExceptionHandler
2 parents 9b0f3fa + 5b38953 commit fe5b196

File tree

3 files changed

+103
-8
lines changed

3 files changed

+103
-8
lines changed

src/main/java/com/rabbitmq/client/impl/StrictExceptionHandler.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,25 +55,36 @@ public void handleConsumerException(Channel channel, Throwable exception,
5555
Consumer consumer, String consumerTag,
5656
String methodName)
5757
{
58-
handleChannelKiller(channel, exception, "Consumer " + consumer
59-
+ " (" + consumerTag + ")"
60-
+ " method " + methodName
61-
+ " for channel " + channel);
58+
String logMessage = "Consumer " + consumer
59+
+ " (" + consumerTag + ")"
60+
+ " method " + methodName
61+
+ " for channel " + channel;
62+
String closeMessage = "Consumer"
63+
+ " (" + consumerTag + ")"
64+
+ " method " + methodName
65+
+ " for channel " + channel;
66+
handleChannelKiller(channel, exception, logMessage, closeMessage);
6267
}
6368

6469
@Override
6570
protected void handleChannelKiller(Channel channel, Throwable exception, String what) {
66-
log(what + " threw an exception for channel " + channel, exception);
71+
handleChannelKiller(channel, exception, what, what);
72+
}
73+
74+
protected void handleChannelKiller(Channel channel, Throwable exception, String logMessage, String closeMessage) {
75+
log(logMessage + " threw an exception for channel " + channel, exception);
6776
try {
68-
channel.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + what);
77+
channel.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + closeMessage);
6978
} catch (AlreadyClosedException ace) {
7079
// noop
7180
} catch (TimeoutException ace) {
7281
// noop
7382
} catch (IOException ioe) {
7483
log("Failure during close of channel " + channel + " after " + exception, ioe);
75-
channel.getConnection().abort(AMQP.INTERNAL_ERROR, "Internal error closing channel for " + what);
84+
channel.getConnection().abort(AMQP.INTERNAL_ERROR, "Internal error closing channel for " + closeMessage);
7685
}
7786
}
7887

88+
89+
7990
}

src/test/java/com/rabbitmq/client/test/ClientTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
RecoveryDelayHandlerTest.class,
5454
FrameBuilderTest.class,
5555
PropertyFileInitialisationTest.class,
56-
ClientVersionTest.class
56+
ClientVersionTest.class,
57+
StrictExceptionHandlerTest.class
5758
})
5859
public class ClientTests {
5960

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) 2018-Present Pivotal Software, Inc. All rights reserved.
2+
//
3+
// This software, the RabbitMQ Java client library, is triple-licensed under the
4+
// Mozilla Public License 1.1 ("MPL"), the GNU General Public License version 2
5+
// ("GPL") and the Apache License version 2 ("ASL"). For the MPL, please see
6+
// LICENSE-MPL-RabbitMQ. For the GPL, please see LICENSE-GPL2. For the ASL,
7+
// please see LICENSE-APACHE2.
8+
//
9+
// This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND,
10+
// either express or implied. See the LICENSE file for specific language governing
11+
// rights and limitations of this software.
12+
//
13+
// If you have any questions regarding licensing, please contact us at
14+
15+
16+
package com.rabbitmq.client.test;
17+
18+
import com.rabbitmq.client.AMQP;
19+
import com.rabbitmq.client.Channel;
20+
import com.rabbitmq.client.Connection;
21+
import com.rabbitmq.client.ConnectionFactory;
22+
import com.rabbitmq.client.Consumer;
23+
import com.rabbitmq.client.DefaultConsumer;
24+
import com.rabbitmq.client.Envelope;
25+
import com.rabbitmq.client.impl.StrictExceptionHandler;
26+
import org.junit.Test;
27+
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.TimeUnit;
30+
31+
import static org.hamcrest.Matchers.is;
32+
import static org.junit.Assert.assertThat;
33+
import static org.junit.Assert.fail;
34+
35+
public class StrictExceptionHandlerTest {
36+
37+
@Test
38+
public void tooLongClosingMessage() throws Exception {
39+
ConnectionFactory cf = TestUtils.connectionFactory();
40+
final CountDownLatch latch = new CountDownLatch(1);
41+
cf.setExceptionHandler(new StrictExceptionHandler() {
42+
@Override
43+
public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) {
44+
try {
45+
super.handleConsumerException(channel, exception, consumer, consumerTag, methodName);
46+
} catch (IllegalArgumentException e) {
47+
fail("No exception should caught");
48+
}
49+
latch.countDown();
50+
}
51+
});
52+
Connection c = null;
53+
try {
54+
c = cf.newConnection();
55+
Channel channel = c.createChannel();
56+
String queue = channel.queueDeclare().getQueue();
57+
channel.basicConsume(queue,
58+
new VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName(
59+
channel
60+
));
61+
channel.basicPublish("", queue, null, new byte[0]);
62+
assertThat(latch.await(5, TimeUnit.SECONDS), is(true));
63+
} finally {
64+
if (c != null) {
65+
c.close();
66+
}
67+
}
68+
}
69+
70+
static class VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName
71+
extends DefaultConsumer {
72+
73+
public VeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongClassName(
74+
Channel channel) {
75+
super(channel);
76+
}
77+
78+
@Override
79+
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) {
80+
throw new RuntimeException();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)