Notification Service
The negotiator features a comprehensive notification service for notifying users about various events tied to negotiations. The system is built using a strategy pattern with event-driven architecture to ensure extensibility and maintainability.
Architecture Overview
The notification system consists of several key components:
Core Components
- NotificationListener - A generic event listener that receives Spring Application Events and dispatches them to appropriate handlers
- NotificationStrategy - An interface that defines how specific events should be handled
- NotificationService - The service responsible for creating and managing in-app notifications
Notifications vs Emails
The system handles two types of communications:
- Notifications - In-app notifications stored in the database that users can view within the negotiator interface
- Emails - Email notifications sent to users' email addresses for important events
Both are triggered by the same events but serve different purposes. Notifications provide an audit trail and in-app alerts, while emails ensure users are notified even when not actively using the platform.
Event Flow
- Business events (e.g., negotiation state changes, new posts) are published as Spring Application Events
- The
NotificationListener
receives these events asynchronously via@TransactionalEventListener
- The listener dispatches events to registered
NotificationStrategy
implementations - Each strategy handles its specific event type and creates appropriate notifications and/or emails
Email Implementation
Email delivery is implemented using SpringMail.
Configuration
To configure Spring Mail, set the following environment variables or update the properties file for the appropriate profile:
SPRING_MAIL_HOST="smtp.example"
SPRING_MAIL_PORT=1025
SPRING_MAIL_USERNAME="user"
SPRING_MAIL_PASSWORD="pass"
Email Templates
Email templates use Thymeleaf as the template engine for dynamic content. Templates can be managed through the negotiator's admin interface.
Developer Guide: Extending the Notification System
Adding New Event Types and Handlers
To add support for new events:
1. Create an Event Class
public class MyCustomEvent extends ApplicationEvent {
private final String negotiationId;
public MyCustomEvent(Object source, String negotiationId) {
super(source);
this.negotiationId = negotiationId;
}
// Getters...
}
2. Implement a Notification Strategy
@Component
public class MyCustomEventHandler implements NotificationStrategy<MyCustomEvent> {
private final NotificationService notificationService;
@Override
public Class<MyCustomEvent> getSupportedEventType() {
return MyCustomEvent.class;
}
@Override
@Transactional
public void notify(MyCustomEvent event) {
// Create notifications and/or trigger emails
NotificationCreateDTO notification = new NotificationCreateDTO(
userIds, "Title", "Body", event.getNegotiationId()
);
notificationService.createNotifications(notification);
}
}
3. Publish the Event
@Service
public class MyBusinessService {
private final ApplicationEventPublisher eventPublisher;
public void performBusinessAction(String negotiationId) {
// Business logic...
eventPublisher.publishEvent(new MyCustomEvent(this, negotiationId));
}
}
The NotificationListener
will automatically discover and register new handlers, dispatching events to appropriate strategies.