@Override protected void waitForConnection() throws InterruptedException { super.waitForConnection(); try { channel.confirmSelect(); } catch (IOException e) { // should never happen - not important for the example scope e.printStackTrace(); } channel.addConfirmListener( new ConfirmListener() { @Override public void handleAck(long deliveryTag, boolean multiple) throws IOException { if (multiple) { ReliableProducer.this.removeItemsUpto(deliveryTag); } else { ReliableProducer.this.removeItem(deliveryTag); } } @Override public void handleNack(long deliveryTag, boolean multiple) throws IOException { if (multiple) { ReliableProducer.this.requeueItemsUpto(deliveryTag); } else { ReliableProducer.this.requeueItem(deliveryTag); } } }); }
@Override protected void waitForConnection() throws InterruptedException { super.waitForConnection(); try { channel.basicConsume( Constants.queue, false, new Consumer() { @Override public void handleCancel(String consumerTag) throws IOException { System.out.println("got handleCancel signal"); } @Override public void handleCancelOk(String consumerTag) { System.out.println("got handleCancelOk signal"); } @Override public void handleConsumeOk(String consumerTag) { System.out.println("got handleConsumeOK signal"); } @Override public void handleDelivery( String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { long messageId = Long.parseLong(properties.getMessageId()); if (worker != null) { // if the message is not a re-delivery, sure it is not a // retransmission if (!envelope.isRedeliver() || toBeWorked(messageId)) { try { worker.handle(new String(body)); // the message is ack'ed just after it has been // secured (handled, stored in database...) setAsWorked(messageId); channel.basicAck(envelope.getDeliveryTag(), false); } catch (WorkerException e) { // the message worker has reported an exception, // so the message // can not be considered to be handled properly, // so requeue it channel.basicReject(envelope.getDeliveryTag(), true); } } } } @Override public void handleRecoverOk(String consumerTag) { System.out.println("got recoverOK signal"); } @Override public void handleShutdownSignal(String consumerTag, ShutdownSignalException cause) { System.out.println("got shutdown signal"); } }); } catch (IOException e) { e.printStackTrace(); } }