|
16 | 16 | * specific language governing permissions and limitations |
17 | 17 | * under the License. |
18 | 18 | */ |
19 | | -package org.apache.fineract.command.implementation; |
| 19 | +package org.apache.fineract.command.async.implementation; |
20 | 20 |
|
| 21 | +import static java.util.Objects.requireNonNull; |
21 | 22 | import static java.util.concurrent.TimeUnit.SECONDS; |
22 | 23 |
|
23 | 24 | import java.util.concurrent.CompletableFuture; |
24 | 25 | import java.util.concurrent.ExecutionException; |
25 | 26 | import java.util.concurrent.TimeoutException; |
26 | 27 | import java.util.function.Supplier; |
| 28 | +import lombok.RequiredArgsConstructor; |
27 | 29 | import lombok.extern.slf4j.Slf4j; |
28 | 30 | import org.apache.fineract.command.core.Command; |
29 | | -import org.apache.fineract.command.core.CommandAuditor; |
30 | | -import org.apache.fineract.command.core.CommandHandler; |
31 | | -import org.apache.fineract.command.core.CommandRouter; |
| 31 | +import org.apache.fineract.command.core.CommandDispatcher; |
| 32 | +import org.apache.fineract.command.core.CommandHandlerManager; |
| 33 | +import org.apache.fineract.command.core.CommandHookManager; |
| 34 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 35 | +import org.springframework.stereotype.Component; |
32 | 36 |
|
| 37 | +// TODO: WIP - not ready yet for prime time |
33 | 38 | @Slf4j |
34 | | -// TODO: not ready yet for prime time |
35 | | -// @Component |
36 | | -// @ConditionalOnProperty(value = "fineract.command.executor", havingValue = "async") |
37 | | -public class AsynchronousCommandExecutor extends BaseCommandExecutor { |
| 39 | +@RequiredArgsConstructor |
| 40 | +@Component |
| 41 | +@ConditionalOnProperty(value = "fineract.command.async.enabled", havingValue = "true") |
| 42 | +public class AsyncCommandDispatcher implements CommandDispatcher { |
38 | 43 |
|
39 | | - public AsynchronousCommandExecutor(CommandRouter router, CommandAuditor auditor) { |
40 | | - super(router, auditor); |
41 | | - } |
| 44 | + private final CommandHandlerManager handlerManager; |
| 45 | + private final CommandHookManager hookManager; |
42 | 46 |
|
43 | 47 | @Override |
44 | | - public <REQ, RES> Supplier<RES> execute(final Command<REQ> command) { |
| 48 | + public <REQ, RES> Supplier<RES> dispatch(final Command<REQ> command) { |
| 49 | + requireNonNull(command, "Command must not be null"); |
| 50 | + |
45 | 51 | CompletableFuture<RES> future = CompletableFuture.supplyAsync(() -> { |
46 | | - auditor.processing(command); |
| 52 | + hookManager.before(command); |
47 | 53 |
|
48 | | - CommandHandler<REQ, RES> handler = router.route(command); |
| 54 | + RES response = handlerManager.handle(command); |
49 | 55 |
|
50 | | - return handler.handle(command); |
51 | | - }).whenComplete((response, t) -> { |
52 | | - if (t == null) { |
53 | | - auditor.processed(command, response); |
54 | | - } else { |
55 | | - command.setError(t.getMessage()); |
| 56 | + hookManager.after(command, response); |
56 | 57 |
|
57 | | - auditor.error(command); |
| 58 | + return response; |
| 59 | + }).whenComplete((response, t) -> { |
| 60 | + if (t != null) { |
| 61 | + hookManager.error(command, t); |
58 | 62 | } |
59 | 63 | }); |
60 | 64 |
|
61 | 65 | return () -> { |
62 | 66 | try { |
63 | 67 | // TODO: make this configurable |
64 | 68 | return future.get(3, SECONDS); |
65 | | - } catch (InterruptedException e) { |
66 | | - throw new RuntimeException(e); |
67 | | - } catch (ExecutionException e) { |
68 | | - throw new RuntimeException(e); |
69 | | - } catch (TimeoutException e) { |
| 69 | + } catch (InterruptedException | ExecutionException | TimeoutException e) { |
70 | 70 | throw new RuntimeException(e); |
71 | 71 | } |
72 | 72 | }; |
|
0 commit comments