protected void closeChannels(Collection<ChannelProxy> theChannels) { for (ChannelProxy channel : theChannels) { try { channel.close(); } catch (Exception ex) { logger.trace("Could not close cached Rabbit Channel", ex); } } }
/** * Add a listener if necessary so we can immediately close an autorecovered * channel if necessary since the actual consumer will no longer exist. * Idempotent operation. * @param channel the channel. */ public static void addRecoveryListenerIfNecessary(Channel channel) { AutorecoveringChannel autorecoveringChannel = null; if (channel instanceof ChannelProxy) { if (((ChannelProxy) channel).getTargetChannel() instanceof AutorecoveringChannel) { autorecoveringChannel = (AutorecoveringChannel) ((ChannelProxy) channel) .getTargetChannel(); } } else if (channel instanceof AutorecoveringChannel) { autorecoveringChannel = (AutorecoveringChannel) channel; } if (autorecoveringChannel != null && hasListener.putIfAbsent(autorecoveringChannel, Boolean.TRUE) == null) { autorecoveringChannel.addRecoveryListener(INSTANCE); } }
private ChannelProxy findOpenChannel(LinkedList<ChannelProxy> channelList, // NOSONAR LinkedList.removeFirst() ChannelProxy channelArg) { ChannelProxy channel = channelArg; synchronized (channelList) { while (!channelList.isEmpty()) { channel = channelList.removeFirst(); if (logger.isTraceEnabled()) { logger.trace(channel + " retrieved from cache"); } if (channel.isOpen()) { break; } else { cleanUpClosedChannel(channel); channel = null; } } } return channel; }
@Test public void testAlwaysCancelAutoRecoverConsumer() throws IOException { ConnectionFactory connectionFactory = mock(ConnectionFactory.class); Connection connection = mock(Connection.class); ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); when(channel.getTargetChannel()).thenReturn(rabbitChannel); when(connectionFactory.createConnection()).thenReturn(connection); when(connection.createChannel(anyBoolean())).thenReturn(channel); final AtomicBoolean isOpen = new AtomicBoolean(true); doReturn(isOpen.get()).when(channel).isOpen(); when(channel.queueDeclarePassive(anyString())) .then(invocation -> mock(AMQP.Queue.DeclareOk.class)); doAnswer(i -> { ((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag"); return "consumerTag"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); BlockingQueueConsumer blockingQueueConsumer = new BlockingQueueConsumer(connectionFactory, new DefaultMessagePropertiesConverter(), new ActiveObjectCounter<>(), AcknowledgeMode.AUTO, true, 2, "test"); blockingQueueConsumer.setDeclarationRetries(1); blockingQueueConsumer.setRetryDeclarationInterval(10); blockingQueueConsumer.setFailedDeclarationRetryInterval(10); blockingQueueConsumer.start(); verify(channel).basicQos(2); isOpen.set(false); blockingQueueConsumer.stop(); verify(channel).basicCancel("consumerTag"); }
assertTrue(txChannel.isTransactional()); verify(mockTxChannel).txSelect(); txChannel.close();
@Nullable private ConfirmListener addConfirmListener(@Nullable com.rabbitmq.client.ConfirmCallback acks, @Nullable com.rabbitmq.client.ConfirmCallback nacks, Channel channel) { ConfirmListener listener = null; if (acks != null && nacks != null && channel instanceof ChannelProxy && ((ChannelProxy) channel).isConfirmSelected()) { listener = channel.addConfirmListener(acks, nacks); } return listener; }
ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); when(channel.getTargetChannel()).thenReturn(rabbitChannel); doReturn(isOpen.get()).when(channel).isOpen(); when(channel.queueDeclarePassive(anyString())) .then(invocation -> mock(AMQP.Queue.DeclareOk.class)); doAnswer(i -> { ((Consumer) i.getArgument(6)).handleConsumeOk("consumerTag"); return "consumerTag"; }).when(channel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); blockingQueueConsumer.start(); verify(channel).basicQos(2); Consumer consumer = (Consumer) TestUtils.getPropertyValue(blockingQueueConsumer, "consumers", Map.class) .get("test"); isOpen.set(false); blockingQueueConsumer.stop(); verify(channel).basicCancel("consumerTag"); consumer.handleDelivery("consumerTag", envelope, props, new byte[0]); assertThat(TestUtils.getPropertyValue(blockingQueueConsumer, "queue", BlockingQueue.class).size(), equalTo(0)); verify(channel).basicNack(3, true, true); verify(channel, times(2)).basicCancel("consumerTag");
@Nullable private ConfirmListener addConfirmListener(@Nullable com.rabbitmq.client.ConfirmCallback acks, @Nullable com.rabbitmq.client.ConfirmCallback nacks, Channel channel) { ConfirmListener listener = null; if (acks != null && nacks != null && channel instanceof ChannelProxy && ((ChannelProxy) channel).isConfirmSelected()) { listener = channel.addConfirmListener(acks, nacks); } return listener; }
ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); given(channel.getTargetChannel()).willReturn(rabbitChannel); willAnswer(i -> isOpen.get()).given(channel).isOpen(); given(channel.queueDeclarePassive(Mockito.anyString())) .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); given(channel.basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class))).willReturn("consumerTag"); latch1.countDown(); return null; }).given(channel).basicQos(anyInt()); final CountDownLatch latch2 = new CountDownLatch(1); willAnswer(i -> { latch2.countDown(); return null; }).given(channel).basicCancel("consumerTag");
/** * Add a listener if necessary so we can immediately close an autorecovered * channel if necessary since the actual consumer will no longer exist. * Idempotent operation. * @param channel the channel. */ public static void addRecoveryListenerIfNecessary(Channel channel) { AutorecoveringChannel autorecoveringChannel = null; if (channel instanceof ChannelProxy) { if (((ChannelProxy) channel).getTargetChannel() instanceof AutorecoveringChannel) { autorecoveringChannel = (AutorecoveringChannel) ((ChannelProxy) channel) .getTargetChannel(); } } else if (channel instanceof AutorecoveringChannel) { autorecoveringChannel = (AutorecoveringChannel) channel; } if (autorecoveringChannel != null && hasListener.putIfAbsent(autorecoveringChannel, Boolean.TRUE) == null) { autorecoveringChannel.addRecoveryListener(INSTANCE); } }
protected void closeChannels(Collection<ChannelProxy> theChannels) { for (ChannelProxy channel : theChannels) { try { channel.close(); } catch (Exception ex) { logger.trace("Could not close cached Rabbit Channel", ex); } } }
private ChannelProxy findOpenChannel(LinkedList<ChannelProxy> channelList, // NOSONAR LinkedList.removeFirst() ChannelProxy channelArg) { ChannelProxy channel = channelArg; synchronized (channelList) { while (!channelList.isEmpty()) { channel = channelList.removeFirst(); if (logger.isTraceEnabled()) { logger.trace(channel + " retrieved from cache"); } if (channel.isOpen()) { break; } else { cleanUpClosedChannel(channel); channel = null; } } } return channel; }
private void setupConfirm(Channel channel, Message message, @Nullable CorrelationData correlationDataArg) { if ((this.publisherConfirms || this.confirmCallback != null) && channel instanceof PublisherCallbackChannel) { PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel; CorrelationData correlationData = this.correlationDataPostProcessor != null ? this.correlationDataPostProcessor.postProcess(message, correlationDataArg) : correlationDataArg; long nextPublishSeqNo = channel.getNextPublishSeqNo(); message.getMessageProperties().setPublishSequenceNumber(nextPublishSeqNo); publisherCallbackChannel.addPendingConfirm(this, nextPublishSeqNo, new PendingConfirm(correlationData, System.currentTimeMillis())); if (correlationData != null && StringUtils.hasText(correlationData.getId())) { message.getMessageProperties().setHeader(PublisherCallbackChannel.RETURNED_MESSAGE_CORRELATION_KEY, correlationData.getId()); } } else if (channel instanceof ChannelProxy && ((ChannelProxy) channel).isConfirmSelected()) { long nextPublishSeqNo = channel.getNextPublishSeqNo(); message.getMessageProperties().setPublishSequenceNumber(nextPublishSeqNo); } }
ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); given(channel.getTargetChannel()).willReturn(rabbitChannel); willAnswer(i -> isOpen.get()).given(channel).isOpen(); given(channel.queueDeclarePassive(Mockito.anyString())) .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); return "consumerTag"; }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); latch1.countDown(); return null; }).given(channel).basicQos(anyInt()); final CountDownLatch latch2 = new CountDownLatch(2); willAnswer(i -> { latch2.countDown(); return null; }).given(channel).basicCancel("consumerTag"); assertTrue(latch3.await(10, TimeUnit.SECONDS)); verify(channel, times(1)).basicConsume(eq("test1"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); verify(channel, times(2)).basicConsume(eq("test2"), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class));
private void closeChannelAfterIllegalArg(final Channel channel, Queue queue) { if (this.logger.isDebugEnabled()) { this.logger.error("Exception while declaring queue: '" + queue.getName() + "'"); } try { if (channel instanceof ChannelProxy) { ((ChannelProxy) channel).getTargetChannel().close(); } } catch (IOException | TimeoutException e1) { this.logger.error("Failed to close channel after illegal argument", e1); } }
private void setupConfirm(Channel channel, Message message, @Nullable CorrelationData correlationDataArg) { if ((this.publisherConfirms || this.confirmCallback != null) && channel instanceof PublisherCallbackChannel) { PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel; CorrelationData correlationData = this.correlationDataPostProcessor != null ? this.correlationDataPostProcessor.postProcess(message, correlationDataArg) : correlationDataArg; long nextPublishSeqNo = channel.getNextPublishSeqNo(); message.getMessageProperties().setPublishSequenceNumber(nextPublishSeqNo); publisherCallbackChannel.addPendingConfirm(this, nextPublishSeqNo, new PendingConfirm(correlationData, System.currentTimeMillis())); if (correlationData != null && StringUtils.hasText(correlationData.getId())) { message.getMessageProperties().setHeader(PublisherCallbackChannel.RETURNED_MESSAGE_CORRELATION_KEY, correlationData.getId()); } } else if (channel instanceof ChannelProxy && ((ChannelProxy) channel).isConfirmSelected()) { long nextPublishSeqNo = channel.getNextPublishSeqNo(); message.getMessageProperties().setPublishSequenceNumber(nextPublishSeqNo); } }
ChannelProxy channel = mock(ChannelProxy.class); Channel rabbitChannel = mock(AutorecoveringChannel.class); given(channel.getTargetChannel()).willReturn(rabbitChannel); given(channel.isOpen()).willReturn(true); given(channel.queueDeclarePassive(Mockito.anyString())) .willAnswer(invocation -> mock(AMQP.Queue.DeclareOk.class)); final AtomicReference<Consumer> consumer = new AtomicReference<>(); return "consumerTag"; }).given(channel) .basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), anyMap(), any(Consumer.class)); qos.set(i.getArgument(0)); return null; }).given(channel).basicQos(anyInt()); final CountDownLatch latch2 = new CountDownLatch(2); final CountDownLatch latch3 = new CountDownLatch(1); }).given(channel).basicAck(anyLong(), anyBoolean()); final CountDownLatch latch4 = new CountDownLatch(1); willAnswer(i -> { latch4.countDown(); return null; }).given(channel).basicNack(19L, true, true); verify(channel).basicAck(10L, true); verify(channel).basicAck(16L, true);
private void closeChannelAfterIllegalArg(final Channel channel, Queue queue) { if (this.logger.isDebugEnabled()) { this.logger.error("Exception while declaring queue: '" + queue.getName() + "'"); } try { if (channel instanceof ChannelProxy) { ((ChannelProxy) channel).getTargetChannel().close(); } } catch (IOException | TimeoutException e1) { this.logger.error("Failed to close channel after illegal argument", e1); } }
/** * Add this template as a confirms listener for the provided channel. * @param channel the channel. * @since 2.0 */ public void addListener(Channel channel) { if (channel instanceof PublisherCallbackChannel) { PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel; Channel key = channel instanceof ChannelProxy ? ((ChannelProxy) channel).getTargetChannel() : channel; if (this.publisherConfirmChannels.putIfAbsent(key, this) == null) { publisherCallbackChannel.addListener(this); if (logger.isDebugEnabled()) { logger.debug("Added publisher confirm channel: " + channel + " to map, size now " + this.publisherConfirmChannels.size()); } } } else { throw new IllegalStateException( "Channel does not support confirms or returns; " + "is the connection factory configured for confirms or returns?"); } }
/** * Add this template as a confirms listener for the provided channel. * @param channel the channel. * @since 2.0 */ public void addListener(Channel channel) { if (channel instanceof PublisherCallbackChannel) { PublisherCallbackChannel publisherCallbackChannel = (PublisherCallbackChannel) channel; Channel key = channel instanceof ChannelProxy ? ((ChannelProxy) channel).getTargetChannel() : channel; if (this.publisherConfirmChannels.putIfAbsent(key, this) == null) { publisherCallbackChannel.addListener(this); if (logger.isDebugEnabled()) { logger.debug("Added publisher confirm channel: " + channel + " to map, size now " + this.publisherConfirmChannels.size()); } } } else { throw new IllegalStateException( "Channel does not support confirms or returns; " + "is the connection factory configured for confirms or returns?"); } }