2017-01-10 6 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(); を1回だけ実行しています。この問題を解決する方法は何ですか?javafxで継続的に実行されているタスク?

+0

を」編集whileループを停止する方法を確認します。 ''これは、バックグラウンドスレッドでタスクを実行しません。現在のスレッド(FXアプリケーションスレッド、おそらくどこから呼び出すかによって異なります)でタスクを実行します。 'Thread'でラップし、スレッド上で' start() 'を呼び出すか、適切な' Executor'にそれを渡す必要があります。 –

答えて

-1

ただ、whileループでコードを入れて、

私は `.RUN()を使用しています上記のコードを実行するためのあなたのコードのチェックこの

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

関連する問題