10
回答
执行一个任务,如果成功就结束,如果遇到错误就执行重新执行,最多重复10次
终于搞明白,存储TCO原来是这样算的>>>   

想执行一个上传文件的任务,如果成功就结束,如果遇到错误就执行重新执行,最多重复10次,中间成功结束任务,10次之后还是失败就停止任务

举报
X红薯
发帖于2年前 10回/341阅
共有10个答案 最后回答: 2年前

状态类


package com.springapp.fsm.status;

/**

 * Created by hava on 2014/5/22.

 */
public interface State {

    public void run();

    public void setKey(Object Key);

    public Boolean check();

    public Boolean error();
    
    public void reset();
}


初始化


package com.springapp.fsm;

import com.springapp.fsm.status.*;

/**

 * Created by hava on 2015/4/21.

 */
public class FileStatusMachine {
    //Interface

    State state;

    //global state

    State StartState;
    State CommandState;
    State BreakState;
    State SuccessState;

    //Sync

//    State SyncProjectState;

    State SyncSnapshotState;
    State SyncTypeState;
    State SyncInfoFileState;
    State SyncInfoDirState;
    State SyncDoneState;

    //Delete

    State DelSnapshotState;
    State DelTypeStae;
    State DelInfoFileState;
    State DelInfoDirState;
    State DeleteDoneState;


    public FileStatusMachine()
    {
        //init all state

        //global

        this.StartState = new StartStateImpl(this);
        this.CommandState = new CommandStateImpl(this);
        this.BreakState = new BreakStateImpl(this);
        this.SuccessState = new SuccessStateImpl(this);
        //Sync

//        this.SyncProjectState = new com.springapp.fsm.status.sync.ProjectStateImpl(this);

        this.SyncSnapshotState= new com.springapp.fsm.status.sync.SnapshotStateImpl(this);
        this.SyncTypeState    = new com.springapp.fsm.status.sync.TypeStateImpl(this);
        this.SyncInfoFileState= new com.springapp.fsm.status.sync.InfoFileStateImpl(this);
        this.SyncInfoDirState = new com.springapp.fsm.status.sync.InfoDirectoryStateImpl(this);
        this.SyncDoneState    = new com.springapp.fsm.status.sync.SyncStateImpl(this);
        //Delete

        this.DelSnapshotState = new com.springapp.fsm.status.delete.SnapshotStateImpl(this);
        this.DelTypeStae      = new com.springapp.fsm.status.delete.TypeStateImpl(this);
        this.DelInfoFileState = new com.springapp.fsm.status.delete.InfoFileStateImpl(this);
        this.DelInfoDirState  = new com.springapp.fsm.status.delete.InfoDirectoryStateImpl(this);
        this.DeleteDoneState  = new com.springapp.fsm.status.delete.DeleteStateImpl(this);

        //init

        this.state = this.getStartState();
    }
调用


FileStatusMachine statusMachine = new FileStatusMachine();
        while(true)
        {
            try
            {
                if (statusMachine.getState().check())
                {
                    statusMachine.getState().run();
                }
                else
                {
                    if(statusMachine.getState().error())
                    {
                        statusMachine.getState().reset();
                    }
                }
           }
            catch(Exception e)
            {
            System.err.println("Gobal:FileSynchronous Method:run() Error:FileStatusMachine " + statusMachine.getState().toString() + " Exception:" + e.toString());
            //jump to the breakstate

            statusMachine.setState(statusMachine.getBreakState());
                //statusMachine.getState().error();

            }
        }







public boolean uploadFile(){
        //尝试次数
        int num=0;
        while(true){
            num++;
            if(num >10){
                System.out.println("任务提交失败了");
                return false;
            }
            try{
                //任务操作
                if(num<10){
                    throw new Exception();
                }
                System.out.println("第"+num+"次任务提交成功了");
                return true;
            }catch(Exception e){
                System.out.println("第"+num+"次任务提交失败了,再次尝试....");
            }
        }
        
    }
顶部