-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReliableDispatchProducer.java
More file actions
62 lines (52 loc) · 2.36 KB
/
ReliableDispatchProducer.java
File metadata and controls
62 lines (52 loc) · 2.36 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
60
61
62
import com.danubemessaging.client.DanubeClient;
import com.danubemessaging.client.DispatchStrategy;
import com.danubemessaging.client.Producer;
import java.util.Map;
/**
* Reliable dispatch producer example: the broker waits for a consumer ack
* before delivering the next message, guaranteeing ordered, lossless delivery.
*
* Run ReliableDispatchConsumer.java in a separate terminal to receive these messages.
*
* Prerequisites: Danube broker running on localhost:6650
* cd docker && docker compose up -d
*/
public class ReliableDispatchProducer {
private static final String BROKER_URL = System.getenv().getOrDefault("DANUBE_BROKER_URL", "http://127.0.0.1:6650");
private static final String TOPIC = "/default/reliable_topic";
private static final String[] SUBJECTS = {
"The Danube system", "Danube platform", "Danube messaging service", "The Danube application"
};
private static final String[] VERBS = {"processes", "handles", "manages", "delivers"};
private static final String[] OBJECTS = {
"messages efficiently", "data reliably", "requests quickly", "events seamlessly"
};
private static final String[] CONCLUSIONS = {
"with high performance.", "at scale.", "in real-time.", "without issues."
};
public static void main(String[] args) throws Exception {
DanubeClient client = DanubeClient.builder()
.serviceUrl(BROKER_URL)
.build();
Producer producer = client.newProducer()
.withTopic(TOPIC)
.withName("prod_reliable")
.withDispatchStrategy(DispatchStrategy.RELIABLE)
.build();
producer.create();
System.out.println("Reliable dispatch producer created");
for (int i = 0; i < 20; i++) {
String message = String.format(
"%s %s %s, with low latency and high throughput. It is designed for reliability, %s",
SUBJECTS[i % SUBJECTS.length],
VERBS[i % VERBS.length],
OBJECTS[i % OBJECTS.length],
CONCLUSIONS[i % CONCLUSIONS.length]);
long msgId = producer.send(message.getBytes(), Map.of());
System.out.printf("Sent message #%d (id=%d)%n", i, msgId);
Thread.sleep(1000);
}
System.out.println("Done");
client.close();
}
}