|
1 | | -# Danube Java examples |
| 1 | +# Danube Java Examples |
2 | 2 |
|
3 | | -These standalone examples demonstrate common Danube Java client workflows. |
| 3 | +Standalone Java examples demonstrating common Danube messaging client workflows. |
| 4 | +Each file is a self-contained program with a `main` method — no build system required. |
4 | 5 |
|
5 | | -## Files |
| 6 | +## Examples |
6 | 7 |
|
7 | | -- `SchemaRegistryProducerExample.java`: register schema + build producer with schema reference |
8 | | -- `ConsumerObservabilityExample.java`: subscribe consumer with lifecycle/message/error listeners |
| 8 | +| File | Description | |
| 9 | +|------|-------------| |
| 10 | +| `SimpleProducerConsumer.java` | Basic producer + consumer, raw byte messages | |
| 11 | +| `JsonProducer.java` | Register a JSON schema and produce schema-tagged messages | |
| 12 | +| `JsonConsumer.java` | Consume and print JSON messages | |
| 13 | +| `PartitionsProducer.java` | Produce across 3 topic partitions | |
| 14 | +| `PartitionsConsumer.java` | Consume from all partitions, showing which partition each message arrived from | |
| 15 | +| `ReliableDispatchProducer.java` | Reliable dispatch: broker waits for ack before sending next message | |
| 16 | +| `ReliableDispatchConsumer.java` | Consume reliable messages and report byte throughput | |
| 17 | +| `SchemaEvolution.java` | Register, evolve, and compatibility-check schemas in the registry | |
9 | 18 |
|
10 | | -## Run locally |
| 19 | +## Prerequisites |
11 | 20 |
|
12 | | -1. Start a Danube broker locally. |
13 | | -2. Build artifacts: |
| 21 | +Start the Danube broker using Docker Compose: |
14 | 22 |
|
15 | 23 | ```bash |
| 24 | +cd docker |
| 25 | +docker compose up -d |
| 26 | +``` |
| 27 | + |
| 28 | +Build the client jars: |
| 29 | + |
| 30 | +```bash |
| 31 | +# From the repo root |
16 | 32 | mvn -DskipTests package |
17 | 33 | ``` |
18 | 34 |
|
19 | | -3. Compile and run an example with your preferred IDE or local `javac/java` setup, ensuring `danube-client` and `danube-client-proto` are on the classpath. |
| 35 | +## Running an Example |
| 36 | + |
| 37 | +Set up the classpath with both the client and its proto dependency: |
| 38 | + |
| 39 | +```bash |
| 40 | +CP=danube-client/target/danube-client-0.2.0.jar:danube-client-proto/target/danube-client-proto-0.2.0.jar |
| 41 | + |
| 42 | +# Compile |
| 43 | +javac -cp "$CP" examples/SimpleProducerConsumer.java -d examples/out |
| 44 | + |
| 45 | +# Run |
| 46 | +java -cp "$CP:examples/out" SimpleProducerConsumer |
| 47 | +``` |
| 48 | + |
| 49 | +Override the broker URL via environment variable if needed: |
| 50 | + |
| 51 | +```bash |
| 52 | +DANUBE_BROKER_URL=http://my-broker:6650 java -cp "$CP:examples/out" SimpleProducerConsumer |
| 53 | +``` |
| 54 | + |
| 55 | +## Producer / Consumer Pairs |
| 56 | + |
| 57 | +These examples are designed to be run together in separate terminals: |
| 58 | + |
| 59 | +| Producer | Consumer | Topic | |
| 60 | +|----------|----------|-------| |
| 61 | +| `JsonProducer` | `JsonConsumer` | `/default/json_topic` | |
| 62 | +| `PartitionsProducer` | `PartitionsConsumer` | `/default/partitioned_topic` | |
| 63 | +| `ReliableDispatchProducer` | `ReliableDispatchConsumer` | `/default/reliable_topic` | |
| 64 | + |
| 65 | +**Always start the consumer first** so it is subscribed before the producer sends. |
| 66 | + |
| 67 | +## Key Concepts |
| 68 | + |
| 69 | +- **`producer.create()`** — registers the producer with the broker (creates topic if needed) |
| 70 | +- **`consumer.subscribe()`** — subscribes to the topic, starts receive loops on virtual threads |
| 71 | +- **`consumer.receive()`** — returns a `Flow.Publisher<StreamMessage>`; attach a `Flow.Subscriber` to receive messages |
| 72 | +- **`consumer.ack(msg)`** — acknowledges a message; required in reliable dispatch mode |
| 73 | +- **Schema registry** — register schemas via `client.newSchemaRegistry()`, then use `.withSchemaLatest(subject)` on the producer builder |
0 commit comments