Skip to content

Commit d900867

Browse files
committed
Merge branch '4.x.x-stable' into 5.1.x-stable
Conflicts: src/test/java/com/rabbitmq/client/test/ClientTests.java
2 parents 44c9e8d + fe5b196 commit d900867

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
@@ -50,25 +50,36 @@ public void handleConsumerException(Channel channel, Throwable exception,
5050
Consumer consumer, String consumerTag,
5151
String methodName)
5252
{
53-
handleChannelKiller(channel, exception, "Consumer " + consumer
54-
+ " (" + consumerTag + ")"
55-
+ " method " + methodName
56-
+ " for channel " + channel);
53+
String logMessage = "Consumer " + consumer
54+
+ " (" + consumerTag + ")"
55+
+ " method " + methodName
56+
+ " for channel " + channel;
57+
String closeMessage = "Consumer"
58+
+ " (" + consumerTag + ")"
59+
+ " method " + methodName
60+
+ " for channel " + channel;
61+
handleChannelKiller(channel, exception, logMessage, closeMessage);
5762
}
5863

5964
@Override
6065
protected void handleChannelKiller(Channel channel, Throwable exception, String what) {
61-
log(what + " threw an exception for channel " + channel, exception);
66+
handleChannelKiller(channel, exception, what, what);
67+
}
68+
69+
protected void handleChannelKiller(Channel channel, Throwable exception, String logMessage, String closeMessage) {
70+
log(logMessage + " threw an exception for channel " + channel, exception);
6271
try {
63-
channel.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + what);
72+
channel.close(AMQP.REPLY_SUCCESS, "Closed due to exception from " + closeMessage);
6473
} catch (AlreadyClosedException ace) {
6574
// noop
6675
} catch (TimeoutException ace) {
6776
// noop
6877
} catch (IOException ioe) {
6978
log("Failure during close of channel " + channel + " after " + exception, ioe);
70-
channel.getConnection().abort(AMQP.INTERNAL_ERROR, "Internal error closing channel for " + what);
79+
channel.getConnection().abort(AMQP.INTERNAL_ERROR, "Internal error closing channel for " + closeMessage);
7180
}
7281
}
7382

83+
84+
7485
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
FrameBuilderTest.class,
5858
PropertyFileInitialisationTest.class,
5959
ClientVersionTest.class,
60-
TestUtilsTest.class
60+
TestUtilsTest.class,
61+
StrictExceptionHandlerTest.class
6162
})
6263
public class ClientTests {
6364

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+
// info@rabbitmq.com.
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)