OSDN Git Service

タスクマネージャにキャンセル処理を実装
authoryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 05:26:26 +0000 (14:26 +0900)
committeryukihane <yukihane.feather@gmail.com>
Tue, 30 Aug 2011 05:26:26 +0000 (14:26 +0900)
frontend/src/yukihane/inqubus/manager/TaskManage.java

index 311c622..47b037e 100644 (file)
@@ -70,6 +70,19 @@ public class TaskManage {
         return false;
     }
 
+    public synchronized boolean cancel(int rowId) {
+        final ManageTarget<DownloadResult> down = downloadTargets.get(rowId);
+        if (down != null) {
+            return down.getFuture().cancel(true);
+        }
+        final ManageTarget<ConvertResult> conv = convertTargets.get(rowId);
+        if (conv != null) {
+            return conv.getFuture().cancel(true);
+        }
+
+        return false;
+    }
+
     private class DownloadListener extends TaskManageInnerListener<DownloadResult, DownloadProgress> {
 
         private DownloadListener(int rowId) {
@@ -80,7 +93,7 @@ public class TaskManage {
         public void done(DownloadResult result) {
             super.done(result);
             synchronized (TaskManage.this) {
-                final ManageTarget<DownloadResult> mt = downloadTargets.remove(Integer.valueOf(getRowId()));
+                final ManageTarget<DownloadResult> mt = removeRequest(getRowId());
                 final RequestProcess request = mt.getRequest();
                 if (request.getConvertProfile().isConvert()) {
                     final DownloadProfile dp = request.getDownloadProfile();
@@ -102,6 +115,11 @@ public class TaskManage {
         protected TaskKind getKind() {
             return TaskKind.DOWNLOAD;
         }
+
+        @Override
+        protected ManageTarget<DownloadResult> removeRequest(int rowId) {
+            return downloadTargets.remove(rowId);
+        }
     }
 
     private class ConvertListener extends TaskManageInnerListener<ConvertResult, ConvertProgress> {
@@ -119,9 +137,14 @@ public class TaskManage {
         public void done(ConvertResult result) {
             super.done(result);
             synchronized (TaskManage.this) {
-                convertTargets.remove(getRowId());
+                removeRequest(getRowId());
             }
         }
+
+        @Override
+        protected ManageTarget<ConvertResult> removeRequest(int rowId) {
+            return convertTargets.remove(rowId);
+        }
     }
 
     abstract class TaskManageInnerListener<T, V extends PercentageReportable & MessageReportable> implements WorkerListener<T, V> {
@@ -160,11 +183,18 @@ public class TaskManage {
         }
 
         @Override
-        public void cancelled() {
+        public final void cancelled() {
             logger.log(Level.FINE, "cancelled: {0}", toString());
+            synchronized (TaskManage.this) {
+                removeRequest(rowId);
+            }
             notify(TaskStatus.CANCELLED);
         }
 
+        /**
+         * この処理をオーバライドしてキューからリクエストを削除する必要があります.
+         * @param result 処理結果.
+         */
         @Override
         public void done(T result) {
             logger.log(Level.FINE, "done: {0}", result);
@@ -172,10 +202,15 @@ public class TaskManage {
         }
 
         @Override
-        public void error(Throwable th) {
+        public final void error(Throwable th) {
             logger.log(Level.SEVERE, "error", th);
+            synchronized (TaskManage.this) {
+                removeRequest(rowId);
+            }
             notify(TaskStatus.ERROR, 0.0, th.getMessage());
         }
+
+        protected abstract ManageTarget<T> removeRequest(int rowId);
     }
 
     class ManageTarget<T> {