Now that we’ve learned about clients and producers, let’s write a message synchronously to our cluster.
Follow along with the Tutorial Video. It is part of your Hands-on Exercise.
In the tutorial we saw how to synchronously create a client, create a producer, and publish a message.
Create a client synchronously:
pulsarClient = PulsarClient.builder()
.serviceUrl(PULSAR_URL)
.authentication(getAuth())
.allowTlsInsecureConnection(false)
.enableTlsHostnameVerification(true)
.build();
Create a producer synchronously:
producer = client.newProducer(Schema.STRING).topic(topic).create();
Publish a message synchronously:
producer.newMessage().value(message).send();
System.out.println("Published message " + message + " synchronously to topic " + topic);
Be sure to close the producer and client when you are finished using them.