-
-
Notifications
You must be signed in to change notification settings - Fork 67
/
PulsarWriter.java
63 lines (55 loc) · 1.72 KB
/
PulsarWriter.java
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
60
61
62
63
/**
* Copyright (c) KMG. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*/
package io.sbk.driver.Pulsar;
import io.sbk.params.ParameterOptions;
import io.sbk.api.Writer;
import lombok.Synchronized;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
/**
* Class for Pulsar writer/producer.
*/
public class PulsarWriter implements Writer<byte[]> {
final private Producer<byte[]> producer;
public PulsarWriter(int writerID, ParameterOptions params, String topicName, PulsarClient client) throws IOException {
try {
this.producer = client.newProducer()
.enableBatching(true)
.topic(topicName)
.blockIfQueueFull(true).create();
} catch (PulsarClientException ex) {
throw new IOException(ex);
}
}
@Override
public CompletableFuture writeAsync(byte[] data) throws IOException {
return producer.sendAsync(data);
}
@Override
public void sync() throws IOException {
try {
producer.flush();
} catch (PulsarClientException ex) {
throw new IOException(ex);
}
}
@Override
@Synchronized
public void close() throws IOException {
try {
producer.close();
} catch (PulsarClientException ex) {
throw new IOException(ex);
}
}
}