You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
162
165
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>
164
174
165
175
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.
170
176
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>
172
186
173
187
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>
178
195
179
196
---
180
197
181
198
## How to Add a New Task Type
182
199
183
-
Adding a new task type requires just 3 steps:
200
+
Adding a new task type requires just **3 steps**:
184
201
185
-
### Step 1: Create a Handler
202
+
**Step 1 — Create a Handler**
186
203
187
204
```java
188
205
packagecom.taskqueue.handler;
@@ -195,28 +212,28 @@ public class MyCustomHandler implements TaskHandler {
195
212
@Override
196
213
publicTaskResultexecute(Tasktask) {
197
214
long start =System.currentTimeMillis();
198
-
215
+
199
216
// Your logic here
200
217
String result ="Processed: "+ task.getPayload();
201
-
218
+
202
219
long duration =System.currentTimeMillis() - start;
0 commit comments