I want to publish a Pub/Sub message in Spring according to the Google documentation (see https://cloud.google.com/pubsub/docs/spring#publishing-messages-using-stream-binder). The only problem is that I am new to Spring Framework and I have no idea of how to turn this example into something that I can use. That is, a method that allows me to send a specific message from a service to the Pub/Sub topic.
JavaScript
x
// Create an output binder to send messages to `topic-one` using a Supplier bean.
@Bean
public Supplier<Flux<Message<String>>> sendMessageToTopicOne() {
return () ->
Flux.<Message<String>>generate(
sink -> {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
// Stop sleep earlier.
}
Message<String> message =
MessageBuilder.withPayload("message-" + rand.nextInt(1000)).build();
LOGGER.info(
"Sending a message via the output binder to topic-one! Payload: "
+ message.getPayload());
sink.next(message);
})
.subscribeOn(Schedulers.boundedElastic());
}
How can I write a method that allows me to send a specific message (lets say a ‘String’) to my Pub/Sub topic that can be called from a service?
Advertisement
Answer
I am now using an old and deprecated method with Channels, but it works fine so far.