Skip to content

starkbaknet/spring-boot-batch-processing

Repository files navigation

Spring Boot Batch Processing Application

A minimized Spring Boot application for processing course invitations using batch processing.

Features

  • Batch Processing: Process multiple email invitations efficiently using Spring Batch
  • Email Service: Mock email service that logs email content (can be configured for real sending)
  • Course Management: Basic CRUD operations for courses
  • Invitation Management: Create, validate, and resend course invitations
  • H2 Database: In-memory database for easy testing
  • REST API: Simple REST endpoints for testing

Fixed Issues

  1. Multiple Email Processing: Fixed the issue where only one email was processed by properly parsing comma-separated email parameters in batch configuration
  2. Batch Configuration: Simplified and fixed the batch reader, processor, and writer components
  3. Exception Handling: Improved error handling and logging throughout the application
  4. Minimized Structure: Removed unnecessary components and simplified the architecture

Quick Start

Prerequisites

  • Java 17 or higher
  • Maven 3.6 or higher

Running the Application

  1. Navigate to the project directory:

    cd /home/stark/dev/batch-processing
  2. Run the application:

    mvn spring-boot:run
  3. The application will start on http://localhost:8080

Sample Data

The application automatically creates sample data:

  • 2 courses (IDs: 1, 2)
  • 3 users (students and a teacher)

Testing the Batch Processing

1. Send Batch Invitations

curl -X POST http://localhost:8080/api/invitations/batch \
  -H "Content-Type: application/json" \
  -d '{
    "courseId": 1,
    "emails": ["test1@example.com", "test2@example.com", "test3@example.com"]
  }'

2. Send Single Invitations (Non-batch)

curl -X POST http://localhost:8080/api/invitations/single \
  -H "Content-Type: application/json" \
  -d '{
    "courseId": 1,
    "emails": ["test4@example.com", "test5@example.com"]
  }'

3. Validate an Invitation

First, check the logs for the invitation code generated, then:

curl -X POST "http://localhost:8080/api/invitations/validate?invitationCode=ABC123&email=test1@example.com&courseId=1"

4. Create a New Course

curl -X POST http://localhost:8080/api/courses \
  -H "Content-Type: application/json" \
  -d '{
    "title": "New Course",
    "description": "A new course description",
    "isPrivate": true
  }'

Monitoring

  • H2 Console: Available at http://localhost:8080/h2-console

    • JDBC URL: jdbc:h2:mem:testdb
    • Username: sa
    • Password: (empty)
  • Logs: Check application logs for batch processing details and email content

Email Configuration

By default, the application logs email content instead of sending real emails. To enable real email sending:

  1. Set environment variables:

    export MAIL_USERNAME=your-email@gmail.com
    export MAIL_PASSWORD=your-app-password
  2. Uncomment the mailSender.send(message) lines in EmailService.java

Project Structure

src/main/java/com/example/batch/
├── BatchProcessingApplication.java          # Main application class
├── config/
│   ├── BatchConfiguration.java              # Basic batch configuration
│   ├── DataInitializer.java                 # Sample data initialization
│   └── StudentInvitationBatchConfig.java    # Batch job configuration
├── controller/
│   ├── CourseController.java                # Course REST endpoints
│   └── InvitationController.java            # Invitation REST endpoints
├── dto/
│   └── StudentInvitationRequest.java        # Request DTO
├── model/
│   ├── Course.java                          # Course entity
│   ├── StudentCourseInvitation.java         # Invitation entity
│   └── User.java                            # User entity
├── repository/
│   ├── CourseRepository.java                # Course data access
│   ├── StudentCourseInvitationRepository.java # Invitation data access
│   └── UserRepository.java                  # User data access
├── service/
│   ├── CourseService.java                   # Course business logic
│   ├── EmailService.java                    # Email sending service
│   ├── InvitationBatchService.java          # Batch job launcher
│   └── StudentCourseInvitationService.java  # Invitation business logic
└── util/
    └── CodeGenerator.java                   # Utility for generating codes

Architecture Overview

  • Spring Boot 2.7.18: Compatible with Java 11
  • Spring Batch: For processing multiple email invitations efficiently
  • Spring Data JPA: For database operations with Hibernate
  • H2 Database: In-memory database for development and testing
  • REST API: Simple endpoints for testing and integration
  • Batch Processing: Fixed to handle multiple emails correctly

Key Improvements Made

  1. Fixed Multiple Email Processing: The main issue where only one email was processed is now resolved by properly parsing comma-separated email parameters in the batch configuration
  2. Java 11 Compatibility: Downgraded from Spring Boot 3.x to 2.7.18 and fixed text block syntax
  3. Minimized Structure: Removed unnecessary components and simplified the architecture
  4. Improved Error Handling: Centralized error handling with proper logging throughout the application
  5. Mock Email Service: Easy to test without requiring real email configuration - logs email content instead
  6. Automatic Sample Data: Ready-to-use test data (2 courses, 3 users) created on startup
  7. Fixed Batch Configuration: Properly handles comma-separated emails and processes them in chunks
  8. Enhanced Logging: Better visibility into batch processing steps and email sending

Testing

Run the automated test script:

./test-batch-processing.sh

This script will:

  • Start the application
  • Test all REST endpoints
  • Verify batch processing functionality
  • Confirm multiple email processing works correctly
  • Show application logs
  • Clean up automatically

About

A minimized Spring Boot application for processing course invitations using batch processing.

Topics

Resources

Stars

Watchers

Forks