Event bus
Asynchronous communication between modules uses the Vert.x event bus, a lightweight distributed messaging system. It lets modules communicate whether they run in the same Java virtual machine or on different nodes of a cluster.
Principles
Addresses. Every message is sent to an address, which is a plain string. Consumers register on an address; producers send to it. One address can have several consumers.
Patterns. Three ways to deliver a message:
| Pattern | Delivery |
|---|---|
| Send | To one consumer of the address. When several are registered, one is picked. |
| Publish | To every consumer of the address |
| Request-reply | To one consumer, and the producer waits for its reply |
Handlers. A consumer registers a handler, a function called each time a message arrives on its address.
Clustering. In cluster mode, messages cross nodes transparently, as if all modules shared one virtual machine. The platform clusters the bus with Hazelcast. See Scaling.
Using the event bus
Send a message:
Vertx vertx = Vertx.vertx();
EventBus eventBus = vertx.eventBus();
eventBus.send("some.address", "Hello World!");
Publish a message:
eventBus.publish("news.uk.sport", "A new article on UK sports!");
Consume messages:
eventBus.consumer("some.address", message -> {
System.out.println("Received message: " + message.body());
});
Request and reply:
// Consumer
eventBus.consumer("request.address", message -> {
System.out.println("Received request: " + message.body());
message.reply("This is the reply");
});
// Producer
eventBus.request("request.address", "Request Message", ar -> {
if (ar.succeeded()) {
System.out.println("Received reply: " + ar.result().body());
} else {
System.out.println("Failed to receive reply");
}
});
Message bodies
JSON is supported natively and is the usual body format:
JsonObject json = new JsonObject().put("key", "value");
eventBus.send("json.address", json);
Other object types need a custom codec that encodes and decodes them on the bus.
Addresses used by the platform
Every module can produce on these addresses.
| Consumer | Address | Use |
|---|---|---|
| Communication | wse.communication.users | Lists the members visible to a given member |
| Communication | wse.communication | Sends e-mail |
| Application registry | wse.app.registry.applications | Lists applications |
| Document space | org.entcore.workspace | Exchanges documents with the document space |
| Directory | userbook.preferences | Member preferences |
| Directory | user.repository | Import and export of a member's data, handled by each module |
| Directory | entcore.directory | Directory lookups |
| Portal | wse.timeline | Publishes notifications to the activity feed |
| Infra | entcore.sms | Sends text messages |
| Feeder | entcore.feeder | Triggers a data import or export |
| Authentication | cas.configuration | Retrieves the CAS configuration; produced by the application registry |