Skip to content

Is unconditional acknowledgement in tutorial two the right thing to do? #254

Open
@dvdsk

Description

On the tutorials page (java) at https://www.rabbitmq.com/tutorials/tutorial-two-java.html there is an example of how to use Acknowledging. In this example the ack is always send. Shouldn't it be send only if the work succeeds? (I am a beginner at java however I think the finally branch always runs)

channel.basicQos(1); // accept only one unack-ed message at a time (see below)

DeliverCallback deliverCallback = (consumerTag, delivery) -> {
  String message = new String(delivery.getBody(), "UTF-8");

  System.out.println(" [x] Received '" + message + "'");
  try {
    doWork(message);
  } finally {
    System.out.println(" [x] Done");
    channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
  }
};
boolean autoAck = false;
channel.basicConsume(TASK_QUEUE_NAME, autoAck, deliverCallback, consumerTag -> { });

I would solve this by taking the code out of the finally block and adding a catch block. Then escape the entire function on an exception in doWork(message).

Apologies if this should have been posted in another repo, I could not find from which repo the above code example originated.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions