Merge pull request #14404 from sam-gardner/BAEL-6721-example-code-for-notification-of-a-complete-task

BAEL-6721 Add example code for notification of a complete task
This commit is contained in:
davidmartinezbarua 2023-07-22 10:26:28 -03:00 committed by GitHub
commit 8db7da5c11
7 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,18 @@
package com.baeldung.concurrent.notificationforcompletetask;
import java.util.concurrent.FutureTask;
public class AlertingFutureTask extends FutureTask<String> {
private final CallbackInterface callback;
public AlertingFutureTask(Runnable runnable, Callback callback) {
super(runnable, null);
this.callback = callback;
}
@Override
protected void done() {
callback.taskDone("task details here");
}
}

View File

@ -0,0 +1,21 @@
package com.baeldung.concurrent.notificationforcompletetask;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class AlertingThreadPoolExecutor extends ThreadPoolExecutor {
private final CallbackInterface callback;
public AlertingThreadPoolExecutor(CallbackInterface callback) {
super(1, 1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10));
this.callback = callback;
}
@Override
protected void afterExecute(Runnable r, Throwable t) {
super.afterExecute(r, t);
callback.taskDone("runnable details here");
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.concurrent.notificationforcompletetask;
public class Callback implements CallbackInterface {
public void taskDone(String details){
System.out.println("task complete: " + details);
// Alerts/notifications go here
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.concurrent.notificationforcompletetask;
public interface CallbackInterface {
void taskDone(String details);
}

View File

@ -0,0 +1,22 @@
package com.baeldung.concurrent.notificationforcompletetask;
public class RunnableImpl implements Runnable {
private final Runnable task;
private final CallbackInterface callback;
private final String taskDoneMessage;
public RunnableImpl(Runnable task, CallbackInterface callback, String taskDoneMessage) {
this.task = task;
this.callback = callback;
this.taskDoneMessage = taskDoneMessage;
}
public void run() {
task.run();
callback.taskDone(taskDoneMessage);
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.concurrent.notificationforcompletetask;
public class Task implements Runnable{
@Override
public void run() {
System.out.println("Task in progress");
// Business logic goes here
}
}

View File

@ -0,0 +1,45 @@
package com.baeldung.concurrent.notificationforcompletetask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import org.junit.Test;
public class NotificationsForCompleteTasksUnitTest {
@Test
public void whenImplementingRunnable_thenReceiveNotificationOfCompletedTask() {
Task task = new Task();
Callback callback = new Callback();
RunnableImpl runnableImpl = new RunnableImpl(task, callback, "ready for next task");
runnableImpl.run();
}
@Test
public void whenUsingCompletableFuture_thenReceiveNotificationOfCompletedTask() {
Task task = new Task();
Callback callback = new Callback();
CompletableFuture.runAsync(task)
.thenAccept(result -> callback.taskDone("completion details: " + result));
}
@Test
public void whenUsingThreadPoolExecutor_thenReceiveNotificationOfCompletedTask(){
Task task = new Task();
Callback callback = new Callback();
AlertingThreadPoolExecutor executor = new AlertingThreadPoolExecutor(callback);
executor.submit(task);
}
@Test
public void whenUsingFutureTask_thenReceiveNotificationOfCompletedTask(){
Task task = new Task();
Callback callback = new Callback();
FutureTask<String> future = new AlertingFutureTask(task, callback);
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.submit(future);
}
}