Java client

Obtaining the Java client library

Streamlio Cloud supports any Apache Pulsar client library starting from 2.3.1 release.

To add the dependency in your Maven project, add this in the <dependencies> section:

<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>2.3.1</version>
</dependency>

Users are also encouraged to use the Streamlio distribution of the Pulsar client, where bugfixes are released in a timely manner. See details in Pulsar Distribution page.

Reference documentation

Detailed documentation of the Pulsar client Java API can be found at https://pulsar.apache.org/api/client/.

Producer Example

PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar+ssl://prod.my-company.cloud.streamlio.com:6651")
.authentication(AuthenticationFactory.token("s.JzdWIiOiJKb2UifQ"))
.build();
Producer<String> producer = client.newProducer(Schema.STRING)
.topic("my-tenant/my-namespace/my-topic")
.create();
producer.send("Hello!");
client.close();

Consumer Example

PulsarClient client = PulsarClient.builder()
.serviceUrl("pulsar+ssl://prod.my-company.cloud.streamlio.com:6651")
.authentication(AuthenticationFactory.token("s.JzdWIiOiJKb2UifQ"))
.build();
Consumer<String> consumer = client.newConsumer(Schema.STRING)
.topic("my-tenant/my-namespace/my-topic")
.subscriptionName("my-subscription")
.subscriptionType(SubscriptionType.Shared)
.subscribe();
while (true) {
Message<String> msg = consumer.receive();
try {
System.out.println("Processing message: " + msg.getValue());
consumer.acknowledge(msg);
} catch (Exception e) {
System.err.println("Failed to process message: " + msg.getValue());
consumer.negativeAcknowledge(msg);
}
}
client.close();

_____ Copyright 2019 Streamlio, Inc. Apache, Apache BookKeeper, Apache Pulsar and associated open source project names are trademarks of the Apache Software Foundation.