Add module devlok-queue
This commit is contained in:
parent
93ef00f83d
commit
33ef243cc3
@ -0,0 +1,9 @@
|
|||||||
|
package de.weapie.devlok.queue;
|
||||||
|
|
||||||
|
public enum QueueStatus {
|
||||||
|
|
||||||
|
FAILED,
|
||||||
|
SUCCESSFUL,
|
||||||
|
UNKNOWN;
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
package de.weapie.devlok.queue.tasks;
|
||||||
|
|
||||||
|
import de.weapie.devlok.queue.QueueStatus;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
|
public abstract class Task {
|
||||||
|
|
||||||
|
public abstract QueueStatus perform();
|
||||||
|
public abstract void result(CompletableFuture<QueueStatus> statusCompletableFuture);
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
package de.weapie.devlok.queue.tasks;
|
||||||
|
|
||||||
|
import de.weapie.devlok.queue.QueueStatus;
|
||||||
|
|
||||||
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
import java.util.concurrent.ExecutorService;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
|
public class TaskProcessor {
|
||||||
|
|
||||||
|
private TaskQueue taskQueue;
|
||||||
|
private ExecutorService executor;
|
||||||
|
private long delayMillis;
|
||||||
|
private boolean running = true;
|
||||||
|
|
||||||
|
public TaskProcessor(TaskQueue taskQueue, long delayMillis) {
|
||||||
|
this.taskQueue = taskQueue;
|
||||||
|
this.executor = Executors.newSingleThreadExecutor();
|
||||||
|
this.delayMillis = delayMillis;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void start() {
|
||||||
|
this.executor.submit(() -> {
|
||||||
|
while(this.running) {
|
||||||
|
try {
|
||||||
|
int processedTasks = 0;
|
||||||
|
|
||||||
|
if(this.taskQueue.empty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(this.taskQueue.size() < this.taskQueue.getThreshold()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (processedTasks < this.taskQueue.getThreshold()) {
|
||||||
|
Task task = this.taskQueue.dequeue();
|
||||||
|
QueueStatus status = task.perform();
|
||||||
|
task.result(CompletableFuture.completedFuture(status));
|
||||||
|
|
||||||
|
processedTasks++;
|
||||||
|
|
||||||
|
if(this.taskQueue.getThreshold() == processedTasks) {
|
||||||
|
this.taskQueue.getProcessClosed().accept(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
TimeUnit.MILLISECONDS.sleep(this.delayMillis);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(processedTasks < this.taskQueue.getThreshold()) {
|
||||||
|
TimeUnit.MILLISECONDS.sleep(this.delayMillis);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
this.running = false;
|
||||||
|
this.executor.shutdownNow();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,65 @@
|
|||||||
|
package de.weapie.devlok.queue.tasks;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
|
@ToString
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TaskQueue {
|
||||||
|
|
||||||
|
private BlockingQueue<Task> queue = new LinkedBlockingQueue<>();
|
||||||
|
private final int delayInMillis;
|
||||||
|
@Getter
|
||||||
|
private final int threshold;
|
||||||
|
@Getter
|
||||||
|
private final Consumer<Boolean> processClosed;
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
private int performedTasks = 0;
|
||||||
|
private boolean processing = false;
|
||||||
|
private TaskProcessor taskProcessor;
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public void enqueue(Task task) {
|
||||||
|
this.queue.add(task);
|
||||||
|
|
||||||
|
if(!this.processing) {
|
||||||
|
this.startProcessing();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SneakyThrows
|
||||||
|
public Task dequeue() {
|
||||||
|
this.performedTasks++;
|
||||||
|
return this.queue.poll();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void startProcessing() {
|
||||||
|
if (!this.processing) {
|
||||||
|
this.taskProcessor = new TaskProcessor(this, delayInMillis);
|
||||||
|
this.taskProcessor.start();
|
||||||
|
this.processing = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void stopProcessing() {
|
||||||
|
if (this.taskProcessor != null) {
|
||||||
|
this.taskProcessor.stop();
|
||||||
|
this.taskProcessor = null;
|
||||||
|
this.processing = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean empty() {
|
||||||
|
return this.queue.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int size() {
|
||||||
|
return this.queue.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -7,3 +7,4 @@ include("devlok-netty")
|
|||||||
include("devlok-databases")
|
include("devlok-databases")
|
||||||
include("devlok-redis")
|
include("devlok-redis")
|
||||||
include("devlok-logger")
|
include("devlok-logger")
|
||||||
|
include("devlok-queue")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user