Skip to content

MrPC7/task-execution-system

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 Concurrent Task Execution System (Spring Boot)

📌 Overview

This project implements a concurrent task execution system using Java 17+, Spring Boot, and Spring MVC.

It allows clients to:

  • Submit long-running tasks asynchronously
  • Execute tasks using a fixed-size worker thread pool
  • Maintain FIFO task execution order
  • Check task status at any time
  • Stop queued or running tasks safely

The system is designed to demonstrate backend concurrency fundamentals, thread safety, and robust REST API design.


🧠 Key Concepts Covered

  • Fixed-size ExecutorService
  • FIFO task queue using BlockingQueue
  • Thread-safe task state using AtomicReference
  • Task cancellation using thread interruption
  • Non-blocking REST request handling using CompletableFuture
  • Safe concurrent access using ConcurrentHashMap

🏗️ High-Level Architecture

Client (Postman / API Client)
        |
        v
TaskController (REST APIs)
        |
        v
TaskManagerService
 ├── ExecutorService (Fixed Thread Pool)
 ├── BlockingQueue<TaskWrapper> (FIFO Queue)
 └── ConcurrentHashMap<String, TaskWrapper> (Task Store)
        |
        v
Worker Threads (execute tasks)

🔄 Task Lifecycle

Each task can be in exactly one of the following states:

State Description
QUEUED Accepted, waiting for a worker
RUNNING Currently executing
DONE Completed successfully
STOPPED Cancelled before or during execution

State transitions are thread-safe and strictly controlled.


⚙️ Threading Model

  • A fixed-size thread pool limits concurrent execution

  • FIFO order is guaranteed using a BlockingQueue

  • Each task:

    • Owns a CompletableFuture<TaskStatus>
    • Owns a Future<?> for interruption
  • REST request threads never execute tasks directly

  • Worker threads are reused efficiently


🧪 REST API Endpoints

1️⃣ Queue Task

Endpoint

POST /queueTask

Request Body

{
  "id": "task-123",
  "task": "example-task",
  "taskParams": {
    "key": "value"
  },
  "time": 5
}

Behavior

  • Enqueues task in FIFO order
  • Executes immediately if a worker is free
  • Blocks response until task is DONE or STOPPED
  • Rejects duplicate task IDs

Response

{
  "id": "task-123",
  "status": "DONE"
}

2️⃣ Check Task Status

Endpoint

POST /checkStatus

Request Body

{
  "id": "task-123"
}

Behavior

  • Returns immediately
  • Reflects current task state

Response

{
  "id": "task-123",
  "status": "RUNNING"
}

3️⃣ Stop Task

Endpoint

POST /stopTask

Request Body

{
  "id": "task-123"
}

Behavior

  • QUEUED → removed from queue
  • RUNNING → interrupted
  • DONE → no operation

Response

{
  "id": "task-123",
  "status": "STOPPED"
}

🔐 Concurrency Guarantees

  • No more than N tasks run concurrently
  • FIFO order preserved for queued tasks
  • Thread-safe state transitions
  • Safe task cancellation without race conditions
  • No worker thread leakage

🔧 Configuration

application.properties

task.worker-pool-size=1
server.port=8080
  • task.worker-pool-size controls concurrency
  • Increasing value increases parallelism

🧩 Project Structure

src/main/java/com/Pranshu/TaskExecutionSystem
├── config
│   └── ExecutorConfig.java
├── controller
│   └── TaskController.java
├── service
│   └── TaskManagerService.java
├── model
│   ├── TaskRequest.java
│   ├── TaskResponse.java
│   ├── TaskStatus.java
│   └── TaskWrapper.java
└── TaskExecutionSystemApplication.java

⚖️ Assumptions & Trade-offs

Assumptions

  • Task IDs are client-generated and unique
  • Task duration is simulated using Thread.sleep
  • In-memory storage is sufficient for assessment scope

Trade-offs

  • No persistence (data lost on restart)
  • Blocking response for /queueTask (per requirements)
  • No retry mechanism

🚀 Possible Improvements

  • Persist tasks using a database
  • Add rate limiting
  • Add retry / timeout policies
  • Use message queues (Kafka / RabbitMQ)
  • Add metrics and monitoring
  • Global exception handling

✅ Conclusion

This project demonstrates:

  • Strong understanding of concurrency
  • Correct use of Java threading primitives
  • Clean REST API design
  • Robust backend engineering fundamentals

About

A high‑performance Spring Boot backend for executing long‑running tasks with controlled concurrency, FIFO scheduling, and safe cancellation via REST APIs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages