Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposes publisher endpoint for Pub/Sub message ordering #421

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions docs/src/main/asciidoc/pubsub.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ After this amount of time has elapsed (counting from the first element added), t
Enables batching. | No | false
| `spring.cloud.gcp.pubsub.publisher.enable-message-ordering`|
Enables message ordering. | No | false
| `spring.cloud.gcp.pubsub.publisher.endpoint`|
The publisher endpoint.
Example: `"us-east1-pubsub.googleapis.com:443"`.
This is useful in conjunction with enabling message ordering because sending messages to the same region ensures they are received in order even when multiple publishers are used. | No | pubsub.googleapis.com:443
|===

==== GRPC Connection Settings
Expand Down Expand Up @@ -182,6 +186,7 @@ By default, the `SimplePubSubMessageConverter` is used to convert payloads of ty

If you are relying on message converters and would like to provide an ordering key, use the `GcpPubSubHeaders.ORDERING_KEY` header.
You will also need to make sure to enable message ordering on the publisher via the `spring.cloud.gcp.pubsub.publisher.enable-message-ordering` property.
Additionally, if you are using multiple publishers, you will want to set the `spring.cloud.gcp.pubsub.publisher.endpoint` to a regional endpoint such as `"us-east1-pubsub.googleapis.com:443"` so that messages are sent to the same region and received in order.

[source,java,indent=0]
----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ public PublisherFactory defaultPublisherFactory(
retrySettings.ifAvailable(factory::setRetrySettings);
batchingSettings.ifAvailable(factory::setBatchingSettings);
factory.setEnableMessageOrdering(gcpPubSubProperties.getPublisher().getEnableMessageOrdering());
factory.setEndpoint(gcpPubSubProperties.getPublisher().getEndpoint());
return factory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public static class Publisher {
*/
private Boolean enableMessageOrdering;

/**
* Set publisher endpoint. Example: "us-east1-pubsub.googleapis.com:443".
*/
private String endpoint;

public Batching getBatching() {
return this.batching;
}
Expand All @@ -154,6 +159,14 @@ public Boolean getEnableMessageOrdering() {
public void setEnableMessageOrdering(Boolean enableMessageOrdering) {
this.enableMessageOrdering = enableMessageOrdering;
}

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ public class PubSubTemplateDocumentationIntegrationTests {

private ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.cloud.gcp.pubsub.subscriber.max-ack-extension-period=0",
"spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true")
"spring.cloud.gcp.pubsub.publisher.enable-message-ordering=true",
"spring.cloud.gcp.pubsub.publisher.endpoint=us-east1-pubsub.googleapis.com:443")
.withConfiguration(AutoConfigurations.of(GcpContextAutoConfiguration.class,
GcpPubSubAutoConfiguration.class));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public class DefaultPublisherFactory implements PublisherFactory {

private Boolean enableMessageOrdering;

private String endpoint;

/**
* Create {@link DefaultPublisherFactory} instance based on the provided {@link GcpProjectIdProvider}.
* <p>The {@link GcpProjectIdProvider} must not be null, neither provide an empty {@code projectId}.
Expand Down Expand Up @@ -133,6 +135,17 @@ public void setEnableMessageOrdering(Boolean enableMessageOrdering) {
this.enableMessageOrdering = enableMessageOrdering;
}

/**
* Set the publisher endpoint. Example: "us-east1-pubsub.googleapis.com:443".
* This is useful in conjunction with enabling message ordering because
* sending messages to the same region ensures they are received in order
* even when multiple publishers are used.
* @param endpoint publisher endpoint
*/
public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

@Override
public Publisher createPublisher(String topic) {
return this.publishers.computeIfAbsent(topic, key -> {
Expand Down Expand Up @@ -179,6 +192,10 @@ void applyPublisherSettings(Publisher.Builder publisherBuilder) {
if (this.enableMessageOrdering != null) {
publisherBuilder.setEnableMessageOrdering(this.enableMessageOrdering);
}

if (this.endpoint != null) {
publisherBuilder.setEndpoint(this.endpoint);
}
}

Map<String, Publisher> getCache() {
Expand Down