Skip to content

How to publish a Pub/Sub message with a Supplier bean?

I want to publish a Pub/Sub message in Spring according to the Google documentation (see 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.

// Create an output binder to send messages to `topic-one` using a Supplier bean.
public Supplier<Flux<Message<String>>> sendMessageToTopicOne() {
  return () ->
              sink -> {
                try {
                } catch (InterruptedException e) {
                  // Stop sleep earlier.

                Message<String> message =
                    MessageBuilder.withPayload("message-" + rand.nextInt(1000)).build();
                    "Sending a message via the output binder to topic-one! Payload: "
                        + message.getPayload());

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?



I am now using an old and deprecated method with Channels, but it works fine so far.