mardi 19 février 2019

Wrapper for TaskExecutor and best interface for it

I have 5 handlers and each handler execute some operation in new thread. Now I Inject java TaskExecutor to each handler, and Inject specific Operation to each handler. And start this operation. Example for one handler:

private final ImportOperation importOperation;
private final TaskExecutor taskExecutor;
// constructor inject

And Start:

taskExecutor.execute(() -> importOperation.importRequest(request.getImportedCharge(),newMessage.getGuid()));

I want create Wrapper my domain executor(I can change the implementation as I want. use TaskExecutor or another and I don't have to change the implementation in all handlers). But I do not understand how create interface for this wrapper. I create 2 variants:

public interface MyHandlerExecutor {

    void execute(Runnable runnable, String guid);
    <T, R> void execute(Operation<T, R> operation, List<T> list, String guid);
}

And use it: 1) first variant:

myHandlerExecutor.execute(() -> importOperation.importRequest(request.getImportedCharge(), newMessage.getGuid()), newMessage.getGuid());

2) Second variant:

myHandlerExecutor.execute(importOperation, request.getImportedCharge(), newMessage.getGuid();

In first variant I do not like that I need pass guid 2 times. In second wariant I do not like 3 method's arguments.

How to properly implement this?

Aucun commentaire:

Enregistrer un commentaire