The interactors are the part of the business logic that follows the command pattern, one example implementation in Java is, for example, the ThreadPoolExecutor and the Runnables. The ThreadPoolExecutor is the invoker that executes the runnable (command). When we are using Interactors in our clean approach we are following the command pattern, so I’ve wrapped the implementation details of the Invoker with my interface InteractorInvoker.
The InteractorInvoker now runs using the ThreadPoolExecutor of Java that is because I don’t want to depend in any other frameworks for do something that the framework already supports it and works well. The thread pool executor is returning Future to anyone who calls it and with this Future you can cancel the interactors. If you cancel an interactor you will get a callback on the “onCancel” method.
As the InteractorInvoker is based now on the Future API the Interactors are using the Callable interface of Java so, this is quite good because now we can use the “T call() throws E” method to simplify the Interactor. What does it mean? T is the return type of the Interactor and E is the error this Interactor throws if there are any errors on the repository.
The new invoker also has an option to invoke various Interactors in the same thread, just call to the interactorInvoker.invoke(interactor) retrieve the Future and call .get() this will block the thread until a result is reached.
So the new interactor has the following advantages about the previous one:
– Based on the Future API of Java, so now we can cancel interactors
– Possibility of call Interactors in the same thread (sync)
– Interactors are simplified because now the Types are being forced by the super Interface “Interactor”
You can see the InteractorInvoker implementation in the Clean Contacts repository (develop branch)