How to manually ack/nack a PubSub message in Camel Route

无人久伴 提交于 2020-06-28 08:16:29

问题


I am setting up a Camel Route with ackMode=NONE meaning acknowlegements are not done automatically. How do I explicitly acknowledge the message in the route?

In my Camel Route definition I've set ackMode to NONE. According to the documentation, I should be able to manually acknowledge the message downstream:

https://github.com/apache/camel/blob/master/components/camel-google-pubsub/src/main/docs/google-pubsub-component.adoc

"AUTO = exchange gets ack’ed/nack’ed on completion. NONE = downstream process has to ack/nack explicitly"

However I cannot figure out how to send the ack.

from("google-pubsub:<project>:<subscription>?concurrentConsumers=1&maxMessagesPerPoll=1&ackMode=NONE")
                .bean("processingBean");

My PubSub subscription has an acknowledgement deadline of 10 seconds and so my message keeps getting re-sent every 10 seconds due to ackMode=NONE. This is as expected. However I cannot find a way to manually acknowledge the message once processing is complete and stop the re-deliveries.


回答1:


I was able to dig through the Camel components and figure out how it is done. First I created a GooglePubSubConnectionFactory bean:

@Bean
    public GooglePubsubConnectionFactory googlePubsubConnectionFactory() {
        GooglePubsubConnectionFactory connectionFactory = new GooglePubsubConnectionFactory();
        connectionFactory.setCredentialsFileLocation(pubsubKey);
        return connectionFactory;
    }

Then I was able to reference the ack id of the message from the header:

@Header(GooglePubsubConstants.ACK_ID) String ackId

Then I used the following code to acknowledge the message:

List<String > ackIdList = new ArrayList<>();
        ackIdList.add(ackId);
        AcknowledgeRequest ackRequest = new AcknowledgeRequest().setAckIds(ackIdList);
        Pubsub pubsub = googlePubsubConnectionFactory.getDefaultClient();
        pubsub.projects().subscriptions().acknowledge("projects/<my project>/subscriptions/<my subscription>", ackRequest).execute();



回答2:


I think it is best if you look how the Camel component does it with ackMode=AUTO. Have a look at this class (method acknowledge)

But why do you want to do this extra work? Camel is your fried to simplify integration by abstracting away low level code.

So when you use ackMode=AUTO Camel automatically commits your successfully processed messages (when the message has successfully passed the whole route) and rolls back your not processable messages.



来源:https://stackoverflow.com/questions/57749818/how-to-manually-ack-nack-a-pubsub-message-in-camel-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!