A robust notification service built with Spring Boot that handles multiple notification channels including email, SMS (via Twilio), and push notifications (via Firebase).
This project provides a centralized notification management system that:
- Processes notification requests from Kafka
- Supports multiple notification channels (email, SMS, push)
- Uses PostgreSQL for data persistence
- Implements circuit breaking with Resilience4j
- Includes scheduled tasks with ShedLock for distributed lock management
- Java 24
- Spring Boot 3.4.4
- Spring Cloud 2024.0.1
- Kafka for event-driven architecture
- PostgreSQL for data storage
- Firebase Admin for push notifications
- Twilio for SMS delivery
- Thymeleaf for email templating
- Docker for containerization
- Resilience4j for circuit breaking
- ShedLock for distributed task scheduling
- JDK 24
- Docker and Docker Compose
- Firebase service account (for push notifications)
- Twilio account for SMS functionality
- SMTP server access for email notifications
-
Clone the repository:
git clone https://github.com/yourusername/notification-management.git cd notification-management -
Configure environment variables: Create a
.envfile in thenotification-servicedirectory with the following variables:# Database SPRING_DATASOURCE_URL=jdbc:postgresql://postgres:5432/notification_db SPRING_DATASOURCE_USERNAME=postgres SPRING_DATASOURCE_PASSWORD=password # Kafka SPRING_KAFKA_BOOTSTRAP_SERVERS=kafka:29092 SPRING_KAFKA_CONSUMER_GROUP_ID=notification-group SPRING_KAFKA_CONSUMER_AUTO_OFFSET_RESET=earliest # Email SPRING_MAIL_HOST=your-smtp-host SPRING_MAIL_PORT=587 SPRING_MAIL_USERNAME=your-email SPRING_MAIL_PASSWORD=your-password SPRING_MAIL_PROPERTIES_MAIL_SMTP_AUTH=true SPRING_MAIL_PROPERTIES_MAIL_SMTP_STARTTLS_ENABLE=true # Twilio TWILIO_ACCOUNT_SID=your-twilio-sid TWILIO_AUTH_TOKEN=your-twilio-token TWILIO_PHONE_NUMBER=your-twilio-phone # Firebase FIREBASE_CONFIG_PATH=/app/firebase-service-account.json # Application SERVER_PORT=8081 NOTIFICATION_RETRY_MAX_ATTEMPTS=3 NOTIFICATION_RETRY_INITIAL_INTERVAL=5000 -
Place your Firebase service account JSON file in the project root as
firebase-service-account.json -
Start the services:
docker-compose up -d
-
Start infrastructure services:
docker-compose up -d postgres pgadmin zookeeper kafka -
Run the notification service:
cd notification-service ./mvnw spring-boot:run
The notification service architecture consists of the following components:
- API Layer: REST endpoints for direct notification requests
- Kafka Consumers: Event-driven notification processing
- Channel Providers: Implementation for each notification channel
- Persistence Layer: Database storage for notification tracking
- Scheduler: Retry mechanism for failed notifications
- Notification requests arrive via REST API or Kafka topics
- Requests are validated and transformed into internal notification models
- The notification dispatcher routes to the appropriate channel provider
- Providers attempt delivery and report success/failure
- Results are persisted to the database
- Failed notifications are scheduled for retry
- Uses Spring Mail with Thymeleaf templates
- Supports HTML formatting and attachments
- Tracks delivery status
- Integrates with Twilio API for SMS delivery
- Supports international phone numbers
- Provides delivery status tracking
- Uses Firebase Cloud Messaging
- Supports tokens and topics
- Handles rich notifications with actions
POST /api/v1/notifications
Request body:
{
"recipient": "[email protected]",
"channel": "EMAIL",
"subject": "Important Notification",
"content": "This is an important message",
"metadata": {
"priority": "HIGH",
"category": "ALERT"
},
"templateId": "welcome-template",
"templateData": {
"userName": "John Doe",
"activationLink": "https://example.com/activate"
}
}GET /api/v1/notifications/{id}
Response:
{
"id": "550e8400-e29b-41d4-a716-446655440000",
"status": "DELIVERED",
"sentAt": "2025-04-17T14:32:22Z",
"deliveredAt": "2025-04-17T14:32:25Z",
"failureReason": null,
"retryCount": 0
}The service uses the following main tables:
- notifications: Stores all notification records
- notification_templates: Email and push notification templates
- notification_preferences: User channel preferences
- notification_delivery_attempts: Tracks delivery attempts and failures
- shedlock: Used by ShedLock for distributed locking
The service consumes from the following Kafka topics:
notification.email.requests- For email notificationsnotification.sms.requests- For SMS notificationsnotification.push.requests- For push notifications
And produces to:
notification.status.updates- For notification status changes
PgAdmin is available at http://localhost:8080 with:
- Email: [email protected]
- Password: admin
The service provides the following endpoints for monitoring:
/actuator/health- Health check endpoint/actuator/metrics- Metrics endpoint/actuator/prometheus- Prometheus metrics
The included Docker Compose file sets up the complete environment. For production, consider:
- Using separate database credentials
- Setting up proper Kafka security
- Configuring SSL for all services
- Using Docker Swarm or Kubernetes for orchestration
Sample Kubernetes manifests are available in the k8s/ directory, including:
- Deployments
- Services
- ConfigMaps
- Secrets
- Ingress rules
The service is highly configurable through application properties:
# Notification settings
notification.default-expiry=24h
notification.batch-size=100
notification.channels.email.enabled=true
notification.channels.sms.enabled=true
notification.channels.push.enabled=true
# Circuit breaker settings
resilience4j.circuitbreaker.instances.emailService.failureRateThreshold=50
resilience4j.circuitbreaker.instances.emailService.waitDurationInOpenState=10000
# ShedLock configuration
shedlock.defaultLockAtMostFor=10m- Follow the Google Java Style Guide
- Write unit tests for all new features
- Update documentation when changing functionality
- Create issues for bugs and feature requests
This project is licensed under the MIT - see the LICENSE file for details.
- Spring Boot team for the excellent framework
- Confluent for Kafka images
- Twilio for SMS capabilities
- Firebase for push notification support
- The open source community for various libraries used in this project