private synchronized HandleStatus handle(final MessageReference reference, final Consumer consumer) { HandleStatus status; try { status = consumer.handle(reference); } catch (Throwable t) { ActiveMQServerLogger.LOGGER.removingBadConsumer(t, consumer, reference); // If the consumer throws an exception we remove the consumer try { removeConsumer(consumer); } catch (Exception e) { ActiveMQServerLogger.LOGGER.errorRemovingConsumer(e); } return HandleStatus.BUSY; } if (status == null) { throw new IllegalStateException("ClientConsumer.handle() should never return null"); } return status; }
/** This will print errors and decide what to do with the errored consumer from the protocol layer. */ @Override public void errorProcessing(Consumer consumer, Throwable t, MessageReference reference) { synchronized (this) { ActiveMQServerLogger.LOGGER.removingBadConsumer(t, consumer, reference); // If the consumer throws an exception we remove the consumer try { removeConsumer(consumer); } catch (Exception e) { ActiveMQServerLogger.LOGGER.errorRemovingConsumer(e); } // The message failed to be delivered, hence we try again addHead(reference, false); } }
@Test public void testAddRemoveConsumer() throws Exception { Consumer cons1 = new FakeConsumer(); Consumer cons2 = new FakeConsumer(); Consumer cons3 = new FakeConsumer(); QueueImpl queue = getTemporaryQueue(); Assert.assertEquals(0, queue.getConsumerCount()); queue.addConsumer(cons1); Assert.assertEquals(1, queue.getConsumerCount()); queue.removeConsumer(cons1); Assert.assertEquals(0, queue.getConsumerCount()); queue.addConsumer(cons1); queue.addConsumer(cons2); queue.addConsumer(cons3); Assert.assertEquals(3, queue.getConsumerCount()); queue.removeConsumer(new FakeConsumer()); Assert.assertEquals(3, queue.getConsumerCount()); queue.removeConsumer(cons1); Assert.assertEquals(2, queue.getConsumerCount()); queue.removeConsumer(cons2); Assert.assertEquals(1, queue.getConsumerCount()); queue.removeConsumer(cons3); Assert.assertEquals(0, queue.getConsumerCount()); queue.removeConsumer(cons3); }
queue.removeConsumer(cons1); queue.removeConsumer(cons3);
queue.removeConsumer(consumer);
queue.acknowledge(ref6); queue.removeConsumer(consumer);