Skip to main content

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:

PatternDelivery
SendTo one consumer of the address. When several are registered, one is picked.
PublishTo every consumer of the address
Request-replyTo 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.

ConsumerAddressUse
Communicationwse.communication.usersLists the members visible to a given member
Communicationwse.communicationSends e-mail
Application registrywse.app.registry.applicationsLists applications
Document spaceorg.entcore.workspaceExchanges documents with the document space
Directoryuserbook.preferencesMember preferences
Directoryuser.repositoryImport and export of a member's data, handled by each module
Directoryentcore.directoryDirectory lookups
Portalwse.timelinePublishes notifications to the activity feed
Infraentcore.smsSends text messages
Feederentcore.feederTriggers a data import or export
Authenticationcas.configurationRetrieves the CAS configuration; produced by the application registry