@Override public void close() throws TimeoutException, IOException { closed = true; GetResponse lastMessage = null; GetResponse response; while ((response = responseQueue.poll()) != null) { lastMessage = response; } if (lastMessage != null) { getChannel().basicNack(lastMessage.getEnvelope().getDeliveryTag(), true, true); } }
@Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { long deliveryTag = envelope.getDeliveryTag(); try { totalBytesRead.addAndGet(body.length); lastSecBytesReadTmp.addAndGet(body.length); final RawMessage rawMessage = new RawMessage(body); // TODO figure out if we want to unsubscribe after a certain time, or if simply blocking is enough here if (amqpTransport.isThrottled()) { amqpTransport.blockUntilUnthrottled(); } sourceInput.processRawMessage(rawMessage); channel.basicAck(deliveryTag, false); } catch (Exception e) { LOG.error("Error while trying to process AMQP message", e); if (channel.isOpen()) { channel.basicNack(deliveryTag, false, requeueInvalid); if (LOG.isDebugEnabled()) { if (requeueInvalid) { LOG.debug("Re-queue message with delivery tag {}", deliveryTag); } else { LOG.debug("Message with delivery tag {} not re-queued", deliveryTag); } } } } } });
@Override public void basicNack(long deliveryTag, boolean multiple, boolean requeue) throws IOException { this.delegate.basicNack(deliveryTag, multiple, requeue); }
@Override public void basicNack(long deliveryTag, boolean multiple, boolean requeue) throws IOException { this.delegate.basicNack(deliveryTag, multiple, requeue); }
public void nackMessage(long deliveryTag) { try { channel.basicNack(deliveryTag, false, true); } catch (Exception ignored) { } } }
public void nackMessage(long deliveryTag) { try { channel.basicNack(deliveryTag, false, true); } catch (Exception ignored) { } } }
public void nackMessage(long deliveryTag) { try { channel.basicNack(deliveryTag, false, true); } catch (Exception ignored) { } } }
public void nackMessage(long deliveryTag) { try { channel.basicNack(deliveryTag, false, true); } catch (Exception ignored) { } } }
@Override public void basicNack(long deliveryTag, boolean multiple) throws IOException { delegate.basicNack(deliveryTag, multiple, false); }
@ServiceActivator(inputChannel = "foo", outputChannel = "bar") public Integer handle(@Payload String payload, @Header(AmqpHeaders.CHANNEL) Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) Long deliveryTag) throws Exception { if (++called > 2) { channel.basicAck(deliveryTag, false); } else { channel.basicNack(deliveryTag, false, true); } return called; }
@Override public void basicNack(long deliveryTag, boolean multiple, boolean requeue, Handler<AsyncResult<JsonObject>> resultHandler) { forChannel(resultHandler, (channel) -> { channel.basicNack(deliveryTag, multiple, requeue); return null; }); }
private void asyncFailure(Message request, Channel channel, Throwable t) { this.logger.error("Future was completed with an exception for " + request, t); try { channel.basicNack(request.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { this.logger.error("Failed to nack message", e); } }
private void asyncFailure(Message request, Channel channel, Throwable t) { this.logger.error("Future was completed with an exception for " + request, t); try { channel.basicNack(request.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e) { this.logger.error("Failed to nack message", e); } }
void explicitNack(long deliveryTag) { if (this.enterCommittingBlock()) { try { this.channel.basicNack(deliveryTag, false, true); } catch (Exception x) { // TODO logging impl debug message this.logger.warn("Cannot reject/requeue message received (dTag={})", deliveryTag, x); // this is fine. we didn't ack the message in the first place } finally { this.leaveCommittingBlock(); } } }
/** * Asserts that the given queue DOES contain * at least one message. * * @param queue The queue name * @throws IOException */ public void queueNotEmtpy(String queue) throws IOException { GetResponse response = channel.basicGet(queue, false); Assert.assertNotNull(response); channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true); }
/** * Asserts that the given queue DOES NOT contain * any messages. * * @param queue The queue name * @throws IOException */ public void queueEmtpy(String queue) throws IOException { GetResponse response = channel.basicGet(queue, false); if (response != null) { channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true); } Assert.assertNull(response); }
/** * <p>Asserts that the first message in the queue equals * the given message.</p> * * @param queue The queue name * @param message The message to assert being head of queue * @throws IOException */ public void messageInQueue(String queue, String message) throws IOException { GetResponse response = channel.basicGet(queue, false); Assert.assertNotNull(response); try { Assert.assertEquals(message, new String(response.getBody(), "UTF-8")); } finally { channel.basicNack(response.getEnvelope().getDeliveryTag(), false, true); } }
@RabbitHandler public void process(String context, Message message, Channel channel) { logger.info("HelloReceiver 监听到消息内容:{}", context); try { //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发 channel.basicAck(message.getMessageProperties().getDeliveryTag(), false); //消息确认 因为我在属性配置文件里面开启了ACK确认 所以如果代码没有执行ACK确认 你在RabbitMQ的后台会看到消息会一直留在队列里面未消费掉 只要程序一启动开始接受该队列消息的时候 又会收到 logger.info("HelloReceiver 消息接收成功"); } catch (Exception e) { e.printStackTrace(); logger.info("HelloReceiver 消息接收失败"); // ack返回false,并重新放回队列 try { logger.info("HelloReceiver ack返回false,并重新放回队列"); channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true); } catch (IOException e1) { e1.printStackTrace(); } } }
@Override public void nack() { try { if (lastMessage != null) { final long deliveryTag = lastMessage.getEnvelope().getDeliveryTag(); final boolean multiple = true; final boolean requeue = true; channel.basicNack(deliveryTag, multiple, requeue); } } catch (final IOException e) { throw new JocoteException(e); } finally { closable = true; } }
private void testRequeueOrNotGuts(Exception ex, boolean expectedRequeue, Channel channel, BlockingQueueConsumer blockingQueueConsumer) throws Exception { DirectFieldAccessor dfa = new DirectFieldAccessor(blockingQueueConsumer); dfa.setPropertyValue("channel", channel); Set<Long> deliveryTags = new HashSet<Long>(); deliveryTags.add(1L); dfa.setPropertyValue("deliveryTags", deliveryTags); blockingQueueConsumer.rollbackOnExceptionIfNecessary(ex); verify(channel).basicNack(1L, true, expectedRequeue); }