Search code examples
google-cloud-platformspring-cloud

GCP Pub/Sub: How to get event details in onFailure() of PublishCallbackListener


We want to have the fail back mechanism in case of any failure to publish event to Pub/Sub. I am using "ListenableFutureCallback" to know message published successfully or not. In case of failure, it is just throwing exception and I need event details to post it to internal messaging service. How do I get event details in onFailure() method. I am using Spring Integration. Below is piece of code.

Listener:

@Component
public class PubSubOperationListener implements ListenableFutureCallback<String> {

    private static Logger LOGGER = LoggerFactory.getLogger(PubSubOperationListener.class);

    @Override
    public void onFailure(Throwable throwable) {
    LOGGER.error("Failed to publish the message and details : {}",throwable);
    // Logic to process it using different approach.
    }

    @Override
    public void onSuccess(String s) {
        LOGGER.info("Message published successfully.");
    }

ServiceActivator:

        PubSubMessageHandler pubSubMessageHandler = new PubSubMessageHandler(pubSubTemplate, testTopic);
        pubSubMessageHandler.setPublishCallback(pubSubOperationListener);
        return pubSubMessageHandler;

Please suggest if there is different approach to do same.


Solution

  • Currently, it's not possible because Spring Cloud GCP simply delegates to the Pub/Sub Publisher in the client library.

    However, when we wrap the Future provided by the Publisher in Spring Cloud GCP, we can potentially include the original message there and other metadata. This would be a feature request that should be filed here.