private void testNackOrRequeue(boolean requeue) throws IOException, TimeoutException { Channel channel = mock(Channel.class); willReturn(true).given(channel).isOpen(); Envelope envelope = new Envelope(123L, false, "ex", "rk"); BasicProperties props = new BasicProperties.Builder().build(); GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0); willReturn(getResponse).given(channel).basicGet("foo", false); Connection connection = mock(Connection.class); willReturn(true).given(connection).isOpen(); willReturn(channel).given(connection).createChannel(); ConnectionFactory connectionFactory = mock(ConnectionFactory.class); willReturn(connection).given(connectionFactory).newConnection((ExecutorService) isNull(), anyString()); CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory); AmqpMessageSource source = new AmqpMessageSource(ccf, "foo"); Message<?> received = source.receive(); verify(connection).createChannel(); StaticMessageHeaderAccessor.getAcknowledgmentCallback(received) .acknowledge(requeue ? Status.REQUEUE : Status.REJECT); verify(channel).basicReject(123L, requeue); verify(connection).createChannel(); ccf.destroy(); verify(channel).close(); verify(connection).close(30000); }
Channel channel = mock(Channel.class); willReturn(true).given(channel).isOpen(); Envelope envelope = new Envelope(123L, false, "ex", "rk"); BasicProperties props = new BasicProperties.Builder().build(); GetResponse getResponse = new GetResponse(envelope, props, "bar".getBytes(), 0);
private Envelope envelope(long tag) { return new Envelope(tag, false, "", ""); }
public GetResponse basicGet(boolean autoAck, Supplier<Long> deliveryTagSupplier) { long deliveryTag = deliveryTagSupplier.get(); Message message = messages.poll(); if (message != null) { if (message.isExpired()) { deadLetter(message); return null; } else { if (!autoAck) { unackedMessagesByDeliveryTag.put(deliveryTag, message); } Envelope envelope = new Envelope( deliveryTag, false, message.exchangeName, message.routingKey); LOGGER.debug(localized("basic_get a message")); return new GetResponse( envelope, message.props, message.body, messages.size()); } } else { LOGGER.debug(localized("basic_get no message available")); return null; } }
AbstractAdaptableMessageListener adapter = (AbstractAdaptableMessageListener) listener; willAnswer(i -> { Envelope envelope = new Envelope(1, false, "", REPLY_QUEUE); reply.set(MessageBuilder.withBody(i.getArgument(4)) // NOSONAR magic # .andProperties(getMessagePropertiesConverter()
AbstractAdaptableMessageListener adapter = (AbstractAdaptableMessageListener) listener; willAnswer(i -> { Envelope envelope = new Envelope(1, false, "", REPLY_QUEUE); reply.set(MessageBuilder.withBody(i.getArgument(4)) // NOSONAR magic # .andProperties(getMessagePropertiesConverter()
unackedMessagesByDeliveryTag.put(deliveryTag, message); Envelope envelope = new Envelope(deliveryTag, false, message.exchangeName,
if (confirm != null) { MessageProperties messageProperties = converter.toMessageProperties(properties, new Envelope(0L, false, exchange, routingKey), StandardCharsets.UTF_8.name()); if (confirm.getCorrelationData() != null) { confirm.getCorrelationData().setReturnedMessage(new Message(body, messageProperties)); // NOSONAR never null
if (confirm != null) { MessageProperties messageProperties = converter.toMessageProperties(properties, new Envelope(0L, false, exchange, routingKey), StandardCharsets.UTF_8.name()); if (confirm.getCorrelationData() != null) { confirm.getCorrelationData().setReturnedMessage(new Message(body, messageProperties)); // NOSONAR never null
@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { if (closed) return; long deliveryTag = envelope.getDeliveryTag(); channelHandler.maxDeliveryTag = deliveryTag = deliveryTag + channelHandler.previousMaxDeliveryTag; delegate.handleDelivery( consumerTag, new Envelope(deliveryTag, envelope.isRedeliver(), envelope.getExchange(), envelope.getRoutingKey()), properties, body); }
@Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { if (closed) return; long deliveryTag = envelope.getDeliveryTag(); channelHandler.maxDeliveryTag = deliveryTag = deliveryTag + channelHandler.previousMaxDeliveryTag; delegate.handleDelivery( consumerTag, new Envelope(deliveryTag, envelope.isRedeliver(), envelope.getExchange(), envelope.getRoutingKey()), properties, body); }
"messageHeaders", headers); unackedMessages.put(internalDeliverTag, System.currentTimeMillis()); final Envelope internalEnvelope = new Envelope(internalDeliverTag, envelope.isRedeliver(), envelope.getExchange(), envelope.getRoutingKey()); Acknowledger acknowledger = createAcknowledger(channel, internalEnvelope, headers, body); Message message = new Message(acknowledger, internalEnvelope, headers, body);
BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); envelope = new Envelope(4L, false, "foo", "bar"); consumer.get().handleDelivery("1", envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS));
verify(channel).basicCancel("consumerTag"); Envelope envelope = new Envelope(1, false, "foo", "bar"); BasicProperties props = mock(BasicProperties.class); consumer.handleDelivery("consumerTag", envelope, props, new byte[0]); envelope = new Envelope(2, false, "foo", "bar"); consumer.handleDelivery("consumerTag", envelope, props, new byte[0]); assertThat(TestUtils.getPropertyValue(blockingQueueConsumer, "queue", BlockingQueue.class).size(), equalTo(2)); envelope = new Envelope(3, false, "foo", "bar"); consumer.handleDelivery("consumerTag", envelope, props, new byte[0]); assertThat(TestUtils.getPropertyValue(blockingQueueConsumer, "queue", BlockingQueue.class).size(), equalTo(0));
BasicProperties props = new BasicProperties(); byte[] payload = "baz".getBytes(); Envelope envelope = new Envelope(1L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); envelope = new Envelope(2L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); envelope = new Envelope(3L, false, "foo", "bar"); consumer.get().handleDelivery(consumerTag, envelope, props, payload); assertTrue(latch.await(5, TimeUnit.SECONDS));
@Test // AMQP-249 public void dontHangConsumerThread() throws Exception { ConnectionFactory mockConnectionFactory = mock(ConnectionFactory.class); Connection mockConnection = mock(Connection.class); Channel mockChannel = mock(Channel.class); given(mockConnectionFactory.newConnection(any(ExecutorService.class), anyString())).willReturn(mockConnection); given(mockConnection.isOpen()).willReturn(true); given(mockConnection.createChannel()).willReturn(mockChannel); given(mockChannel.queueDeclare()).willReturn(new AMQImpl.Queue.DeclareOk("foo", 0, 0)); final AtomicReference<Consumer> consumer = new AtomicReference<Consumer>(); willAnswer(invocation -> { consumer.set(invocation.getArgument(6)); return null; }).given(mockChannel).basicConsume(anyString(), anyBoolean(), anyString(), anyBoolean(), anyBoolean(), isNull(), any(Consumer.class)); SingleConnectionFactory connectionFactory = new SingleConnectionFactory(mockConnectionFactory); connectionFactory.setExecutor(mock(ExecutorService.class)); RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setReplyTimeout(1); Message input = new Message("Hello, world!".getBytes(), new MessageProperties()); template.doSendAndReceiveWithTemporary("foo", "bar", input, null); Envelope envelope = new Envelope(1, false, "foo", "bar"); // used to hang here because of the SynchronousQueue and doSendAndReceive() already exited consumer.get().handleDelivery("foo", envelope, new AMQP.BasicProperties(), new byte[0]); }
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS)); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 });
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS)); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] {0});
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS)); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] {0});
assertTrue(consumerLatch.await(10, TimeUnit.SECONDS)); consumer.get().handleDelivery("qux", new Envelope(1, false, "foo", "bar"), new BasicProperties(), new byte[] { 0 });