Event Publishing Architecture
Events are raised within the domain model and published to RabbitMQ:RabbitMQ Configuration
The service is configured to publish events to theorder-publish-x exchange:
OrderRabbitmqConfig.java
- Exchange:
order-publish-x(topic exchange) - Routing Key Pattern:
com.ecommerce.order.sdk.event.order.# - Event Package: All events are in
com.ecommerce.order.sdk.event.orderpackage
Domain Events
All order events extend the baseOrderEvent class:
OrderEvent.java
OrderCreatedEvent
Published when a new order is created:- During order creation via
POST /orders - Before the order is persisted to the database
- Complete order details including all items
- Calculated total price
- Delivery address
- Creation timestamp
- Trigger inventory reservation checks
- Update order query database (CQRS)
- Send order confirmation emails
- Log order metrics and analytics
OrderProductChangedEvent
Published when product quantities are modified:- During product count changes via
POST /orders/{id}/products - After validation but before persistence
- Order ID and product ID
- Previous and new quantity values
- Update inventory reservations
- Recalculate order summaries in query database
- Track order modifications for analytics
OrderAddressChangedEvent
Published when delivery address is updated:- During address updates via
POST /orders/{id}/address/detail - After validation passes
- Order ID
- Previous and new address detail strings
- Update shipping information in logistics systems
- Refresh order summaries
- Audit address changes
OrderPaidEvent
Published when payment is successfully processed:- During payment processing via
POST /orders/{id}/payment - After price validation passes
- Before order status changes to PAID
- Order ID only (minimal payload)
- Finalize inventory reservation
- Trigger fulfillment workflows
- Update payment status in query database
- Send payment confirmation
OrderPaidEvent has the smallest payload since consumers can fetch full order details if needed using the order ID.
Event Consumption
Other services consume these events via RabbitMQ:Order Query Service
Consumes all order events to maintain denormalized views:Inventory Service
Consumes payment events to finalize inventory:Event Ordering
Events are published synchronously within the same transaction:- Domain logic executes and raises event
- Event is collected by
BaseAggregate - Transaction commits (order persisted)
- Events are published to RabbitMQ
- Events are only published if the transaction succeeds
- Event order matches operation order
- No duplicate events for failed transactions
Event Schema Evolution
Event classes are in the SDK module and versioned:- Events should be backward compatible
- Add new fields with default values
- Never remove or rename existing fields
- Consider event versioning for breaking changes
CQRS Pattern
Events enable Command Query Responsibility Segregation: Command Side (this service):- Accepts commands that modify order state
- Publishes events for all state changes
- Optimized for write operations
- Consumes events to build read models
- Provides optimized queries without joins
- Eventually consistent with command side
Local Development
To run the service with RabbitMQ locally:Access RabbitMQ Management UI
Open http://localhost:15672
- Username:
guest - Password:
guest