|
| 1 | +# BabelQueue for Spring |
| 2 | + |
| 3 | +[](https://github.com/BabelQueue/babelqueue-spring/actions/workflows/ci.yml) |
| 4 | +[](https://central.sonatype.com/artifact/com.babelqueue/babelqueue-spring) |
| 5 | +[](LICENSE) |
| 6 | + |
| 7 | +> **Polyglot Queues, Simplified.** A Spring Boot adapter that makes your Spring |
| 8 | +> services produce and consume the canonical BabelQueue message envelope over |
| 9 | +> RabbitMQ — so they exchange messages with Laravel, Symfony, Python, Go, Node and |
| 10 | +> .NET over one strict JSON format. |
| 11 | +
|
| 12 | +This is the **Spring adapter** on top of the framework-agnostic |
| 13 | +[`babelqueue-core`](https://github.com/BabelQueue/babelqueue-java): a Spring AMQP |
| 14 | +`MessageConverter`, an ergonomic publisher, and Spring Boot auto-configuration. The |
| 15 | +full standard is documented at **[babelqueue.com](https://babelqueue.com)**. |
| 16 | + |
| 17 | +## Installation |
| 18 | + |
| 19 | +Maven: |
| 20 | + |
| 21 | +```xml |
| 22 | +<dependency> |
| 23 | + <groupId>com.babelqueue</groupId> |
| 24 | + <artifactId>babelqueue-spring</artifactId> |
| 25 | + <version>0.1.0</version> |
| 26 | +</dependency> |
| 27 | +``` |
| 28 | + |
| 29 | +Gradle: |
| 30 | + |
| 31 | +```kotlin |
| 32 | +implementation("com.babelqueue:babelqueue-spring:0.1.0") |
| 33 | +``` |
| 34 | + |
| 35 | +Bring your own Spring AMQP (the adapter targets **Spring Boot 3**, Java **17+**): |
| 36 | + |
| 37 | +```kotlin |
| 38 | +implementation("org.springframework.boot:spring-boot-starter-amqp") |
| 39 | +``` |
| 40 | + |
| 41 | +## How it works |
| 42 | + |
| 43 | +Auto-configuration registers a single `MessageConverter` bean |
| 44 | +(`BabelQueueMessageConverter`). Spring Boot wires it into **both** the |
| 45 | +`RabbitTemplate` (producing) and the default `@RabbitListener` container factory |
| 46 | +(consuming), so the canonical envelope is the wire format with no extra setup. Both |
| 47 | +beans back off if your application defines its own. |
| 48 | + |
| 49 | +## Producing |
| 50 | + |
| 51 | +```java |
| 52 | +import com.babelqueue.spring.BabelQueuePublisher; |
| 53 | +import java.util.Map; |
| 54 | + |
| 55 | +@Service |
| 56 | +class Orders { |
| 57 | + private final BabelQueuePublisher babelQueue; |
| 58 | + |
| 59 | + Orders(BabelQueuePublisher babelQueue) { |
| 60 | + this.babelQueue = babelQueue; |
| 61 | + } |
| 62 | + |
| 63 | + void create() { |
| 64 | + babelQueue.publish("urn:babel:orders:created", Map.of("order_id", 1042L), "orders"); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +Or build a typed message by implementing `com.babelqueue.PolyglotMessage` and |
| 70 | +`babelQueue.publish(message, "orders")`. Either way the broker receives: |
| 71 | + |
| 72 | +```json |
| 73 | +{ |
| 74 | + "job": "urn:babel:orders:created", |
| 75 | + "trace_id": "…", |
| 76 | + "data": { "order_id": 1042 }, |
| 77 | + "meta": { "id": "…", "queue": "orders", "lang": "java", "schema_version": 1, "created_at": 1749132727000 }, |
| 78 | + "attempts": 0 |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +…plus the contract AMQP properties (`type` = URN, `correlation_id` = trace_id, |
| 83 | +`message_id` = meta.id, `x-schema-version` / `x-source-lang` / `x-attempts`). |
| 84 | + |
| 85 | +## Consuming |
| 86 | + |
| 87 | +A `@RabbitListener` method receives the decoded `Envelope` — from any producer, in |
| 88 | +any language: |
| 89 | + |
| 90 | +```java |
| 91 | +import com.babelqueue.Envelope; |
| 92 | +import com.babelqueue.EnvelopeCodec; |
| 93 | + |
| 94 | +@Component |
| 95 | +class OrderListener { |
| 96 | + @RabbitListener(queues = "orders") |
| 97 | + void onMessage(Envelope envelope) { |
| 98 | + switch (EnvelopeCodec.urn(envelope)) { |
| 99 | + case "urn:babel:orders:created" -> |
| 100 | + log.info("[{}] order {}", envelope.traceId(), envelope.data().get("order_id")); |
| 101 | + default -> { /* ignore */ } |
| 102 | + } |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +Non-conformant messages (missing URN, unsupported `meta.schema_version`, blank |
| 108 | +`trace_id`, missing `data`) raise a `MessageConversionException`, so Spring rejects |
| 109 | +them — route them to a dead-letter exchange the usual Spring AMQP way. |
| 110 | + |
| 111 | +## Configuration |
| 112 | + |
| 113 | +```yaml |
| 114 | +babelqueue: |
| 115 | + default-queue: orders # used by the publisher when no queue is given |
| 116 | +``` |
| 117 | +
|
| 118 | +## License |
| 119 | +
|
| 120 | +[MIT](LICENSE) © Muhammet Şafak |
0 commit comments