Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions src/main/java/javax/enterprise/concurrent/ManagedExecutors.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package javax.enterprise.concurrent;

import java.util.Locale;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;

Expand All @@ -14,65 +14,80 @@ public static boolean isCurrentThreadShutdown() {
return thread instanceof ManageableThread && ((ManageableThread)thread).isShutdown();
}

public static <V> Callable<V> managedTask(Callable<V> task, ManagedTaskListener taskListener) {
public static <V> Callable<V> managedTask(Callable<V> task, ManagedTaskListener taskListener) throws IllegalArgumentException {
return new ManagedCallable<V>(task, taskListener, null);
}

public static <V> Callable<V> managedTask(Callable<V> task, Map<String, String> executionProperties, ManagedTaskListener taskListener) {
public static <V> Callable<V> managedTask(Callable<V> task, Map<String, String> executionProperties, ManagedTaskListener taskListener) throws IllegalArgumentException {
return new ManagedCallable<V>(task, taskListener, executionProperties);
}

static class ManagedCallable<V> implements Callable<V>, ManagedTask {
static class ManagedCallable<V> extends AbstractManagedTask implements Callable<V> {
private final Callable<V> task;
private final ManagedTaskListener taskListener;
private final Map<String, String> executionProperties;

ManagedCallable(final Callable<V> task, final ManagedTaskListener taskListener, final Map<String, String> executionProperties) {
super(task, taskListener, executionProperties);
this.task = task;
this.taskListener = taskListener;
this.executionProperties = executionProperties;
}

public V call() throws Exception {
return task.call();
}

public Map<String, String> getExecutionProperties() {
return executionProperties;
}

public ManagedTaskListener getManagedTaskListener() {
return taskListener;
}
}

public static Runnable managedTask(Runnable task, ManagedTaskListener taskListener) {
public static Runnable managedTask(Runnable task, ManagedTaskListener taskListener) throws IllegalArgumentException {
return new ManagedRunnable(task, taskListener, null);
}

public static Runnable managedTask(Runnable task, Map<String, String> executionProperties, ManagedTaskListener taskListener) {
public static Runnable managedTask(Runnable task, Map<String, String> executionProperties, ManagedTaskListener taskListener) throws IllegalArgumentException {
return new ManagedRunnable(task, taskListener, executionProperties);
}

static class ManagedRunnable implements Runnable, ManagedTask {
static class ManagedRunnable extends AbstractManagedTask implements Runnable {
private final Runnable task;
private final ManagedTaskListener taskListener;
private final Map<String, String> executionProperties;

ManagedRunnable(final Runnable task, final ManagedTaskListener taskListener, final Map<String, String> executionProperties) {
super(task, taskListener, executionProperties);
this.task = task;
this.taskListener = taskListener;
this.executionProperties = executionProperties;
}

public void run() {
task.run();
}
}

abstract static class AbstractManagedTask implements ManagedTask {
private final ManagedTaskListener taskListener;
private Map<String, String> executionProperties = null;

AbstractManagedTask(Object task, ManagedTaskListener taskListener, Map<String, String> executionProperties) throws IllegalArgumentException {
if (task == null) {
throw new IllegalArgumentException("null task");
}
final ManagedTask managedTask = task instanceof ManagedTask ? (ManagedTask) task : null;
if (taskListener != null) {
this.taskListener = taskListener;
} else {
this.taskListener = managedTask != null ? managedTask.getManagedTaskListener() : null;
}
if (managedTask != null && managedTask.getExecutionProperties() != null) {
this.executionProperties = new HashMap<String, String>(managedTask.getExecutionProperties());
}
if (executionProperties != null) {
if (this.executionProperties == null) {
this.executionProperties = new HashMap<String, String>(executionProperties);
} else {
this.executionProperties.putAll(executionProperties);
}
}
}

@Override
public Map<String, String> getExecutionProperties() {
return executionProperties;
}

@Override
public ManagedTaskListener getManagedTaskListener() {
return taskListener;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
/**
*/
public interface ManagedTaskListener {
void taskAborted(Future<?> future, ManagedExecutorService executor, Throwable exception);
void taskAborted(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception);

void taskDone(Future<?> future, ManagedExecutorService executor, Throwable exception);
void taskDone(Future<?> future, ManagedExecutorService executor, Object task, Throwable exception);

void taskStarting(Future<?> future, ManagedExecutorService executor);
void taskStarting(Future<?> future, ManagedExecutorService executor, Object task);

void taskSubmitted(Future<?> future, ManagedExecutorService executor);
void taskSubmitted(Future<?> future, ManagedExecutorService executor, Object task);
}