How to acknowledge message in Rabbitmq in a different channel
I have a query on RabbitMq consumer acknowledgement, I read documentation on RabbitMq stating that acknowledging the message should be on the same channel from which consumer received. But I'm in a situation where due to some reason consumer process is stopped after I received message and haven't acknowledge the Rabbitmq, when the consumer process is restarted, consumer starts getting unacknowledged messages from RabbitMq, but here consumer cannot send the acknowledge to those messages as I get a channel exception stating the tag doesn't belongs to the channel. So, my question is how to handle this scenario and how can I acknowledge the rabbitmq to remove the message after my consumer process is completed reading the message?
rabbitmq
add a comment |
I have a query on RabbitMq consumer acknowledgement, I read documentation on RabbitMq stating that acknowledging the message should be on the same channel from which consumer received. But I'm in a situation where due to some reason consumer process is stopped after I received message and haven't acknowledge the Rabbitmq, when the consumer process is restarted, consumer starts getting unacknowledged messages from RabbitMq, but here consumer cannot send the acknowledge to those messages as I get a channel exception stating the tag doesn't belongs to the channel. So, my question is how to handle this scenario and how can I acknowledge the rabbitmq to remove the message after my consumer process is completed reading the message?
rabbitmq
add a comment |
I have a query on RabbitMq consumer acknowledgement, I read documentation on RabbitMq stating that acknowledging the message should be on the same channel from which consumer received. But I'm in a situation where due to some reason consumer process is stopped after I received message and haven't acknowledge the Rabbitmq, when the consumer process is restarted, consumer starts getting unacknowledged messages from RabbitMq, but here consumer cannot send the acknowledge to those messages as I get a channel exception stating the tag doesn't belongs to the channel. So, my question is how to handle this scenario and how can I acknowledge the rabbitmq to remove the message after my consumer process is completed reading the message?
rabbitmq
I have a query on RabbitMq consumer acknowledgement, I read documentation on RabbitMq stating that acknowledging the message should be on the same channel from which consumer received. But I'm in a situation where due to some reason consumer process is stopped after I received message and haven't acknowledge the Rabbitmq, when the consumer process is restarted, consumer starts getting unacknowledged messages from RabbitMq, but here consumer cannot send the acknowledge to those messages as I get a channel exception stating the tag doesn't belongs to the channel. So, my question is how to handle this scenario and how can I acknowledge the rabbitmq to remove the message after my consumer process is completed reading the message?
rabbitmq
rabbitmq
asked Nov 22 '18 at 8:37
AbhinayAbhinay
1441317
1441317
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
As you said acknowledgement must be sent on the same channel
Acknowledgement must be sent on the same channel the delivery it is for was received on. Attempts to acknowledge using a different channel will result in a channel-level protocol exception
The easy way to do that is to use autoack=true
, so the message acknowledged automatically once it is consumed.
boolean autoAck = true; // acknowledgment is covered below
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
EDIT
if auto_ack does not work for you, you could use channel_consumer.basicCancel(consumerTag);
something like that:
final Consumer consumer = new DefaultConsumer(channel_consumer) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
channel_consumer.basicCancel(consumerTag);
System.out.println(" [x] stopping" + message + "'");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] elaborated getting ack" + message + "'");
channel_consumer.basicAck(envelope.getDeliveryTag(), false);
} finally {
System.out.println(" [x] Done");
}
}
};
boolean autoAck = false; // acknowledgment is covered below
channel_consumer.basicConsume("test", autoAck, consumer);
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426824%2fhow-to-acknowledge-message-in-rabbitmq-in-a-different-channel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As you said acknowledgement must be sent on the same channel
Acknowledgement must be sent on the same channel the delivery it is for was received on. Attempts to acknowledge using a different channel will result in a channel-level protocol exception
The easy way to do that is to use autoack=true
, so the message acknowledged automatically once it is consumed.
boolean autoAck = true; // acknowledgment is covered below
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
EDIT
if auto_ack does not work for you, you could use channel_consumer.basicCancel(consumerTag);
something like that:
final Consumer consumer = new DefaultConsumer(channel_consumer) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
channel_consumer.basicCancel(consumerTag);
System.out.println(" [x] stopping" + message + "'");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] elaborated getting ack" + message + "'");
channel_consumer.basicAck(envelope.getDeliveryTag(), false);
} finally {
System.out.println(" [x] Done");
}
}
};
boolean autoAck = false; // acknowledgment is covered below
channel_consumer.basicConsume("test", autoAck, consumer);
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
add a comment |
As you said acknowledgement must be sent on the same channel
Acknowledgement must be sent on the same channel the delivery it is for was received on. Attempts to acknowledge using a different channel will result in a channel-level protocol exception
The easy way to do that is to use autoack=true
, so the message acknowledged automatically once it is consumed.
boolean autoAck = true; // acknowledgment is covered below
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
EDIT
if auto_ack does not work for you, you could use channel_consumer.basicCancel(consumerTag);
something like that:
final Consumer consumer = new DefaultConsumer(channel_consumer) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
channel_consumer.basicCancel(consumerTag);
System.out.println(" [x] stopping" + message + "'");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] elaborated getting ack" + message + "'");
channel_consumer.basicAck(envelope.getDeliveryTag(), false);
} finally {
System.out.println(" [x] Done");
}
}
};
boolean autoAck = false; // acknowledgment is covered below
channel_consumer.basicConsume("test", autoAck, consumer);
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
add a comment |
As you said acknowledgement must be sent on the same channel
Acknowledgement must be sent on the same channel the delivery it is for was received on. Attempts to acknowledge using a different channel will result in a channel-level protocol exception
The easy way to do that is to use autoack=true
, so the message acknowledged automatically once it is consumed.
boolean autoAck = true; // acknowledgment is covered below
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
EDIT
if auto_ack does not work for you, you could use channel_consumer.basicCancel(consumerTag);
something like that:
final Consumer consumer = new DefaultConsumer(channel_consumer) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
channel_consumer.basicCancel(consumerTag);
System.out.println(" [x] stopping" + message + "'");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] elaborated getting ack" + message + "'");
channel_consumer.basicAck(envelope.getDeliveryTag(), false);
} finally {
System.out.println(" [x] Done");
}
}
};
boolean autoAck = false; // acknowledgment is covered below
channel_consumer.basicConsume("test", autoAck, consumer);
As you said acknowledgement must be sent on the same channel
Acknowledgement must be sent on the same channel the delivery it is for was received on. Attempts to acknowledge using a different channel will result in a channel-level protocol exception
The easy way to do that is to use autoack=true
, so the message acknowledged automatically once it is consumed.
boolean autoAck = true; // acknowledgment is covered below
channel.basicConsume(TASK_QUEUE_NAME, autoAck, consumer);
EDIT
if auto_ack does not work for you, you could use channel_consumer.basicCancel(consumerTag);
something like that:
final Consumer consumer = new DefaultConsumer(channel_consumer) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + message + "'");
try {
channel_consumer.basicCancel(consumerTag);
System.out.println(" [x] stopping" + message + "'");
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(" [x] elaborated getting ack" + message + "'");
channel_consumer.basicAck(envelope.getDeliveryTag(), false);
} finally {
System.out.println(" [x] Done");
}
}
};
boolean autoAck = false; // acknowledgment is covered below
channel_consumer.basicConsume("test", autoAck, consumer);
edited Nov 22 '18 at 9:38
answered Nov 22 '18 at 9:13
GabrieleGabriele
14.9k42536
14.9k42536
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
add a comment |
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
We want to acknowledge only once we performed some operation like persisting/processing the message, so don't want to auto acknowledge.
– Abhinay
Nov 22 '18 at 9:15
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
@Abhinay updated the answer
– Gabriele
Nov 22 '18 at 9:38
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
for channel_consumer.basicCancel(consumerTag); is there any restrictions like it should be in same channel like basicAck has? if so, then basicCancel also cannot be used in my scenario.
– Abhinay
Nov 22 '18 at 10:59
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53426824%2fhow-to-acknowledge-message-in-rabbitmq-in-a-different-channel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown