|
| 1 | +# BabelQueue for Java |
| 2 | + |
| 3 | +[](https://github.com/BabelQueue/babelqueue-java/actions/workflows/ci.yml) |
| 4 | +[](https://central.sonatype.com/artifact/com.babelqueue/babelqueue-core) |
| 5 | +[](https://javadoc.io/doc/com.babelqueue/babelqueue-core) |
| 6 | +[](LICENSE) |
| 7 | + |
| 8 | +> **Polyglot Queues, Simplified.** Read and write the canonical BabelQueue message |
| 9 | +> envelope from Java — so your Java/Spring services exchange messages with Laravel, |
| 10 | +> Symfony, Python, Go and Node over one strict JSON format, on the broker you |
| 11 | +> already run. |
| 12 | +
|
| 13 | +This is the framework-agnostic **Java core**: the wire-envelope codec, contracts |
| 14 | +and dead-letter helpers — **zero dependencies** (pure JDK, including its own |
| 15 | +minimal JSON codec, so no Jackson/Gson is forced on you). The full standard is |
| 16 | +documented at **[babelqueue.com](https://babelqueue.com)**. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +Maven: |
| 21 | + |
| 22 | +```xml |
| 23 | +<dependency> |
| 24 | + <groupId>com.babelqueue</groupId> |
| 25 | + <artifactId>babelqueue-core</artifactId> |
| 26 | + <version>0.1.0</version> |
| 27 | +</dependency> |
| 28 | +``` |
| 29 | + |
| 30 | +Gradle: |
| 31 | + |
| 32 | +```kotlin |
| 33 | +implementation("com.babelqueue:babelqueue-core:0.1.0") |
| 34 | +``` |
| 35 | + |
| 36 | +Requires Java **17+**. |
| 37 | + |
| 38 | +## Usage |
| 39 | + |
| 40 | +```java |
| 41 | +import com.babelqueue.*; |
| 42 | +import java.util.Map; |
| 43 | + |
| 44 | +// Produce — build the canonical envelope and publish the JSON to your broker. |
| 45 | +Envelope env = EnvelopeCodec.make( |
| 46 | + "urn:babel:orders:created", |
| 47 | + Map.of("order_id", 1042L), |
| 48 | + "orders", |
| 49 | + null); |
| 50 | +String body = EnvelopeCodec.encode(env); // compact UTF-8 JSON |
| 51 | +// jedis.rpush("queues:orders", body); |
| 52 | +// / channel.basicPublish("", "orders", props, body.getBytes(StandardCharsets.UTF_8)); |
| 53 | + |
| 54 | +// Consume — decode a message produced by ANY BabelQueue SDK. |
| 55 | +Envelope in = EnvelopeCodec.decode(body); |
| 56 | +if (EnvelopeCodec.accepts(in)) { |
| 57 | + switch (EnvelopeCodec.urn(in)) { |
| 58 | + case "urn:babel:orders:created" -> |
| 59 | + System.out.println(in.data().get("order_id") + " " + in.traceId()); |
| 60 | + default -> { /* unknown URN */ } |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +The envelope is identical to every other SDK's: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "job": "urn:babel:orders:created", |
| 70 | + "trace_id": "…", |
| 71 | + "data": { "order_id": 1042 }, |
| 72 | + "meta": { "id": "…", "queue": "orders", "lang": "java", "schema_version": 1, "created_at": 1749132727000 }, |
| 73 | + "attempts": 0 |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +> JSON numbers decode into `data` as `Long` (integers) or `Double` (decimals); |
| 78 | +> objects as `LinkedHashMap` (insertion order preserved). `encode` leaves slashes |
| 79 | +> and non-ASCII unescaped, so the bytes match the PHP/Python/Node cores. |
| 80 | +
|
| 81 | +### Typed messages (optional) |
| 82 | + |
| 83 | +```java |
| 84 | +record OrderCreated(long orderId) implements PolyglotMessage, HasTraceId { |
| 85 | + public String getBabelUrn() { return "urn:babel:orders:created"; } |
| 86 | + public Map<String, Object> toPayload() { return Map.of("order_id", orderId); } |
| 87 | + public String getBabelTraceId() { return null; } // or an inbound trace to continue |
| 88 | +} |
| 89 | + |
| 90 | +Envelope env = EnvelopeCodec.fromMessage(new OrderCreated(1042L), "orders"); |
| 91 | +``` |
| 92 | + |
| 93 | +### Dead-letter |
| 94 | + |
| 95 | +```java |
| 96 | +Envelope dlq = DeadLetters.annotate(env, "failed", "orders", 3, "boom", "java.lang.RuntimeException"); |
| 97 | +// publish EnvelopeCodec.encode(dlq) to the "orders.dlq" queue |
| 98 | +``` |
| 99 | + |
| 100 | +`DeadLetters.annotate` returns a copy — the original envelope is preserved |
| 101 | +unchanged inside the dead-lettered message, so any-language consumers can still |
| 102 | +read it. |
| 103 | + |
| 104 | +## What this core is (and isn't) |
| 105 | + |
| 106 | +It enforces the **contract**: the envelope shape, URN identity, trace propagation, |
| 107 | +schema-version gating and the dead-letter block. It is intentionally **not** a |
| 108 | +worker/runtime — broker wiring, acks and retry loops stay in your own code (or a |
| 109 | +future Spring adapter), exactly as with the other SDK cores. |
| 110 | + |
| 111 | +`UnknownUrnStrategy` (`FAIL`, `DELETE`, `RELEASE`, `DEAD_LETTER`) is provided for |
| 112 | +adapters to act on. |
| 113 | + |
| 114 | +## Conformance |
| 115 | + |
| 116 | +This core passes the shared **cross-SDK conformance suite** (vendored under |
| 117 | +[`src/test/resources/conformance/`](src/test/resources/conformance)) — the same |
| 118 | +fixtures every BabelQueue SDK must satisfy, so a Java producer and, say, a Laravel |
| 119 | +consumer agree byte-for-byte. |
| 120 | + |
| 121 | +```bash |
| 122 | +mvn test |
| 123 | +``` |
| 124 | + |
| 125 | +## License |
| 126 | + |
| 127 | +[MIT](LICENSE) © Muhammet Şafak |
0 commit comments