Messaging
The Messaging Module encapsulates the application's messaging architecture and abstracts the choice of the message bus, providing a simple yet powerful interface for producing and consuming events. This module promotes a decoupled, event-driven design, ensuring scalability and maintainability across the system.
Purpose
The primary goal of the Messaging Module is to handle inter-module communication via event-driven mechanisms. This allows modules to emit and consume events without tight coupling, enabling flexibility in evolving the system and integrating with different message brokers.
Features
-
Event Abstraction:
- Abstracts the complexity of the message bus implementation.
- Supports multiple message brokers (e.g., Apache Pulsar, Kafka).
-
Annotations for Event Consumption:
- Simplifies event consumption with custom annotations.
- Developers can annotate their classes and methods to handle events without manual wiring.
-
Outbox Pattern:
- Ensures reliable event delivery by maintaining a transactional outbox table.
- Events are stored in the outbox during database transactions and later published to the message bus.
-
Producer and Consumer Flexibility:
- Producers can emit events to specific topics with minimal configuration.
- Consumers can subscribe to events by defining a topic and event class.
Consuming Events
To consume an event in a specific module, annotate the relevant class with @ProcessMessageContainer. Use the @ProcessMessageConsumer annotation on the method that handles the event.
Module import
<dependency>
<groupId>com.farm</groupId>
<artifactId>messaging</artifactId>
</dependency>
Consume event
@ProcessMessageContainer
@Component
public class UserEventConsumer {
@ProcessMessageConsumer(topic = EventTopic.USER, clazz = UserCreatedEvent.class)
void consumeMessage(UserCreatedEvent userCreatedEvent) {
// Process the UserCreatedEvent
System.out.println("New user created: " + userCreatedEvent.getUsername());
}
}