TasksWatchDog does not remove finished tasks from watches #PL-6016

This commit is contained in:
Yuriy Artamonov 2015-10-06 14:35:54 +00:00
parent 29fd310b11
commit 24650c4d9f
6 changed files with 50 additions and 14 deletions

View File

@ -48,9 +48,12 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
checkNotNull(task);
// create task handler
TaskExecutor<T, V> taskExecutor = new DesktopTaskExecutor<>(task);
DesktopTaskExecutor<T, V> taskExecutor = new DesktopTaskExecutor<>(task);
TaskHandlerImpl<T, V> taskHandler = new TaskHandlerImpl<>(taskExecutor, watchDog);
return new TaskHandlerImpl<>(taskExecutor, watchDog);
taskExecutor.setTaskHandler(taskHandler);
return taskHandler;
}
/**
@ -71,6 +74,7 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
private volatile boolean isInterrupted = false;
private Map<String, Object> params;
private TaskHandlerImpl<T, V> taskHandler;
private DesktopTaskExecutor(BackgroundTask<T, V> runnableTask) {
this.runnableTask = runnableTask;
@ -87,6 +91,7 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
@Override
protected final V doInBackground() throws Exception {
Thread.currentThread().setName("BackgroundTaskThread");
try {
if (!isInterrupted) {
// do not run any activity if canceled before start
@ -112,7 +117,10 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
} catch (Exception ex) {
if (!(ex instanceof InterruptedException) && !isCancelled())
taskException = ex;
} finally {
watchDog.removeTask(taskHandler);
}
return result;
}
@ -212,5 +220,9 @@ public class DesktopBackgroundWorker implements BackgroundWorker {
public final void handleProgress(T... changes) {
publish(changes);
}
public void setTaskHandler(TaskHandlerImpl<T,V> taskHandler) {
this.taskHandler = taskHandler;
}
}
}

View File

@ -19,7 +19,7 @@ import javax.swing.*;
public class DesktopTasksWatchDog extends TasksWatchDog {
@Override
protected boolean checkHangup(long actualTimeMs, final TaskHandlerImpl taskHandler) {
protected synchronized boolean checkHangup(long actualTimeMs, final TaskHandlerImpl taskHandler) {
long timeout = taskHandler.getTimeoutMs();

View File

@ -17,24 +17,31 @@ public interface WatchDog {
String NAME = "cuba_BackgroundWorker_WatchDog";
/**
* Add task under WatchDog control
* Add task under WatchDog control.
*
* @param backroundTask Task handler
* @param taskHandler task handler
*/
void manageTask(TaskHandlerImpl backroundTask);
void manageTask(TaskHandlerImpl taskHandler);
/**
* Remove finished, canceled or hangup tasks
* Task completed, remove it from watches.
*
* @param taskHandler task handler
*/
void removeTask(TaskHandlerImpl taskHandler);
/**
* Remove finished, canceled or hangup tasks.
*/
void cleanupTasks();
/**
* Stop execution of all background tasks
* Stop execution of all background tasks.
*/
void stopTasks();
/**
* @return Active tasks count
* @return active tasks count
*/
int getActiveTasksCount();
}

View File

@ -10,6 +10,7 @@ import com.haulmont.cuba.core.sys.AppContext;
import com.haulmont.cuba.gui.executors.BackgroundTaskHandler;
import com.haulmont.cuba.gui.executors.WatchDog;
import javax.annotation.concurrent.ThreadSafe;
import javax.inject.Inject;
import java.util.LinkedHashSet;
import java.util.LinkedList;
@ -22,6 +23,7 @@ import java.util.Set;
* @author artamonov
* @version $Id$
*/
@ThreadSafe
public abstract class TasksWatchDog implements WatchDog {
@Inject
@ -43,7 +45,7 @@ public abstract class TasksWatchDog implements WatchDog {
long actual = timeSource.currentTimestamp().getTime();
List<BackgroundTaskHandler> forRemove = new LinkedList<>();
List<TaskHandlerImpl> forRemove = new LinkedList<>();
for (TaskHandlerImpl task : watches) {
if (task.isCancelled() || task.isDone()) {
forRemove.add(task);
@ -80,10 +82,15 @@ public abstract class TasksWatchDog implements WatchDog {
/**
* {@inheritDoc}
*
* @param backroundTask Task handler
* @param taskHandler Task handler
*/
@Override
public synchronized void manageTask(TaskHandlerImpl backroundTask) {
watches.add(backroundTask);
public synchronized void manageTask(TaskHandlerImpl taskHandler) {
watches.add(taskHandler);
}
@Override
public synchronized void removeTask(TaskHandlerImpl taskHandler) {
watches.remove(taskHandler);
}
}

View File

@ -109,6 +109,8 @@ public class WebBackgroundWorker implements BackgroundWorker {
// create task handler
final TaskHandlerImpl<T, V> taskHandler = new TaskHandlerImpl<>(taskExecutor, watchDog);
taskExecutor.setTaskHandler(taskHandler);
// add timer to AppWindow for UI ping
CubaTimer.ActionListener timerListener = new CubaTimer.ActionListener() {
private long intentVersion = 0;
@ -187,6 +189,7 @@ public class WebBackgroundWorker implements BackgroundWorker {
private volatile Exception taskException = null;
private Map<String, Object> params;
private TaskHandlerImpl<T, V> taskHandler;
private WebTaskExecutor(App app, BackgroundTask<T, V> runnableTask,
WebTimerListener webTimerListener) {
@ -208,6 +211,7 @@ public class WebBackgroundWorker implements BackgroundWorker {
@Override
public final void run() {
Thread.currentThread().setName("BackgroundTaskThread");
// Set security permissions
AppContext.setSecurityContext(securityContext);
@ -249,6 +253,8 @@ public class WebBackgroundWorker implements BackgroundWorker {
}
// Remove from executions
app.removeBackgroundTask(this);
watchDog.removeTask(taskHandler);
}
}
@ -400,5 +406,9 @@ public class WebBackgroundWorker implements BackgroundWorker {
}
}
}
public void setTaskHandler(TaskHandlerImpl<T,V> taskHandler) {
this.taskHandler = taskHandler;
}
}
}

View File

@ -23,7 +23,7 @@ public class BackgroundTaskManager {
private transient Set<Thread> taskSet;
public BackgroundTaskManager() {
taskSet = Collections.synchronizedSet(new LinkedHashSet<Thread>());
taskSet = Collections.synchronizedSet(new LinkedHashSet<>());
}
/**