3D bin-packing optimization engine for shipping cartons using Spring Boot, Kafka, MongoDB, and hexagonal architecture.
The Cartonization Service is responsible for optimizing shipping carton utilization through advanced 3D bin-packing algorithms. Within the Paklog fulfillment platform, this bounded context calculates the most efficient way to pack items into shipping cartons, minimizing shipping costs while ensuring proper fit and safety.
Cartonization & Packing Optimization - Manages carton definitions and calculates optimal packing solutions for fulfillment orders.
- Carton - Defines available shipping carton types with dimensions, weight limits, and metadata
- PackingSolution - Represents the calculated optimal packing strategy for a set of items
- Package - Individual package within a packing solution
- ItemToPack - Item requiring packing with its dimensions and quantity
- DimensionSet - Length, width, height with units
- Weight - Weight value with unit
- SKU - Stock keeping unit identifier
- CartonId - Unique carton identifier
- PackingRules - Business rules for packing constraints
- CartonCreatedEvent - New carton type added to system
- CartonUpdatedEvent - Carton type modified
- CartonDeactivatedEvent - Carton type removed from active use
- PackingSolutionCalculated - Optimal packing solution computed
- PackingAlgorithmService - Implements 3D bin-packing algorithms
- BusinessRuleValidator - Validates packing business rules
- Cartonization: Process of determining optimal carton sizes for shipment
- Bin Packing: Algorithm for fitting items into containers
- Packing Solution: Calculated arrangement of items in cartons
- Available to Promise: Items that can be packed and shipped
src/main/java/com/paklog/cartonization/
├── domain/ # Core business logic
│ ├── model/
│ │ ├── aggregate/ # Carton, PackingSolution
│ │ ├── entity/ # Package
│ │ └── valueobject/ # DimensionSet, Weight, SKU
│ ├── service/ # Domain services
│ └── event/ # Domain events
├── application/ # Use cases & orchestration
│ ├── port/
│ │ ├── in/ # Input ports (use cases)
│ │ └── out/ # Output ports (repositories, clients)
│ └── service/ # Application services
└── infrastructure/ # External adapters
├── persistence/ # MongoDB repositories
├── messaging/ # Kafka publishers
├── web/ # REST controllers
└── config/ # Configuration
- Hexagonal Architecture - Clear separation between domain and infrastructure
- Domain-Driven Design - Rich domain models with business logic
- CQRS - Separation of command and query responsibilities
- Event-Driven Architecture - Asynchronous communication via domain events
- Transactional Outbox Pattern - Reliable event publishing
- Repository Pattern - Abstraction over data persistence
- Dependency Inversion - Domain depends on abstractions, not implementations
- SOLID Principles - Clean, maintainable, and testable code
- Java 21 - Programming language
- Spring Boot 3.2.0 - Application framework
- Maven - Build and dependency management
- MongoDB - Document database for aggregates
- Spring Data MongoDB - Data access layer
- Redis - Caching layer for performance
- Apache Kafka - Event streaming platform
- Spring Kafka - Kafka integration
- CloudEvents - Standardized event format
- Spring Web MVC - REST API framework
- SpringDoc OpenAPI - API documentation
- Bean Validation - Input validation
- Spring Boot Actuator - Health checks and metrics
- Micrometer - Metrics collection
- Prometheus - Metrics aggregation
- Grafana - Metrics visualization
- Loki - Log aggregation
- JUnit 5 - Unit testing framework
- Testcontainers - Integration testing with containers
- Mockito - Mocking framework
- AssertJ - Fluent assertions
- Docker - Containerization
- Docker Compose - Local development environment
- Kubernetes - Container orchestration
- ✅ Hexagonal Architecture (Ports and Adapters)
- ✅ Domain-Driven Design tactical patterns
- ✅ CQRS for command/query separation
- ✅ Event-Driven Architecture
- ✅ Microservices architecture
- ✅ RESTful API design
- ✅ SOLID principles
- ✅ Clean Code practices
- ✅ Comprehensive unit and integration testing
- ✅ Test-Driven Development (TDD)
- ✅ Dependency injection
- ✅ Immutable value objects
- ✅ CloudEvents specification
- ✅ Transactional Outbox Pattern
- ✅ At-least-once delivery semantics
- ✅ Event versioning strategy
- ✅ Schema evolution support
- ✅ Structured logging (JSON)
- ✅ Distributed tracing
- ✅ Health check endpoints
- ✅ Prometheus metrics exposition
- ✅ Correlation ID propagation
- Java 21+
- Maven 3.8+
- Docker & Docker Compose
-
Clone the repository
git clone https://github.com/paklog/cartonization.git cd cartonization -
Start infrastructure services
docker-compose up -d mongodb kafka redis
-
Build and run the application
mvn spring-boot:run
-
Verify the service is running
curl http://localhost:8080/actuator/health
# Start all services
docker-compose up -d
# View logs
docker-compose logs -f cartonization
# Stop all services
docker-compose downOnce running, access the interactive API documentation:
- Swagger UI: http://localhost:8080/swagger-ui.html
- OpenAPI Spec: http://localhost:8080/v3/api-docs
POST /api/v1/cartons- Create new carton typeGET /api/v1/cartons- List all carton typesGET /api/v1/cartons/{id}- Get carton by IDPUT /api/v1/cartons/{id}- Update carton typeDELETE /api/v1/cartons/{id}- Deactivate carton typePOST /api/v1/packing-solutions- Calculate optimal packing solution
# Run unit tests
mvn test
# Run integration tests
mvn verify
# Run tests with coverage
mvn clean verify jacoco:report
# View coverage report
open target/site/jacoco/index.htmlKey configuration properties:
spring:
data:
mongodb:
uri: mongodb://localhost:27017/cartonization
kafka:
bootstrap-servers: localhost:9092
redis:
host: localhost
port: 6379
cartonization:
algorithm:
default-strategy: BEST_FIT_DECREASING
cache:
enabled: true
ttl: 3600com.paklog.cartonization.carton.created.v1com.paklog.cartonization.carton.updated.v1com.paklog.cartonization.carton.deactivated.v1com.paklog.cartonization.packing.solution.calculated.v1
All events follow the CloudEvents specification and are published to Kafka.
- Health: http://localhost:8080/actuator/health
- Metrics: http://localhost:8080/actuator/metrics
- Prometheus: http://localhost:8080/actuator/prometheus
- Follow hexagonal architecture principles
- Implement domain logic in the domain layer
- Keep infrastructure concerns separate
- Write comprehensive tests for all layers
- Document domain concepts using ubiquitous language
- Follow existing code style and conventions
Copyright © 2024 Paklog. All rights reserved.