Search in sources :

Example 1 with PubSubDeliveryException

use of org.springframework.cloud.gcp.pubsub.core.PubSubDeliveryException in project spring-cloud-gcp by spring-cloud.

the class PubSubPublisherTemplate method publish.

@Override
public ListenableFuture<String> publish(final String topic, PubsubMessage pubsubMessage) {
    Assert.hasText(topic, "The topic can't be null or empty.");
    Assert.notNull(pubsubMessage, "The pubsubMessage can't be null.");
    ApiFuture<String> publishFuture = this.publisherFactory.createPublisher(topic).publish(pubsubMessage);
    final SettableListenableFuture<String> settableFuture = new SettableListenableFuture<>();
    ApiFutures.addCallback(publishFuture, new ApiFutureCallback<String>() {

        @Override
        public void onFailure(Throwable throwable) {
            String errorMessage = "Publishing to " + topic + " topic failed.";
            LOGGER.warn(errorMessage, throwable);
            PubSubDeliveryException pubSubDeliveryException = new PubSubDeliveryException(pubsubMessage, errorMessage, throwable);
            settableFuture.setException(pubSubDeliveryException);
        }

        @Override
        public void onSuccess(String result) {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Publishing to " + topic + " was successful. Message ID: " + result);
            }
            settableFuture.set(result);
        }
    });
    return settableFuture;
}
Also used : SettableListenableFuture(org.springframework.util.concurrent.SettableListenableFuture) PubSubDeliveryException(org.springframework.cloud.gcp.pubsub.core.PubSubDeliveryException)

Aggregations

PubSubDeliveryException (org.springframework.cloud.gcp.pubsub.core.PubSubDeliveryException)1 SettableListenableFuture (org.springframework.util.concurrent.SettableListenableFuture)1