2017-01-10 57 views
0
private Task<Void> getTransferId = new Task<Void>() { 

    @Override 
    protected Void call() throws Exception { 
     Client client = ClientBuilder.newClient(); 
     WebTarget webTarget = client.target(Constants.BASE_URL); 
     WebTarget helloworldWebTarget = webTarget.path(Constants.TRANSFER_FILE); 
     System.out.println(" checking working.....status"); 
     Invocation.Builder invocationBuilder = helloworldWebTarget.request(MediaType.APPLICATION_JSON); 
     PostFileLog fileLog = new PostFileLog(); 
     fileLog.setSenderId(myUuid); 
     fileLog.setReceiverId(clickedPossitionUuid); 
     fileLog.setFileName(fileName); 
     fileLog.setFileType(fileTypeInt); 
     fileLog.setFileSize(fileSizeFinal); 
     fileLog.setStatus(Constants.STATUS_PENDING); 
     fileLog.setFileDesc(Constants.NO_CAPTION); 
     Response response = invocationBuilder.post(Entity.entity(fileLog, MediaType.APPLICATION_JSON)); 
     int status = response.getStatus(); 
     PostFileLog reponceLog = response.readEntity(PostFileLog.class); 
     transferId = reponceLog.getUuid(); 
     System.out.println(transferId + " fist sending TrasferId andstastus " + status); 

     return null; 
    } 
}; 

爲了執行上面的代碼,我使用了.run(); 它只執行一次。我需要持續執行這個問題,任何人都可以幫助我解決這個問題。javafx中的連續運行任務?

+0

「對於執行上面的代碼,我使用'.RUN(); '」。這將不會在後臺線程中運行該任務:它將在當前線程上運行它(可能是FX應用程序線程,具體取決於您從哪裏調用它)。你應該把它封裝在一個'Thread'中,並在線程中調用'start()',或者將它提交給一個合適的'Executor'。 –

回答

-1

只要把你的代碼中while循環,使的方式來阻止while循環

編輯你的代碼檢查該

boolean isStop=false; 
private Task getTransferId = new Task() 
{ 

@Override 
protected Void call() throws Exception 

{ 
    while(!isStop){ 
    Client client = ClientBuilder.newClient(); 
    WebTarget webTarget = client.target(Constants.BASE_URL); 
    WebTarget helloworldWebTarget = webTarget.path(Constants.TRANSFER_FILE); 
    System.out.println(" checking working.....status"); 
    Invocation.Builder invocationBuilder = helloworldWebTarget.request(MediaType.APPLICATION_JSON); 
    PostFileLog fileLog = new PostFileLog(); 
    fileLog.setSenderId(myUuid); 
    fileLog.setReceiverId(clickedPossitionUuid); 
    fileLog.setFileName(fileName); 
    fileLog.setFileType(fileTypeInt); 
    fileLog.setFileSize(fileSizeFinal); 
    fileLog.setStatus(Constants.STATUS_PENDING); 
    fileLog.setFileDesc(Constants.NO_CAPTION); 
    Response response = invocationBuilder.post(Entity.entity(fileLog, MediaType.APPLICATION_JSON)); 
    int status = response.getStatus(); 
    PostFileLog reponceLog = response.readEntity(PostFileLog.class); 
    transferId = reponceLog.getUuid(); 
    System.out.println(transferId + " fist sending TrasferId andstastus " + status); 
} 
    return null; 
} 
}; 

public void StopTask(){//To stop the task 
isStop=true; 
} 
+1

如果你使用一個標誌,至少使其變得不穩定。否則'Task'線程可能永遠不會看到更新的值。不過,我會推薦使用'cancel' +'isCancelled'來代替... – fabian