BAEL-6721 Add example code for notification of a complete task
This commit is contained in:
parent
2546a2e218
commit
712e8b89e2
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,5 @@
|
||||||
|
package com.baeldung.concurrent.notificationForCompleteTask;
|
||||||
|
|
||||||
|
public interface CallbackInterface {
|
||||||
|
void taskDone(String details);
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue