A production-ready Spring Boot microservice for handling notifications via multiple channels including email, real-time WebSocket, and queue-based messaging with RabbitMQ.
- π§ Email Notifications: SMTP-based email sending with HTML/plain text support
- β‘ Real-time Notifications: WebSocket-based real-time messaging via STOMP
- π Queue-based Processing: RabbitMQ message queuing with fallback to async processing
- π JWT Authentication: Secure API access with JWT tokens
- πΎ Database Support: MySQL for production, H2 for development
- π³ Docker Ready: Full containerization with Docker Compose
- π Monitoring: Spring Boot Actuator with health checks and metrics
- π§ͺ Comprehensive Testing: Unit tests with JUnit 5 and Mockito
- Java 17+
- Maven 3.6+
- Docker & Docker Compose (optional, for containerized setup)
- MySQL 8.0+ (for production)
- RabbitMQ 3.12+ (optional, falls back to async processing)
# Clone the repository
git clone https://github.com/kenzycodex/spring-notification-service.git
cd spring-notification-service
# Run with Maven (uses H2 database)
mvn spring-boot:run
# π Service running at http://localhost:8080# Build the project
mvn clean compile
# Run with development profile (uses H2 database)
mvn spring-boot:run
# Or specify profile explicitly
mvn spring-boot:run -Dspring-boot.run.profiles=devThe service will start on http://localhost:8080
# Create environment file
echo "MAIL_USERNAME=your-email@gmail.com" > .env
echo "MAIL_PASSWORD=your-app-password" >> .env
echo "MAIL_FROM=noreply@yourcompany.com" >> .env
echo "JWT_SECRET=your-very-long-secure-secret-key-here" >> .env
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f notification-service| Variable | Description | Default | Required |
|---|---|---|---|
SPRING_PROFILES_ACTIVE |
Active profile (dev/prod) | dev |
No |
DB_HOST |
MySQL host | mysql |
Prod only |
DB_PORT |
MySQL port | 3306 |
Prod only |
DB_NAME |
Database name | notification_db |
Prod only |
DB_USERNAME |
Database username | notification_user |
Prod only |
DB_PASSWORD |
Database password | notification_password |
Prod only |
RABBITMQ_HOST |
RabbitMQ host | rabbitmq |
No |
RABBITMQ_USERNAME |
RabbitMQ username | notification_user |
No |
RABBITMQ_PASSWORD |
RabbitMQ password | notification_password |
No |
MAIL_HOST |
SMTP host | smtp.gmail.com |
No |
MAIL_USERNAME |
SMTP username | - | Yes for email |
MAIL_PASSWORD |
SMTP password | - | Yes for email |
MAIL_FROM |
From email address | - | No |
JWT_SECRET |
JWT secret key | - | Yes |
JWT_EXPIRATION |
JWT expiration (seconds) | 3600 |
No |
- Enable 2-Factor Authentication on your Gmail account
- Generate an App Password:
Google Account > Security > App passwords - Use your Gmail address as
MAIL_USERNAME - Use the generated app password as
MAIL_PASSWORD
POST /api/auth/register
Content-Type: application/json
{
"username": "testuser",
"password": "password123",
"email": "user@example.com"
}POST /api/auth/login
Content-Type: application/json
{
"username": "testuser",
"password": "password123"
}Response:
{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
"type": "Bearer",
"username": "testuser",
"message": "Authentication successful"
}Add header: Authorization: Bearer {your-jwt-token}
POST /api/notify/email
Content-Type: application/json
{
"to": "recipient@example.com",
"subject": "Test Notification",
"body": "This is a test email notification",
"html": false
}POST /api/notify/realtime
Content-Type: application/json
{
"topic": "user-notifications",
"message": "New message for you!",
"recipient": "username"
}POST /api/notify/queue
Content-Type: application/json
{
"type": "EMAIL",
"recipient": "user@example.com",
"message": "Subject|Email body content",
"priority": 0
}GET /api/notify/healthConnect to WebSocket endpoint for real-time notifications:
// Connect to WebSocket
const socket = new SockJS('http://localhost:8080/ws');
const stompClient = Stomp.over(socket);
stompClient.connect({}, function (frame) {
console.log('Connected: ' + frame);
// Subscribe to a topic
stompClient.subscribe('/topic/user-notifications', function (message) {
const notification = JSON.parse(message.body);
console.log('Received notification:', notification);
});
});mvn testmvn verifymvn jacoco:reportUse the provided Postman collection or curl commands:
# Register user
curl -X POST http://localhost:8080/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"password123","email":"test@example.com"}'
# Login and get token
TOKEN=$(curl -X POST http://localhost:8080/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username":"testuser","password":"password123"}' | jq -r '.token')
# Send email notification
curl -X POST http://localhost:8080/api/notify/email \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d '{"to":"test@example.com","subject":"Test","body":"Hello World"}'- Import Project: File β Open β Select
pom.xml - Set JDK: File β Project Structure β Project β SDK: Java 17
- Enable Annotation Processing: Settings β Build β Compiler β Annotation Processors
- Install Plugins: Spring Boot, Database Navigator (optional)
- Follow Spring Boot best practices
- Use meaningful variable and method names
- Add JavaDoc for public methods
- Maintain test coverage above 80%
- Create Feature Branch:
git checkout -b feature/your-feature - Add Service Layer: Business logic in
servicepackage - Add Controller: REST endpoints in
controllerpackage - Add Tests: Unit tests for all new code
- Update Documentation: README and API docs
- Build JAR:
mvn clean package -DskipTests- Run with Production Profile:
java -jar -Dspring.profiles.active=prod target/notification-service-1.0.0.jar- Docker Deployment:
# Build image
docker build -t notification-service:1.0.0 .
# Run container
docker run -d \
-p 8080:8080 \
-e SPRING_PROFILES_ACTIVE=prod \
-e DB_HOST=your-mysql-host \
-e MAIL_USERNAME=your-email@gmail.com \
-e MAIL_PASSWORD=your-app-password \
-e JWT_SECRET=your-secure-secret \
notification-service:1.0.0apiVersion: apps/v1
kind: Deployment
metadata:
name: notification-service
spec:
replicas: 3
selector:
matchLabels:
app: notification-service
template:
metadata:
labels:
app: notification-service
spec:
containers:
- name: notification-service
image: notification-service:1.0.0
ports:
- containerPort: 8080
env:
- name: SPRING_PROFILES_ACTIVE
value: "prod"
- name: DB_HOST
value: "mysql-service"
# Add other environment variables- Application:
GET /actuator/health - Database: Included in health endpoint
- RabbitMQ: Included if configured
- Prometheus metrics:
/actuator/prometheus - Application metrics:
/actuator/metrics
- Log files:
logs/notification-service.log - Log levels configurable per environment
- Structured JSON logging in production
- JWT tokens for API authentication
- Secure password encoding with BCrypt
- CORS configuration for cross-origin requests
- Input validation on all endpoints
- SQL injection prevention with JPA
- XSS protection with proper encoding
-
Email Not Sending
- Check SMTP credentials
- Verify Gmail app password
- Check firewall settings
-
Database Connection Failed
- Verify MySQL is running
- Check connection parameters
- Ensure database exists
-
RabbitMQ Connection Issues
- Service falls back to async processing
- Check RabbitMQ server status
- Verify connection credentials
-
JWT Token Issues
- Ensure JWT_SECRET is set
- Check token expiration
- Verify Authorization header format
Enable debug logging:
logging:
level:
com.notificationservice: DEBUG
org.springframework.web: DEBUGβββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β REST Client ββββββ NotificationAPI ββββββ JWT Security β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
ββββββββββββββ΄βββββββββββββ
β NotificationService β
ββββββββββββββ¬βββββββββββββ
β
βββββββββββββββββββββββββΌββββββββββββββββββββββββ
β β β
βββββββββΌβββββββ ββββββββββββΌββββββββββ ββββββββββΌβββββββββ
β EmailService β β WebSocketService β β QueueService β
ββββββββββββββββ ββββββββββββββββββββββ βββββββββββββββββββ
β β β
βββββββββΌβββββββ ββββββββββββΌββββββββββ ββββββββββΌβββββββββ
β SMTP β β WebSocket β β RabbitMQ β
ββββββββββββββββ ββββββββββββββββββββββ βββββββββββββββββββ
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
For support and questions:
- Create an issue in the repository
- Email: kenzycodex@gmail.com
- Documentation: README Docs
- Java 17+ installed
- Maven dependencies resolved
- Database configuration set
- Email SMTP configured
- JWT secret configured
- RabbitMQ setup (optional)
- Docker images built
- Environment variables set
- Health checks passing
- Tests passing
- Security review completed