0
我是Vertx的新手。如何在Vertx中编写异步文件处理程序
我在玩API,我试图写一个FileSizeHandler。我不知道这是否是正确的做法,但我想征求意见。
在我的代码我想用这样的处理程序:
public class MyVerticle extends AbstractVerticle {
@Override
public void start() throws Exception {
getFileSize("./my_file.txt", event -> {
if(event.succeeded()){
Long result = event.result();
System.out.println("FileSize is " + result);
} else {
System.out.println(event.cause().getLocalizedMessage());
}
});
}
private void getFileSize(String filepath, Handler<AsyncResult<Long>> resultHandler){
resultHandler.handle(new FileSizeHandler(filepath));
}
}
这里是我FileSizeHandler
类:
public class FileSizeHandler implements AsyncResult<Long> {
private boolean isSuccess;
private Throwable cause;
private Long result;
public FileSizeHandler(String filePath){
cause = null;
isSuccess = false;
result = 0L;
try {
result = Files.size(Paths.get(filePath));
isSuccess = !isSuccess;
} catch (IOException e) {
cause = e;
}
}
@Override
public Long result() {
return result;
}
@Override
public Throwable cause() {
return cause;
}
@Override
public boolean succeeded() {
return isSuccess;
}
@Override
public boolean failed() {
return !isSuccess;
}
}
让我困扰的处理程序,是我必须做的它在类的构造函数中。有没有更好的方法来做到这一点?