2 xuzhenglin2010 xuzhenglin2010 于 2016.09.20 16:49 提问

如何获取ListenableFuture的任务执行结果并返回

private void syncUpload(final File apkFile,final String fileName){
//提交上传任务
final ListenableFuture listenableFuture = uploadExecutor.submit(new SyncPublishAppThread(apkFile,fileName));
//添加回调函数
Futures.addCallback(listenableFuture, new FutureCallback() {
@Override
public void onSuccess(Boolean isok) {
if(isok){
log.debug(fileName+"发布成功!");
FileUtils.deleteQuietly(apkFile);
}else{
log.debug(fileName+"发布失败!");
}
}
@Override
public void onFailure(Throwable t) {
log.error(fileName+"发布失败,错误信息:"+t.getMessage());
}

});
}

其中,SyncPublishAppThread类的代码如下:
class SyncPublishAppThread implements Callable{
File apkFile;
String fileName;
public SyncPublishAppThread(File apkFile,String fileName) {
this.apkFile = apkFile;
this.fileName = fileName;
}
@Override
public Boolean call() throws Exception {
try {
String response = HttpUtils.postFile(appPublishServer, apkFile, fileName, "app");
if(StringUtils.isNotBlank(response)){
JSONObject obj = JSONObject.parseObject(response);
String resultMsg = obj.getString("resultMsg");
if(StringUtils.isNotBlank(resultMsg)&&"1".equals(resultMsg)){
return true;
}
}
} catch (Exception e) {
log.error("发布失败!错误信息:"+e.getMessage());
throw new RuntimeException("发布失败!错误信息:"+e.getMessage());
}
return false;
}
}

现在我想获取SyncPublishAppThread类的对象的call()方法的Boolean返回值,并把该返回值通过syncUpload()方法返回,即把syncUpload()方法的返回类型由void改成Boolean。请问具体该如何修改代码才能实现这个功能,恳求大神们赐教。

1个回答

devmiao
devmiao   Ds   Rxr 2016.09.21 00:41
Csdn user default icon
上传中...
上传图片
插入图片