Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

WIP binder producer error propagation #2502

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
import org.springframework.integration.core.MessageProducer;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.support.ErrorMessage;
import org.springframework.util.concurrent.ListenableFutureCallback;

/**
* Message channel binder for Pub/Sub.
Expand Down Expand Up @@ -74,6 +76,21 @@ protected MessageHandler createProducerMessageHandler(ProducerDestination destin
PubSubMessageHandler messageHandler = new PubSubMessageHandler(this.pubSubTemplate, destination.getName());
messageHandler.setBeanFactory(getBeanFactory());
messageHandler.setSync(producerProperties.getExtension().isSync());
if (errorChannel != null) {
messageHandler.setPublishCallback(new ListenableFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
System.out.println("Sending throwable to error channel: " + throwable);
errorChannel.send(new ErrorMessage(throwable));
}

@Override
public void onSuccess(String messageId) {
// no-op
System.out.println("Successfully published message " + messageId);
}
});
}
return messageHandler;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.springframework.cloud.gcp.stream.binder.pubsub;

import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.cloud.pubsub.v1.Publisher;
import com.google.protobuf.ByteString;
import com.google.pubsub.v1.PubsubMessage;
import org.awaitility.Awaitility;
import org.awaitility.Duration;
import org.junit.Test;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;

public class TempClientLibraryTest {

@Test
public void failedPublishCallsListener() throws IOException, InterruptedException {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test fails; the future error callback is never invoked.

Publisher publisher = Publisher.newBuilder("projects/elfel-spring/topics/exampleTopic")
.setEndpoint("cecicestnespasunendpoint:443")
.build();

PubsubMessage message = PubsubMessage.newBuilder()
.setData(ByteString.copyFromUtf8("test message"))
.build();

AtomicBoolean errorSet = new AtomicBoolean(false);

ApiFutures.addCallback(publisher.publish(message),
new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
System.out.println("Sending throwable to error channel: " + throwable);
errorSet.set(true);
}

@Override
public void onSuccess(String messageId) {
System.out.println("Successfully published message " + messageId);
}
});

Awaitility.await()
.atMost(Duration.TWO_MINUTES)
.until(() -> errorSet.get());

}


@Test
public void successfulPublishCallsListener() throws IOException, InterruptedException {
Publisher publisher = Publisher.newBuilder("projects/elfel-spring/topics/exampleTopic")
.build();

PubsubMessage message = PubsubMessage.newBuilder()
.setData(ByteString.copyFromUtf8("test successful message"))
.build();

AtomicBoolean successSet = new AtomicBoolean(false);

ApiFutures.addCallback(publisher.publish(message),
new ApiFutureCallback<String>() {
@Override
public void onFailure(Throwable throwable) {
System.out.println("publish errored out: " + throwable);
}

@Override
public void onSuccess(String messageId) {
System.out.println("Successfully published message " + messageId);
successSet.set(true);
}
});

Awaitility.await()
.atMost(Duration.FIVE_SECONDS)
.until(() -> successSet.get());

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
spring.cloud.stream.bindings.input.destination=my-topic
spring.cloud.stream.bindings.output.destination=my-topic

spring.cloud.stream.bindings.output.producer.errorChannelEnabled=true

# If group is specified, the Pub/Sub subscription name will be [PUBSUB_TOPIC_NAME].[PUBSUB_GROUP_NAME]
spring.cloud.stream.bindings.input.group=my-group

Expand Down