Skip to content

Commit dedce0e

Browse files
committed
docs: enhance README formatting and improve clarity of instructions
1 parent 08b61c6 commit dedce0e

1 file changed

Lines changed: 113 additions & 96 deletions

File tree

README.md

Lines changed: 113 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,102 @@
1+
<div align="center">
2+
13
# JavaTaskQueue
24

3-
> A distributed-style Task Queue and Job Scheduler in pure Java 17 — inspired by Celery and Sidekiq.
5+
**A distributed-style Task Queue and Job Scheduler in pure Java 17**
6+
7+
*Inspired by Celery and Sidekiq*
48

59
[![CI](https://github.com/rajparihar281/JavaTaskQueue/actions/workflows/ci.yml/badge.svg)](https://github.com/rajparihar281/JavaTaskQueue/actions)
10+
![Java](https://img.shields.io/badge/Java-17-orange?logo=openjdk)
11+
![Maven](https://img.shields.io/badge/Maven-3.8+-blue?logo=apachemaven)
12+
![License](https://img.shields.io/badge/License-MIT-green)
13+
14+
</div>
615

716
---
817

918
## Architecture
1019

1120
```
12-
┌──────────────────────────────────────────────────────────────────┐
13-
│ JavaTaskQueue │
14-
│ │
15-
│ ┌─────────────┐ ┌──────────────┐ ──────────────────┐ │
16-
│ │ TCP Server │────▶│ TaskBroker │────▶│ WorkerPool │ │
17-
│ │ (port 9090) │ │ (Priority │ │ (4 threads) │ │
18-
│ │ │ │ Queue) │ │ │
19-
│ └──────┬───────┘ └──────┬───────┘ └─────────────────┘ │
20-
│ │ │
21-
│ │ │
22-
│ │ ┌──────┴───────┐ ──────────────────┐ │
23-
│ │ │ Delayed │ TaskHandler │ │
24-
│ │ │ Scheduler │ Registry │ │
25-
│ │ └──────────────┘ │ ├─ EchoHandler │ │
26-
│ │ │ ├─ SleepHandler │ │
27-
│ ▼ │ └─ ComputeHndlr │ │
28-
│ ┌──────────────┐ ──────────────────┘ │
29-
│ │ SQLite │ │
30-
│ │ Persistence │ │
31-
│ │ (taskqueue │ │
32-
│ │ .db) │ │
33-
│ └──────────────┘ │
34-
│ │
35-
│ ┌──────────────┐ │
36-
│ │ TaskQueue │ (Separate process — interactive REPL client) │
37-
│ │ Client
38-
│ └──────────────┘ │
39-
└──────────────────────────────────────────────────────────────────┘
21+
┌─────────────────────────────────────────────────────────────────────────
22+
JavaTaskQueue
23+
24+
│ ┌───────────── ┌──────────────┐ ┌──────────────────────┐ │
25+
│ │ TCP Server │────▶│ TaskBroker │────▶│ WorkerPool │ │
26+
│ │ (port 9090) │ │ (Priority │ (4 threads) │ │
27+
│ │ │ │ Queue) │ │ │
28+
│ └──────┬───────┘ └──────┬───────┘ └──────────┬───────────┘ │
29+
│ │
30+
│ │
31+
│ │ ┌──────┴─────── ┌──────────────────────┐ │
32+
│ │ │ Delayed TaskHandler │ │
33+
│ │ │ Scheduler Registry │ │
34+
│ │ └────────────── ├─ EchoHandler │ │
35+
│ │ ├─ SleepHandler │ │
36+
│ ▼ └─ ComputeHandler │ │
37+
│ ┌──────────────┐ └──────────────────────┘ │
38+
│ │ SQLite │
39+
│ │ Persistence │
40+
│ │ (taskqueue │
41+
│ │ .db) │
42+
│ └──────────────┘
43+
44+
│ ┌──────────────┐
45+
│ │ TaskQueue │ (Separate process — interactive REPL client)
46+
│ │ Client
47+
│ └──────────────┘
48+
└─────────────────────────────────────────────────────────────────────────
4049
```
4150

4251
---
4352

4453
## Quick Start
4554

46-
### Prerequisites
47-
- Java 17+
48-
- Maven 3.8+
55+
> **Prerequisites:** Java 17+ · Maven 3.8+
4956
50-
### Build
5157
```bash
58+
# 1. Build
5259
mvn clean package
53-
```
5460

55-
### Start the Server
56-
```bash
61+
# 2. Start the server
5762
java -jar target/JavaTaskQueue-jar-with-dependencies.jar
58-
```
5963

60-
### Start the Client (separate terminal)
61-
```bash
64+
# 3. Start the client (separate terminal)
6265
java -cp target/JavaTaskQueue-jar-with-dependencies.jar com.taskqueue.client.TaskQueueClient
63-
```
6466

65-
### Run Tests
66-
```bash
67+
# 4. Run tests
6768
mvn test
6869
```
6970

7071
---
7172

7273
## Command Reference
7374

74-
| Command | Syntax | Description |
75-
|---------|--------|-------------|
76-
| **SUBMIT** | `SUBMIT {"type":"...","payload":"...","priority":"HIGH\|NORMAL\|LOW"}` | Submit a new task to the queue |
77-
| **STATUS** | `STATUS <taskId>` | Check the current status of a task |
78-
| **LIST** | `LIST` | List all tasks (JSON array) |
79-
| **CANCEL** | `CANCEL <taskId>` | Cancel a pending task |
80-
| **SHUTDOWN** | `SHUTDOWN` | Gracefully shut down the server |
75+
### Server Commands
76+
77+
| Command | Syntax | Description |
78+
|:-----------|:--------------------------------------------------------------------------|:---------------------------------------|
79+
| `SUBMIT` | `SUBMIT {"type":"...","payload":"...","priority":"HIGH\|NORMAL\|LOW"}` | Submit a new task to the queue |
80+
| `STATUS` | `STATUS <taskId>` | Check the current status of a task |
81+
| `LIST` | `LIST` | List all tasks (JSON array) |
82+
| `CANCEL` | `CANCEL <taskId>` | Cancel a pending task |
83+
| `SHUTDOWN` | `SHUTDOWN` | Gracefully shut down the server |
8184

8285
### Built-in Task Types
8386

84-
| Type | Payload Format | Description |
85-
|------|---------------|-------------|
86-
| `ECHO` | Any string | Echoes the payload back |
87-
| `SLEEP` | Integer (ms) | Sleeps for the specified duration |
88-
| `COMPUTE` | `FACTORIAL:N` or `ISPRIME:N` | Computes factorial or checks primality |
87+
| Type | Payload Format | Description |
88+
|:----------|:------------------------------|:-----------------------------------------|
89+
| `ECHO` | Any string | Echoes the payload back |
90+
| `SLEEP` | Integer (ms) | Sleeps for the specified duration |
91+
| `COMPUTE` | `FACTORIAL:N` or `ISPRIME:N` | Computes factorial or checks primality |
8992

9093
---
9194

9295
## Example Session
9396

94-
```
97+
**Terminal 1 — Start the server:**
98+
99+
```console
95100
$ java -jar target/JavaTaskQueue-jar-with-dependencies.jar
96101
╔═══════════════════════════════════════════════════════╗
97102
║ JavaTaskQueue v1.0.0 ║
@@ -101,9 +106,9 @@ $ java -jar target/JavaTaskQueue-jar-with-dependencies.jar
101106
JavaTaskQueue started on port 9090
102107
```
103108

104-
In another terminal:
109+
**Terminal 2 — Start the client:**
105110

106-
```
111+
```console
107112
$ java -cp target/JavaTaskQueue-jar-with-dependencies.jar com.taskqueue.client.TaskQueueClient
108113
Connecting to JavaTaskQueue server at localhost:9090...
109114
Connected! Type 'help' for available commands.
@@ -140,9 +145,9 @@ jtq> SUBMIT {"type":"SLEEP","payload":"2000","priority":"LOW"}
140145

141146
jtq> LIST
142147
[
143-
{ "taskId": "a1b2c3d4-...", "type": "COMPUTE", "status": "DONE", ... },
144-
{ "taskId": "f7e8d9c0-...", "type": "ECHO", "status": "DONE", ... },
145-
{ "taskId": "1a2b3c4d-...", "type": "SLEEP", "status": "RUNNING", ... }
148+
{ "taskId": "a1b2c3d4-...", "type": "COMPUTE", "status": "DONE", ... },
149+
{ "taskId": "f7e8d9c0-...", "type": "ECHO", "status": "DONE", ... },
150+
{ "taskId": "1a2b3c4d-...", "type": "SLEEP", "status": "RUNNING", ... }
146151
]
147152

148153
jtq> exit
@@ -153,36 +158,48 @@ Goodbye!
153158

154159
## Design Decisions
155160

156-
### Why `PriorityBlockingQueue`?
161+
<details>
162+
<summary><strong>Why <code>PriorityBlockingQueue</code>?</strong></summary>
157163

158164
The `PriorityBlockingQueue` from `java.util.concurrent` provides a thread-safe, unbounded queue that automatically orders elements by their natural ordering (or a comparator). This is perfect for a task queue where:
159-
- **Priority matters**: HIGH-priority tasks are dequeued before LOW-priority tasks.
160-
- **Blocking semantics**: Worker threads can block on `poll()` without busy-waiting.
161-
- **Thread safety**: No external synchronization needed for queue operations.
162165

163-
### Why SQLite for Persistence?
166+
- **Priority matters** — HIGH-priority tasks are dequeued before LOW-priority tasks.
167+
- **Blocking semantics** — Worker threads can block on `poll()` without busy-waiting.
168+
- **Thread safety** — No external synchronization needed for queue operations.
169+
170+
</details>
171+
172+
<details>
173+
<summary><strong>Why SQLite for Persistence?</strong></summary>
164174

165175
SQLite provides durable, ACID-compliant persistence with zero configuration:
166-
- **No server required**: The database is a single file (`taskqueue.db`) — no setup needed.
167-
- **Crash recovery**: On restart, pending and running tasks are recovered from the database and resubmitted to the broker.
168-
- **Minimal footprint**: The `xerial/sqlite-jdbc` driver is a single JAR dependency.
169-
- **Atomic operations**: Transactions ensure data integrity even during unexpected shutdowns.
170176

171-
### Why the Strategy Pattern for Handlers?
177+
- **No server required** — The database is a single file (`taskqueue.db`) — no setup needed.
178+
- **Crash recovery** — On restart, pending and running tasks are recovered from the database and resubmitted to the broker.
179+
- **Minimal footprint** — The `xerial/sqlite-jdbc` driver is a single JAR dependency.
180+
- **Atomic operations** — Transactions ensure data integrity even during unexpected shutdowns.
181+
182+
</details>
183+
184+
<details>
185+
<summary><strong>Why the Strategy Pattern for Handlers?</strong></summary>
172186

173187
The `TaskHandler` interface and `TaskHandlerRegistry` implement the Strategy pattern:
174-
- **Open/Closed Principle**: New task types can be added by implementing a new handler and registering it — no existing code needs to change.
175-
- **Testability**: Handlers are isolated units that can be tested independently.
176-
- **Runtime flexibility**: Handlers can be registered/replaced at runtime.
177-
- **Default fallback**: The registry returns an `EchoHandler` for unknown types, ensuring the system never crashes due to a missing handler.
188+
189+
- **Open/Closed Principle** — New task types can be added by implementing a new handler and registering it — no existing code needs to change.
190+
- **Testability** — Handlers are isolated units that can be tested independently.
191+
- **Runtime flexibility** — Handlers can be registered/replaced at runtime.
192+
- **Default fallback** — The registry returns an `EchoHandler` for unknown types, ensuring the system never crashes due to a missing handler.
193+
194+
</details>
178195

179196
---
180197

181198
## How to Add a New Task Type
182199

183-
Adding a new task type requires just 3 steps:
200+
Adding a new task type requires just **3 steps**:
184201

185-
### Step 1: Create a Handler
202+
**Step 1Create a Handler**
186203

187204
```java
188205
package com.taskqueue.handler;
@@ -195,28 +212,28 @@ public class MyCustomHandler implements TaskHandler {
195212
@Override
196213
public TaskResult execute(Task task) {
197214
long start = System.currentTimeMillis();
198-
215+
199216
// Your logic here
200217
String result = "Processed: " + task.getPayload();
201-
218+
202219
long duration = System.currentTimeMillis() - start;
203-
return new TaskResult(task.getTaskId(), TaskStatus.DONE,
220+
return new TaskResult(task.getTaskId(), TaskStatus.DONE,
204221
result, "", duration);
205222
}
206223
}
207224
```
208225

209-
### Step 2: Register the Handler
226+
**Step 2Register the Handler**
210227

211-
In `Main.java`, add the registration line:
228+
In `Main.java`, add:
212229

213230
```java
214231
registry.register("MY_CUSTOM", new MyCustomHandler());
215232
```
216233

217-
### Step 3: Submit Tasks
234+
**Step 3Submit Tasks**
218235

219-
```
236+
```console
220237
jtq> SUBMIT {"type":"MY_CUSTOM","payload":"your data here","priority":"NORMAL"}
221238
```
222239

@@ -226,21 +243,21 @@ jtq> SUBMIT {"type":"MY_CUSTOM","payload":"your data here","priority":"NORMAL"}
226243

227244
```
228245
JavaTaskQueue/
229-
├── pom.xml # Maven build configuration
230-
├── .github/workflows/ci.yml # GitHub Actions CI
231-
├── README.md # This file
246+
├── pom.xml # Maven build configuration
247+
├── .github/workflows/ci.yml # GitHub Actions CI
248+
├── README.md # This file
232249
└── src/
233250
├── main/java/com/taskqueue/
234-
│ ├── Main.java # Application entry point
235-
│ ├── model/ # Data models (Task, TaskStatus, etc.)
236-
│ ├── broker/ # Task queue broker
237-
│ ├── worker/ # Worker pool and threads
238-
│ ├── handler/ # Task execution handlers
239-
│ ├── server/ # TCP server and client handler
240-
│ ├── client/ # Interactive REPL client
241-
│ ├── persistence/ # SQLite persistence layer
242-
│ └── scheduler/ # Delayed task scheduler
243-
└── test/java/com/taskqueue/ # Unit tests
251+
│ ├── Main.java # Application entry point
252+
│ ├── model/ # Data models (Task, TaskStatus, etc.)
253+
│ ├── broker/ # Task queue broker
254+
│ ├── worker/ # Worker pool and threads
255+
│ ├── handler/ # Task execution handlers
256+
│ ├── server/ # TCP server and client handler
257+
│ ├── client/ # Interactive REPL client
258+
│ ├── persistence/ # SQLite persistence layer
259+
│ └── scheduler/ # Delayed task scheduler
260+
└── test/java/com/taskqueue/ # Unit tests
244261
```
245262

246263
---

0 commit comments

Comments
 (0)