OSDN Git Service

一定間隔で情報を更新する際に利用するタスクマネージャーを作成.まだテストはおこなっていない.
authorspark_xp <spark_xp@d8c9ecd3-d47d-4367-8645-de82c00e513f>
Tue, 14 Sep 2010 08:47:15 +0000 (08:47 +0000)
committerspark_xp <spark_xp@d8c9ecd3-d47d-4367-8645-de82c00e513f>
Tue, 14 Sep 2010 08:47:15 +0000 (08:47 +0000)
git-svn-id: http://svn.sourceforge.jp/svnroot/nt-manager/NishioTweetManager/trunk@28 d8c9ecd3-d47d-4367-8645-de82c00e513f

src/twitter/task/TweetTaskException.java [new file with mode: 0644]
src/twitter/task/TweetTaskManager.java [new file with mode: 0644]
src/twitter/task/TweetUpdateTask.java [new file with mode: 0644]

diff --git a/src/twitter/task/TweetTaskException.java b/src/twitter/task/TweetTaskException.java
new file mode 100644 (file)
index 0000000..77fc07e
--- /dev/null
@@ -0,0 +1,28 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package twitter.task;
+
+/**
+ *
+ * @author nishio
+ */
+public class TweetTaskException extends Exception {
+
+    /**
+     * Creates a new instance of <code>TweetTaskException</code> without detail message.
+     */
+    public TweetTaskException() {
+    }
+
+
+    /**
+     * Constructs an instance of <code>TweetTaskException</code> with the specified detail message.
+     * @param msg the detail message.
+     */
+    public TweetTaskException(String msg) {
+        super(msg);
+    }
+}
diff --git a/src/twitter/task/TweetTaskManager.java b/src/twitter/task/TweetTaskManager.java
new file mode 100644 (file)
index 0000000..b1f9929
--- /dev/null
@@ -0,0 +1,232 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package twitter.task;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ * タスクを放り込むと指定した間隔で実行してくれるクラス
+ * @author nishio
+ */
+public class TweetTaskManager {
+
+    /**
+     * 実際に行うタスク
+     */
+    private class TweetTaskTimerTask implements Runnable {
+        private TweetUpdateTask task;
+
+        /**
+         *
+         * @param task
+         */
+        public TweetTaskTimerTask( TweetUpdateTask task ) {
+            this.task = task;
+        }
+
+        /**
+         * 実際に実行するタスク
+         */
+        @Override
+        public void run() {
+            try {
+                //タスク実行
+                task.runTask();
+            } catch (TweetTaskException ex) {
+                Logger.getLogger(TweetTaskManager.class.getName()).log(
+                        Level.SEVERE, "TimerTask内でエラーが発生しました", ex);
+            }
+        }
+    }
+
+    /**
+     * Timerに関するデータを保持
+     */
+    private class TimerData {
+
+        private ScheduledFuture<?> future;
+        private final ScheduledExecutorService scheduler;
+        private final Runnable task;
+        private long time = 0;
+        private String timerID = null;
+
+        /**
+         * 
+         * @param task
+         * @param timerID タイマー識別の為に名前を付ける
+         */
+        public TimerData(TweetUpdateTask task, String timerID) {
+            this.task = new TweetTaskTimerTask(task);
+            scheduler = Executors.newSingleThreadScheduledExecutor();
+            if( timerID == null ) {
+                throw new NullPointerException("TimerIDが設定されていません");
+            }
+            this.timerID = timerID;
+        }
+
+        /**
+         * 更新リセット
+         */
+        public void reset() {
+            stop();
+            if (future != null) {
+                future = scheduler.scheduleAtFixedRate(task, time, time,
+                        TimeUnit.MILLISECONDS);
+            }
+        }
+
+        /**
+         * シャットダウン
+         */
+        public void shutdown() {
+            scheduler.shutdown();
+        }
+
+        /**
+         * 一定時間毎にTweetUpdateTaskを実行
+         *
+         * @param time 実行間隔[ms]
+         */
+        public void start(long time) {
+            future = scheduler.scheduleAtFixedRate(task, 0, time,
+                    TimeUnit.MILLISECONDS);
+            this.time = time;
+        }
+
+        /**
+         * タスク終了
+         */
+        public void stop() {
+            if (future != null) {
+                future.cancel(true);
+            }
+        }
+
+        /**
+         * @return the timerID
+         */
+        public String getTimerID() {
+            return timerID;
+        }
+
+        /**
+         * @param timerID the timerID to set
+         */
+        public void setTimerID(String timerID) {
+            this.timerID = timerID;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (obj == null) {
+                return false;
+            }
+            if (getClass() != obj.getClass()) {
+                return false;
+            }
+            final TimerData other = (TimerData) obj;
+            if ((this.timerID == null) ? (other.timerID != null) : !this.timerID.equals(other.timerID)) {
+                return false;
+            }
+            return true;
+        }
+
+        @Override
+        public int hashCode() {
+            int hash = 3;
+            hash = 23 * hash + (this.timerID != null ? this.timerID.hashCode() : 0);
+            return hash;
+        }
+
+    }
+
+    //タイマー
+    private List<TimerData> timerList = null;
+
+
+    /**
+     *
+     */
+    public TweetTaskManager() {
+       timerList = new ArrayList<TimerData>();
+    }
+
+    /**
+     * タスクを追加
+     * @param timerID タイマー識別の為に名前をつける
+     * @param task 実行したいタスク
+     */
+    public void addTask(String timerID, TweetUpdateTask task) throws TweetTaskException {
+        //すでに同じIDのタスクが存在していたら追加できないようにする
+        if( timerList.contains( new TimerData(null, timerID) ) ) {
+            throw new TweetTaskException("すでにタスクが存在しています");
+        }
+        //新しいタスクを追加
+        TimerData t = new TimerData(task, timerID);
+        timerList.add(t);
+    }
+
+    /**
+     * タスク実行
+     * @param timerID タイマー識別子
+     * @param period 実行間隔[ms]
+     * @return タイマーの実行に成功したかどうか
+     */
+    public boolean startTask(String timerID, long period) throws TweetTaskException {
+        boolean found = false;
+        for( TimerData t : timerList ) {
+            if( t.getTimerID().equals(timerID) ) {
+                t.start(period);
+                found = true;
+                break;
+            }
+        }
+        return found;
+    }
+
+    /**
+     * タスクを終了する
+     * @param timerID
+     * @return
+     */
+    public boolean shutdownTask(String timerID) {
+        boolean found = false;
+        for (TimerData t : timerList) {
+            if (t.getTimerID().equals(timerID)) {
+                t.stop();
+                t.shutdown();
+                found = true;
+                break;
+            }
+        }
+        return found;
+    }
+
+    /**
+     * タイマーの更新間隔をリセットする
+     * @param timerID
+     * @return
+     */
+    public boolean resetTask(String timerID) {
+        boolean found = false;
+        for (TimerData t : timerList) {
+            if (t.getTimerID().equals(timerID)) {
+                t.reset();
+                found = true;
+                break;
+            }
+        }
+        return found;
+    }
+
+}
diff --git a/src/twitter/task/TweetUpdateTask.java b/src/twitter/task/TweetUpdateTask.java
new file mode 100644 (file)
index 0000000..565bbef
--- /dev/null
@@ -0,0 +1,18 @@
+/*
+ * To change this template, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+package twitter.task;
+
+/**
+ *
+ * @author nishio
+ */
+public interface TweetUpdateTask {
+    /**
+     * 一定間隔で実行するタスクの処理
+     * @throws TweetTaskException
+     */
+    void runTask() throws TweetTaskException;
+}