|
| 1 | +package com.rabbitmq.client.test.functional; |
| 2 | + |
| 3 | +import com.rabbitmq.client.AMQP; |
| 4 | +import com.rabbitmq.client.Channel; |
| 5 | +import com.rabbitmq.client.Connection; |
| 6 | +import com.rabbitmq.client.ConnectionFactory; |
| 7 | +import com.rabbitmq.client.Consumer; |
| 8 | +import com.rabbitmq.client.DefaultConsumer; |
| 9 | +import com.rabbitmq.client.Envelope; |
| 10 | +import com.rabbitmq.client.ExceptionHandler; |
| 11 | +import com.rabbitmq.client.impl.DefaultExceptionHandler; |
| 12 | +import junit.framework.TestCase; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | +import java.util.concurrent.CountDownLatch; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | + |
| 18 | +public class ExceptionHandling extends TestCase { |
| 19 | + private ConnectionFactory newConnectionFactory(ExceptionHandler eh) { |
| 20 | + ConnectionFactory cf = new ConnectionFactory(); |
| 21 | + cf.setExceptionHandler(eh); |
| 22 | + return cf; |
| 23 | + } |
| 24 | + |
| 25 | + public void testHandleConsumerException() throws IOException, InterruptedException { |
| 26 | + final CountDownLatch latch = new CountDownLatch(1); |
| 27 | + final DefaultExceptionHandler eh = new DefaultExceptionHandler() { |
| 28 | + @Override |
| 29 | + public void handleConsumerException(Channel channel, Throwable exception, Consumer consumer, String consumerTag, String methodName) { |
| 30 | + latch.countDown(); |
| 31 | + } |
| 32 | + }; |
| 33 | + ConnectionFactory cf = newConnectionFactory(eh); |
| 34 | + assertEquals(cf.getExceptionHandler(), eh); |
| 35 | + Connection conn = cf.newConnection(); |
| 36 | + assertEquals(conn.getExceptionHandler(), eh); |
| 37 | + Channel ch = conn.createChannel(); |
| 38 | + String q = ch.queueDeclare().getQueue(); |
| 39 | + ch.basicConsume(q, new DefaultConsumer(ch) { |
| 40 | + @Override |
| 41 | + public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { |
| 42 | + throw new RuntimeException("oops"); |
| 43 | + } |
| 44 | + }); |
| 45 | + ch.basicPublish("", q, null, "".getBytes()); |
| 46 | + wait(latch); |
| 47 | + } |
| 48 | + |
| 49 | + private void wait(CountDownLatch latch) throws InterruptedException { |
| 50 | + latch.await(30, TimeUnit.MINUTES); |
| 51 | + } |
| 52 | +} |
0 commit comments