不调用子类中的覆盖方法

时间:2016-08-04 18:42:04

标签: java inheritance

我有一个抽象的超类AsyncActor,它扩展了Actor类。此超类包含两个抽象方法notifySuccessnotifyFailurenotifyFailure中覆盖了AsyncActor,但notifySuccess并非CreateObjActor,因为它的实现从子类更改为子类。我有两个演员DeleteObjActorAsyncActor。每个扩展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方法包含大量代码(此处未显示),如果必须将它们放在每个子类中,将导致大量重复代码。

0 个答案:

没有答案