jp.co.fujitsu.reffi.client.swing.model
クラス TimerProcessCore

java.lang.Object
  上位を拡張 jp.co.fujitsu.reffi.client.swing.model.BaseModel
      上位を拡張 jp.co.fujitsu.reffi.client.swing.model.TimerProcessCore
すべての実装されたインタフェース:
java.util.concurrent.Callable<java.lang.Object>, Model

public class TimerProcessCore
extends BaseModel

[概 要]

設定されたタイマー契機で任意のコード、Actionを実行する機能モデルです。

[詳 細]

タイマーが実行されるとModelProcessEvent.SUCCESSイベントが発行されます。
イベントを受け取ったコントローラのモデル処理監視リスナはBaseController#successForwardをコールします。
定期的に実行するコードを、TimerProcessCoreをリザーブしたBaseAction継承アクションのsuccessForward に実装しておくことで、タイマー契機でコードを繰り返し実行出来るようになります。

intervalActionフィールドが設定されていると、ModelProcessEvent.SUCCESSイベントの発行の代わりに intervalActionの実行をコントローラに委譲します。
繰り返し実行されるのはアクションのsuccessForwardでは無く、intervalActionになります。

intervalActionの指定有無に関わらず、TimerProcessCoreをリザーブしたアクションのcompleteが コールされるのは、TimerProcessCoreが停止したタイミングです。

[備 考]

使用例)
・ローカルJVMのメモリ遷移を表すJFreeChartグラフを定期的に更新する。
        package demo.jfreechart.action;

        import java.lang.management.ManagementFactory;
        import java.lang.management.MemoryPoolMXBean;
        import java.util.List;
        
        import jp.co.fujitsu.reffi.client.swing.action.BaseAction;
        import jp.co.fujitsu.reffi.client.swing.controller.ParameterMapping;
        import jp.co.fujitsu.reffi.client.swing.event.ModelProcessEvent;
        import jp.co.fujitsu.reffi.client.swing.model.Model;
        import jp.co.fujitsu.reffi.client.swing.model.TimerProcessCore;
        
        import org.jfree.data.time.Millisecond;
        
        import demo.jfreechart.TimeSeriesChartIFrame;
        
        public class TimeSeriesChartInitAction extends BaseAction {
        
                MemoryPoolMXBean eden;
                
                MemoryPoolMXBean survivor;
                
                MemoryPoolMXBean tenured;
                
                MemoryPoolMXBean perm;
                
                @Override
                protected boolean prepare(ParameterMapping parameterMapping) throws Exception {

                        // 世代別メモリのMXBeanを取得してフィールドに保存します。
                        List memoryPoolMXBeans = ManagementFactory
                                        .getMemoryPoolMXBeans();
                        for (MemoryPoolMXBean mpbean : memoryPoolMXBeans) {
                                if("Eden Space".equals(mpbean.getName())) {
                                        this.eden = mpbean;
                                }else if("Survivor Space".equals(mpbean.getName())) {
                                        this.survivor = mpbean;
                                }else if("Tenured Gen".equals(mpbean.getName())) {
                                        this.tenured = mpbean;
                                }else if("Perm Gen".equals(mpbean.getName())) {
                                        this.perm = mpbean;
                                }
                        }
        
                        return true;
                }
        
                @Override
                protected void reserveModels(List> models) {
                        // タイマー実行機能モデルをリザーブ
                        models.add(TimerProcessCore.class);
                }
        
                @Override
                public boolean nextModel(int index, ModelProcessEvent prev, Model next) throws Exception {
                        if(index == 0) {
                                // 1秒毎にタイマー実行され、successForwardが定期実行されるよう設定します。
                                // タイマーをストップさせる為に必要なtimerIdは、画面クラスのハッシュコードにしています。
                                Object eventSource = getParameterMapping().getEventSource();
                                ((TimerProcessCore)next).setTimerId(String.valueOf(eventSource.hashCode()));
                                ((TimerProcessCore)next).setPeriod(1000);
                        }
        
                        return true;
                }
        
                @Override
                public void successForward(int index, Model model, Object result) throws Exception {
                        // 1秒毎にコントローラからコールバックされます。
                        // JFreeChartの描画データを追加しています。
                        TimeSeriesChartIFrame iframe = (TimeSeriesChartIFrame) getParameterMapping().getEventSource();
        
                        iframe.edenSeries.add(new Millisecond(), eden.getUsage().getUsed());
                        iframe.survivorSeries.add(new Millisecond(), survivor.getUsage().getUsed());
                        iframe.tenuredSeries.add(new Millisecond(), tenured.getUsage().getUsed());
                        iframe.permSeries.add(new Millisecond(), perm.getUsage().getUsed());
                }
        }
 
・サーバ時刻を取得するアクションを定期的に実行する。
        package demo.timeraction.action;
        
        import java.util.List;
        
        import javax.swing.JLabel;
        
        import jp.co.fujitsu.reffi.client.swing.action.BaseAction;
        import jp.co.fujitsu.reffi.client.swing.controller.ParameterMapping;
        import jp.co.fujitsu.reffi.client.swing.event.ModelProcessEvent;
        import jp.co.fujitsu.reffi.client.swing.model.HTTPRequestCore;
        import jp.co.fujitsu.reffi.client.swing.model.Model;
        
        // タイマーで定期実行されるアクションです。
        public class PeriodicAction extends BaseAction {
                
                @Override
                protected boolean prepare(ParameterMapping parameterMapping) throws Exception {
                        System.out.println("PeriodicAction#prepare");
                        return true;
                }
        
                @Override
                protected void reserveModels(List> models) {
                        // サーバに時刻を問い合わせる為のHTTPリクエスト機能モデルをリザーブ
                        models.add(HTTPRequestCore.class);
                }
                
                
                @Override
                public boolean nextModel(int index, ModelProcessEvent prev, Model next) throws Exception {
                        if(index == 0) {
                                // サーバロジックは任意です(Struts等でも可)。
                                // PeriodicAccessModelはサーバ側でnew Date()をシリアライズして返却します。
                                String serverSleepMs = getComponentValueAsString("timerActionFrame.jtfServerSleepMs");
                                
                                ((HTTPRequestCore)next).setRequestUrl("/webcontroller");
                                ((HTTPRequestCore)next).addUrlParamteters("model.fqcn", "demo.server.model.PeriodicAccessModel");
                                ((HTTPRequestCore)next).addUrlParamteters("sleepMs", serverSleepMs);
                        }
                        return true;
                }
        
                @Override
                public void successForward(int index, Model model, Object result) throws Exception {
                        // 画面にサーバ時刻を表示します。
                        JLabel jlResult = (JLabel)getComponent("timerActionFrame.jlResult");
                        jlResult.setText(result.toString());
                        System.out.println("PeriodicAction#successForward");
                }
        
                @Override
                public Exception failureForward(int index, Model model, Exception e) {
                        System.out.println("PeriodicAction#failureForward");
                        return e;
                }
        
                @Override
                public void complete() {
                        System.out.println("PeriodicAction#complete");
                }
        }
 
        package demo.timeraction.action;
        
        import java.util.List;
        
        import javax.swing.JButton;
        
        import jp.co.fujitsu.reffi.client.swing.action.BaseAction;
        import jp.co.fujitsu.reffi.client.swing.controller.ParameterMapping;
        import jp.co.fujitsu.reffi.client.swing.event.ModelProcessEvent;
        import jp.co.fujitsu.reffi.client.swing.model.Model;
        import jp.co.fujitsu.reffi.client.swing.model.TimerProcessCore;
        
        // タイマー開始ボタン押下アクションです。
        public class TimerActionStartAction extends BaseAction {
                
                @Override
                protected boolean prepare(ParameterMapping parameterMapping) throws Exception {
                        
                        // 開始、停止ボタンの有無効を切り替えます。
                        JButton jbStart = (JButton)getComponent("timerActionFrame.jbStart");
                        JButton jbStop = (JButton)getComponent("timerActionFrame.jbStop");
                        
                        jbStart.setEnabled(false);
                        jbStop.setEnabled(true);
                        
                        return true;
                }
        
                @Override
                protected void reserveModels(List> models) {
                        // タイマー開始用機能モデルをリザーブ
                        models.add(TimerProcessCore.class);
                }
        
                @Override
                public boolean nextModel(int index, ModelProcessEvent prev, Model next) throws Exception {
                        if(index == 0) {
                                // 画面から初期遅延、間隔、自動終了ミリ秒数を取ります。(ここでは自動終了ミリ秒数は0です)
                                int timerInitDelayMs = 
                                        Integer.parseInt(getComponentValueAsString("timerActionFrame.jtfTimerInitDelayMs"));
                                int timerPeriodMs = 
                                        Integer.parseInt(getComponentValueAsString("timerActionFrame.jtfTimerPeriodMs"));
                                int timerStopLaterMs = 
                                        Integer.parseInt(getComponentValueAsString("timerActionFrame.jtfTimerStopLaterMs"));
                                
                                ((TimerProcessCore)next).setTimerId(String.valueOf(getOwnWindow().hashCode()));
                                ((TimerProcessCore)next).setInitialDelay(timerInitDelayMs);
                                ((TimerProcessCore)next).setPeriod(timerPeriodMs);
                                ((TimerProcessCore)next).setStopLater(timerStopLaterMs);
                                ((TimerProcessCore)next).setIntervalAction(PeriodicAction.class);
                        }
        
                        return true;
                }
        
                @Override
                public void successForward(int index, Model model, Object result) throws Exception {
                        System.out.println("TimerActionExecuteAction#successForward");
                }
        
                @Override
                public Exception failureForward(int index, Model model, Exception e) {
                        System.out.println("TimerActionExecuteAction#failureForward");
                        return e;
                }
        
                @Override
                public void complete() {
                        System.out.println("TimerActionExecuteAction#complete");
                        JButton jbStart = (JButton)getComponent("timerActionFrame.jbStart");
                        JButton jbStop = (JButton)getComponent("timerActionFrame.jbStop");
                        
                        jbStart.setEnabled(true);
                        jbStop.setEnabled(false);
                }
        } 
 
        package demo.timeraction.action;
        
        import java.util.List;
        
        import jp.co.fujitsu.reffi.client.swing.action.BaseAction;
        import jp.co.fujitsu.reffi.client.swing.event.ModelProcessEvent;
        import jp.co.fujitsu.reffi.client.swing.model.Model;
        import jp.co.fujitsu.reffi.client.swing.model.TimerProcessCore;
        
        // タイマー停止ボタン押下アクションです。
        public class TimerActionStopAction extends BaseAction {
        
                @Override
                protected void reserveModels(List> models) {
                        // タイマー停止用にリザーブ
                        models.add(TimerProcessCore.class);
                }
        
                @Override
                public boolean nextModel(int index, ModelProcessEvent prev, Model next) throws Exception {
                        if(index == 0) {
                                // タイマーを開始したtimerIdに対してタイマーストップを依頼します。
                                ((TimerProcessCore)next).setTimerId(String.valueOf(getOwnWindow().hashCode()));
                                ((TimerProcessCore)next).setStop(true);
                        }
        
                        return true;
                }
        
                @Override
                public void successForward(int index, Model model, Object result) throws Exception {
                        System.out.println("TimerActionStopAction#successForward");
                }
        
                @Override
                public Exception failureForward(int index, Model model, Exception e) {
                        System.out.println("TimerActionStopAction#failureForward");
                        return e;
                }
        
                @Override
                public void complete() {
                        System.out.println("TimerActionStopAction#complete");
                }
        }
 
・開始ボタンを押下して数秒後に停止ボタンを押下した実行結果
        PeriodicAction#prepare
        PeriodicAction#successForward
        PeriodicAction#complete
                :
                :
        PeriodicAction#prepare
        PeriodicAction#successForward
        PeriodicAction#complete
        TimerActionExecuteAction#complete
        TimerActionStopAction#successForward
        TimerActionStopAction#complete 
 

[環 境] JDK 6.0 Update 11

Copyright (c) 2008-2009 FUJITSU Japan All rights reserved.

作成者:
Project Reffi

コンストラクタの概要
TimerProcessCore()
           
 
メソッドの概要
 java.util.concurrent.ScheduledExecutorService getExecutorService()
          [概 要] タイマー実行するスケジューラサービスを返却します。
 java.util.concurrent.ScheduledFuture<?> getFuture()
          [概 要] タイマー実行中のタスクを返却します。
 long getInitialDelay()
          [概 要] 初期遅延ミリ秒数を返却します。
 java.lang.Class<? extends AbstractAction> getIntervalAction()
          [概 要] 定期実行するアクションクラスを返却します。
 long getPeriod()
          [概 要] タイマー実行する間隔ミリ秒数を返却します。
 long getStopLater()
          [概 要] タイマーを終了させるミリ秒数を返却します。
 java.lang.String getTimerId()
          [概 要] タイマーの識別子を返却します。
 boolean isStop()
          [概 要] タイマーを停止するかどうかのフラグを返却します。
 boolean isStopImmediately()
           
protected  void mainproc()
          [概 要] TimerProcessCoreManagerにタイマーの開始、停止を委譲します。
 void onStop()
          [概 要] タイマー終了時にテンプレートコールされます。
 void onTick()
          [概 要] 指定したタイマー間隔でコールされるメソッドです。
protected  void postStop()
          [概 要] タイマー終了時にテンプレートコールされるオーバーライドメソッドです。
protected  void postTick()
          [概 要] タイマーハンドラ拡張用メソッドです。
 void setExecutorService(java.util.concurrent.ScheduledExecutorService executorService)
          [概 要] タイマー実行するスケジューラサービスを設定します。
 void setFuture(java.util.concurrent.ScheduledFuture<?> future)
          [概 要] タイマー実行中のタスクを設定します。
 void setInitialDelay(long initialDelay)
          [概 要] 初期遅延ミリ秒数を設定します。
 void setIntervalAction(java.lang.Class<? extends AbstractAction> intervalAction)
          [概 要] 定期実行するアクションクラスを設定します。
 void setPeriod(long period)
          [概 要] タイマー実行する間隔ミリ秒数を設定します。
 void setStop(boolean stop)
          [概 要] タイマーを停止するかどうかのフラグを設定します。
 void setStopImmediately(boolean stopImmediately)
           
 void setStopLater(long stopLater)
          [概 要] タイマーを終了させるミリ秒数を設定します。
 void setTimerId(java.lang.String timerId)
          [概 要] タイマーの識別子を設定します。
 
クラス jp.co.fujitsu.reffi.client.swing.model.BaseModel から継承されたメソッド
addModelProcessListener, call, done, finalproc, fireModelFailure, fireModelFinished, fireModelSuccess, getController, getExecuteIndex, getListenerList, getParameterMapping, getResult, getSuccessCount, incrementSuccessCount, init, isAsync, isSkip, postproc, preproc, removeModelProcessListener, run, setAsync, setController, setExecuteIndex, setListenerList, setParameterMapping, setResult, setSkip, trap
 
クラス java.lang.Object から継承されたメソッド
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

コンストラクタの詳細

TimerProcessCore

public TimerProcessCore()
メソッドの詳細

getTimerId

public java.lang.String getTimerId()

[概 要]

タイマーの識別子を返却します。

[詳 細]

timerIdフィールドを返却します。

[備 考]

戻り値:
タイマーの識別子

setTimerId

public void setTimerId(java.lang.String timerId)

[概 要]

タイマーの識別子を設定します。

[詳 細]

timerIdフィールドを引数timerIdで設定します。

[備 考]

パラメータ:
timerId - タイマーの識別子

getInitialDelay

public long getInitialDelay()

[概 要]

初期遅延ミリ秒数を返却します。

[詳 細]

initialDelayフィールドを返却します。

[備 考]

戻り値:
初期遅延ミリ秒数

setInitialDelay

public void setInitialDelay(long initialDelay)

[概 要]

初期遅延ミリ秒数を設定します。

[詳 細]

initialDelayフィールドを引数initialDelayで設定します。

[備 考]

パラメータ:
initialDelay - 初期遅延ミリ秒数

getPeriod

public long getPeriod()

[概 要]

タイマー実行する間隔ミリ秒数を返却します。

[詳 細]

periodフィールドを返却します。

[備 考]

戻り値:
タイマー実行する間隔ミリ秒数

setPeriod

public void setPeriod(long period)

[概 要]

タイマー実行する間隔ミリ秒数を設定します。

[詳 細]

periodフィールドを引数periodで設定します。

[備 考]

パラメータ:
period - タイマー実行する間隔ミリ秒数

getStopLater

public long getStopLater()

[概 要]

タイマーを終了させるミリ秒数を返却します。

[詳 細]

stopLaterフィールドを返却します。

[備 考]

戻り値:
タイマーを終了させるミリ秒数

setStopLater

public void setStopLater(long stopLater)

[概 要]

タイマーを終了させるミリ秒数を設定します。

[詳 細]

stopLaterフィールドを引数stopLaterで設定します。

[備 考]

パラメータ:
stopLater - タイマーを終了させるミリ秒数

getExecutorService

public java.util.concurrent.ScheduledExecutorService getExecutorService()

[概 要]

タイマー実行するスケジューラサービスを返却します。

[詳 細]

executorServiceフィールドを返却します。

[備 考]

戻り値:
タイマー実行するスケジューラサービス

setExecutorService

public void setExecutorService(java.util.concurrent.ScheduledExecutorService executorService)

[概 要]

タイマー実行するスケジューラサービスを設定します。

[詳 細]

executorServiceフィールドを引数executorServiceで設定します。

[備 考]

パラメータ:
executorService - タイマー実行するスケジューラサービス

getFuture

public java.util.concurrent.ScheduledFuture<?> getFuture()

[概 要]

タイマー実行中のタスクを返却します。

[詳 細]

futureフィールドを返却します。

[備 考]

戻り値:
タイマー実行中のタスク

setFuture

public void setFuture(java.util.concurrent.ScheduledFuture<?> future)

[概 要]

タイマー実行中のタスクを設定します。

[詳 細]

futureフィールドを引数futureで設定します。

[備 考]

パラメータ:
future - タイマー実行中のタスク

isStop

public boolean isStop()

[概 要]

タイマーを停止するかどうかのフラグを返却します。

[詳 細]

stopフィールドを返却します。

[備 考]

戻り値:
タイマーを停止するかどうかのフラグ

setStop

public void setStop(boolean stop)

[概 要]

タイマーを停止するかどうかのフラグを設定します。

[詳 細]

stopフィールドを引数stopで設定します。

[備 考]

パラメータ:
stop - タイマーを停止するかどうかのフラグ

isStopImmediately

public boolean isStopImmediately()

setStopImmediately

public void setStopImmediately(boolean stopImmediately)

getIntervalAction

public java.lang.Class<? extends AbstractAction> getIntervalAction()

[概 要]

定期実行するアクションクラスを返却します。

[詳 細]

intervalActionフィールドを返却します。

[備 考]

戻り値:
定期実行するアクションクラス

setIntervalAction

public void setIntervalAction(java.lang.Class<? extends AbstractAction> intervalAction)

[概 要]

定期実行するアクションクラスを設定します。

[詳 細]

intervalActionフィールドを引数intervalActionで設定します。

[備 考]

パラメータ:
intervalAction - 定期実行するアクションクラス

mainproc

protected void mainproc()
                 throws java.lang.Exception

[概 要]

TimerProcessCoreManagerにタイマーの開始、停止を委譲します。

[詳 細]

isStop()を判定して開始、停止をManagerに依頼します。
TimerProcessCoreManagerにはgetTimerId()とこの機能モデルのインスタンスが 引数として渡されます。
Manager側では、渡されたtimerIdを元に開始されたTimerProcessCoreインスタンス を管理、停止するTimerProcessCoreインスタンスの削除を行います。

isStopがtrueの場合は、TimerProcessCoreManager#stopが正常に行われた後、 ModelProcessEvent.SUCCESS、ModelProcessEvent.FINISHEDイベントが発行されます。

[備 考]

オーバーライド:
クラス BaseModel 内の mainproc
例外:
java.lang.Exception - オーバーライド先で発生する可能性が有る例外

onTick

public final void onTick()

[概 要]

指定したタイマー間隔でコールされるメソッドです。

[詳 細]

ModelProcessEvent.SUCCESSイベントを発行します。
このイベントを受け取ったコントローラのモデル処理監視リスナは、
BaseAction継承アクションのsuccessForwardをコールバックします。

[備 考]


postTick

protected void postTick()

[概 要]

タイマーハンドラ拡張用メソッドです。

[詳 細]

#timerCompleted()によってテンプレートコールされます。
タイマー処理として共通の処理を実装する場合、このメソッドを オーバーライドした機能モデルを作成することで、汎用的なコードを 記述出来ます。

[備 考]


onStop

public final void onStop()

[概 要]

タイマー終了時にテンプレートコールされます。

[詳 細]

postStop()をコールしてModelProcessEvent.FINISHED イベントを発行します。

[備 考]


postStop

protected void postStop()

[概 要]

タイマー終了時にテンプレートコールされるオーバーライドメソッドです。

[詳 細]

デフォルト処理は有りません。

[備 考]

このメソッドをオーバーライドすることでタイマー終了時の共通処理を記述出来ます。



Copyright © 2008-2010. All Rights Reserved.