我有一个抽象的超类AsyncActor
,它扩展了Actor
类。此超类包含两个抽象方法notifySuccess
和notifyFailure
。 notifyFailure
中覆盖了AsyncActor
,但notifySuccess
并非CreateObjActor
,因为它的实现从子类更改为子类。我有两个演员DeleteObjActor
和AsyncActor
。每个扩展notifySuccess
并覆盖execute
。位于AsyncTask
的{{1}}方法调用notifySuccess
,但没有任何反应。
public class AsyncActor extends Action<String> {
private Future future;
private final Action action;
private final ExecutorService executor = Executors.newSingleThreadExecutor();
public AsyncActor(Action action) {
this.action = action;
}
public Action getAction() {
return action;
}
public Future getFuture() {
return future;
}
public void execute() {
if(action.execute()) {
executeAsynchronously();
}
}
public void executeAsynchronously() {
ServiceFw.log.debug("Writing directory to file...");
Callable<String> asyncTask = () -> {
try {
ServiceFw.entryManager.writeBack();
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
ServiceFw.log.debug("Exception thrown during asynchronous wait");
e.printStackTrace();
} catch (BusinessException e) {
notifyFailure();
}
return "write back operation";
};
future = executor.submit(asyncTask);
Runnable poll = () -> {
if(future!=null) {
notifySuccess();
} else {
error.setSeverity(ExtendedError.Severity.Warning);
notifyFailure(error);
}
};
poll.run();
}
@Override
public void notifySuccess() {
}
@Override
public void notifyFailure() {
ServiceFw.log.error("Error with asynchronous processing of write back" );
ServiceFw.entryManager.deleteActor(this);
}
}
public class CreateObjActor extends AsyncActor {
public CreateObjActor(Action action) {
super(action);
}
@Override
public void notifySuccess() {
try {
ServiceFw.log.debug("Finished asynchronous operation: " + getFuture().get());
Entry entry = getAction().getEntryModel();
if (getAction().isNotify()) {
Notification notification = new Notification(entry);
ServiceFw.notificationDispatcher.dispatchNotification(notification);
try {
ServiceFw.eventFramework.dispatchEvent("entry-added");
} catch (BusinessException e) {
ServiceFw.log.error("Event could not be dispatched for newly added entry: " + entry.toString());
}
}
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
} finally {
ServiceFw.entryManager.deleteActor(this);
}
}
}
我试图让AsyncActor
不抽象并提供notifySuccess
的空实现,但这也不起作用。有没有办法可以从超类中调用该方法? execute
方法包含大量代码(此处未显示),如果必须将它们放在每个子类中,将导致大量重复代码。