forked from sijie/pulsar-java-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderTutorial.java
More file actions
59 lines (46 loc) · 2.02 KB
/
ReaderTutorial.java
File metadata and controls
59 lines (46 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package tutorial;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Reader;
import org.apache.pulsar.client.api.ReaderBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
public class ReaderTutorial {
private static final Logger log = LoggerFactory.getLogger(ReaderTutorial.class);
private static final String SERVICE_URL = "pulsar://localhost:6650";
private static final String TOPIC_NAME = "tutorial-topic";
public static void main(String[] args) throws IOException {
PulsarClient client = PulsarClient.builder()
.serviceUrl(SERVICE_URL)
.build();
ReaderBuilder<byte[]> readerBuilder = client.newReader()
.topic(TOPIC_NAME);
Reader<byte[]> earliestReader = readerBuilder
.startMessageId(MessageId.earliest)
.create();
Reader<byte[]> latestReader = readerBuilder
.startMessageId(MessageId.latest)
.create();
log.info("Reading messages from earliest ({})", MessageId.earliest);
do {
Message<byte[]> msg = earliestReader.readNext();
log.info("Message received with ID {}", msg.getMessageId());
if (earliestReader.hasReachedEndOfTopic()) {
log.info("Done reading from earliest");
earliestReader.close();
}
} while (!earliestReader.hasReachedEndOfTopic());
log.info("Reading messages from latest ({})", MessageId.latest);
do {
Message<byte[]> msg = latestReader.readNext();
log.info("Message received with ID {}", msg.getMessageId());
if (latestReader.hasReachedEndOfTopic()) {
log.info("Done reading from latest");
latestReader.close();
}
} while (!latestReader.hasReachedEndOfTopic());
client.close();
}
}