@Override public void handleDelivery(final String consumerTag, final Envelope envelope, final BasicProperties properties, final byte[] body) throws IOException { if (!autoAcknowledge && closed) { channel.basicReject(envelope.getDeliveryTag(), true); return; } try { responseQueue.put(new GetResponse(envelope, properties, body, Integer.MAX_VALUE)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } };
break; case REJECT: this.ackInfo.getChannel().basicReject(deliveryTag, false); break; case REQUEUE: this.ackInfo.getChannel().basicReject(deliveryTag, true); break; default:
@Override public void basicReject(long deliveryTag, boolean requeue) throws IOException { this.delegate.basicReject(deliveryTag, requeue); }
@Override public void basicReject(long deliveryTag, boolean requeue) throws IOException { this.delegate.basicReject(deliveryTag, requeue); }
@Override public void processReject(long tag) { try { channel.basicReject(tag, false); // Do NOT count down for messages we are going to reject. // latch.countDown(); } catch (IOException e) { e.printStackTrace(); } }
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); }
public void deadLetter(Long msgId) throws Exception { try { channel.basicReject(msgId, false); } catch (ShutdownSignalException sse) { reset(); String msg = "shutdown signal received while attempting to fail with no redelivery"; log.error(msg, sse); throw new Exception(msg, sse); } catch (Exception e) { String msg = "could not fail with dead-lettering (when configured) for msgId: " + msgId; log.error(msg, e); throw new Exception(msg, e); } } }
public void failWithRedelivery(Long msgId) throws Exception { try { channel.basicReject(msgId, true); } catch (ShutdownSignalException sse) { reset(); String msg = "shutdown signal received while attempting to fail with redelivery"; log.error(msg, sse); throw new Exception(msg, sse); } catch (Exception e) { String msg = "could not fail with redelivery for msgId: " + msgId; log.error(msg, e); throw new Exception(msg, e); } }
/** * Tells the AMQP broker to drop (Basic.Reject) the message. * * <p><strong>N.B.</strong> this does <em>not</em> requeue the message: * failed messages will simply be dropped. This is to prevent infinite * redelivery in the event of non-transient failures (e.g. malformed * messages). However it means that messages will <em>not</em> be retried * in the event of transient failures.</p> * * <p><strong>TODO</strong> make this configurable.</p> */ @Override public void fail(Object msgId) { if (msgId instanceof Long) { final long deliveryTag = (Long) msgId; if (amqpChannel != null) { try { amqpChannel.basicReject(deliveryTag, false /* don't requeue */); } catch (IOException e) { log.warn("Failed to reject delivery-tag " + deliveryTag, e); } } } else { log.warn(String.format("don't know how to reject(%s: %s)", msgId.getClass().getName(), msgId)); } }
private void rejectMessage(long deliveryTag) { try { channel.basicReject(deliveryTag, requeuing); } catch (IOException ex) { logger.error("Error rejecting message from {}: {}", this, ex); counterGroup.incrementAndGet(COUNTER_EXCEPTION); } counterGroup.incrementAndGet(COUNTER_REJECT); }
public void deadLetter(Long msgId) { reinitIfNecessary(); try { channel.basicReject(msgId, false); } catch (ShutdownSignalException sse) { reset(); logger.error("Shutdown signal received while attempting to fail with no redelivery.", sse); reporter.reportError(sse); } catch (Exception e) { logger.error("Could not fail with dead-lettering (when configured) for msgId: " + msgId, e); reporter.reportError(e); } }
public void failWithRedelivery(Long msgId) { reinitIfNecessary(); try { channel.basicReject(msgId, true); } catch (ShutdownSignalException sse) { reset(); logger.error("shutdown signal received while attempting to fail with redelivery", sse); reporter.reportError(sse); } catch (Exception e) { logger.error("could not fail with redelivery for msgId: " + msgId, e); reporter.reportError(e); } }
public void failWithRedelivery(Long msgId) { reinitIfNecessary(); try { channel.basicReject(msgId, true); } catch (ShutdownSignalException sse) { reset(); logger.error("Shutdown signal received while attempting to fail with redelivery.", sse); reporter.reportError(sse); } catch (Exception e) { logger.error("Could not fail with redelivery for msgId: " + msgId, e); reporter.reportError(e); } }
public void deadLetter(Long msgId) { reinitIfNecessary(); try { channel.basicReject(msgId, false); } catch (ShutdownSignalException sse) { reset(); logger.error("shutdown signal received while attempting to fail with no redelivery", sse); reporter.reportError(sse); } catch (Exception e) { logger.error("could not fail with dead-lettering (when configured) for msgId: " + msgId, e); reporter.reportError(e); } }
public void rollbackAll() { for (Channel channel : this.channels) { if (logger.isDebugEnabled()) { logger.debug("Rolling back messages to channel: " + channel); } RabbitUtils.rollbackIfNecessary(channel); if (this.deliveryTags.containsKey(channel)) { for (Long deliveryTag : this.deliveryTags.get(channel)) { try { channel.basicReject(deliveryTag, this.requeueOnRollback); } catch (IOException ex) { throw new AmqpIOException(ex); } } // Need to commit the reject (=nack) RabbitUtils.commitIfNecessary(channel); } } }
public void rollbackAll() { for (Channel channel : this.channels) { if (logger.isDebugEnabled()) { logger.debug("Rolling back messages to channel: " + channel); } RabbitUtils.rollbackIfNecessary(channel); if (this.deliveryTags.containsKey(channel)) { for (Long deliveryTag : this.deliveryTags.get(channel)) { try { channel.basicReject(deliveryTag, this.requeueOnRollback); } catch (IOException ex) { throw new AmqpIOException(ex); } } // Need to commit the reject (=nack) RabbitUtils.commitIfNecessary(channel); } } }
@Override public void onMessage(Message message, Channel channel) throws Exception { MessageProperties messageProperties = message.getMessageProperties(); Long deliveryTag = messageProperties.getDeliveryTag(); Long consumerCount = redisTemplate.opsForHash().increment(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId(), 1); logger.info("收到消息,当前消息ID:{} 消费次数:{}", messageProperties.getMessageId(), consumerCount); try { receiveMessage(message); // 成功的回执 channel.basicAck(deliveryTag, false); // 如果消费成功,将Redis中统计消息消费次数的缓存删除 redisTemplate.opsForHash().delete(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId()); } catch (Exception e) { logger.error("RabbitMQ 消息消费失败," + e.getMessage(), e); if (consumerCount >= MQConstants.MAX_CONSUMER_COUNT) { // 入死信队列 channel.basicReject(deliveryTag, false); } else { // 重回到队列,重新消费, 按照2的指数级递增 Thread.sleep((long) (Math.pow(MQConstants.BASE_NUM, consumerCount)*1000)); redisTemplate.opsForHash().increment(MQConstants.MQ_CONSUMER_RETRY_COUNT_KEY, messageProperties.getMessageId(), 1); channel.basicNack(deliveryTag, false, true); } } }
break; case REJECT: this.ackInfo.getChannel().basicReject(deliveryTag, false); break; case REQUEUE: this.ackInfo.getChannel().basicReject(deliveryTag, true); break; default:
@Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { RabbitMessage msg = RabbitMessage.newMessage().body(body) .properties(properties) .redeliver(envelope.isRedeliver()); long deliveryTag = envelope.getDeliveryTag(); _self.onRabbitReceive(msg, (Void, e) -> { if (e != null) { _channel.basicReject(deliveryTag, false); } else { _channel.basicAck(deliveryTag, false); } }); } });
rejectLatch.countDown(); return null; }).given(channel).basicReject(anyLong(), anyBoolean()); willAnswer(invocation -> { rejectLatch.countDown(); verify(channel).basicReject(anyLong(), eq(expectRequeue));