TaskExecutor的包装及其最佳界面

时间:2019-02-19 09:47:40

标签: java spring oop design-patterns

我有5个处理程序,每个处理程序在新线程中执行一些操作。现在,我向每个处理程序注入java TaskExecutor,并向每个处理程序注入特定的操作。并开始此操作。一个处理程序的示例:

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

然后开始:

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

我想创建Wrapper我的域执行器(我可以根据需要更改实现。可以使用TaskExecutor或其他方法执行,而不必在所有处理程序中都更改实现)。但是我不明白如何为该包装创建接口。我创建了2个变体:

public interface MyHandlerExecutor {

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

并使用它: 1)第一个变体:

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

2)第二个变体:

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

在第一个变体中,我不喜欢我需要两次通过guid。在第二个警告中,我不喜欢3方法的参数。

如何正确实施?

0 个答案:

没有答案