0

I have an abstract superclass AsyncActor which extends the Actor class. This superclass contains two abstract methods, notifySuccess and notifyFailure. notifyFailure is overriden in AsyncActor, but notifySuccess isn't because it's implementation changes from subclass to subclass. I have two actors, CreateObjActor and DeleteObjActor. Each extends AsyncActor and overrides notifySuccess. The execute method located in AsyncTask calls notifySuccess, but nothing happens.

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);
    }
}
}

I've tried making AsyncActor not abstract and providing an empty implementation of notifySuccess, but this didn't work either. Is there a way I can call the method from the superclass? The execute method contains a lot of code (not shown here) that would result in a lot of duplicate code if it had to be placed in each subclass.

SVN600
  • 345
  • 1
  • 5
  • 19
  • 1
    Why did you let out the essential parts of your code? Your question is missing the declaration of the overrides in both subclasses. – Seelenvirtuose Aug 04 '16 at 18:48
  • Kindly show the code of object creation and the method call. – Sumeet Aug 04 '16 at 18:49
  • Yes, you can using super keyword Ex:- super.methodName() – JavaHopper Aug 04 '16 at 18:50
  • Uhm. Your subclass asyncactor seems odd. Google "Java implements" "The distinction is that implements means that you're using the elements of a Java Interface in your class, and extends means that you are creating a subclass of the class you are extending. You can only extend one class in your new class, but you can implement as many interfaces as you would like". Seems like you should use extend for a subclass. Also, see http://stackoverflow.com/questions/4090834/when-do-i-use-super – WickedFan Aug 04 '16 at 18:51
  • Maybe your condition is evaluating to false, thus `notifySuccess()` is not called? – garnulf Aug 04 '16 at 18:52
  • @JavaHopper the super is redundant if I remember. – WickedFan Aug 04 '16 at 18:53
  • What do you mean by nothing happens? – Sumeet Aug 04 '16 at 18:56
  • 1
    where did AsyncTask come into picture. And what is its relationship in inheritance hierarchy? – JavaHopper Aug 04 '16 at 18:59
  • I just updated with complete code – SVN600 Aug 04 '16 at 19:04

0 Answers0