Answer the question
In order to leave comments, you need to log in
How to correctly organize a call to an external REST service?
Good afternoon!
I have a web application (Java, Spring) that handles various requests from the frontend via REST.
There is a need to process some requests to request additional information from external services.
The application works for me on the principle of CRM, i.e. For complex frontend requests, tasks are created that go through various stages of execution and then the user receives a notification about the completion of a particular task.
I have a thread that handles tasks i.e. fetches a new task, and moves it through the various stages of execution.
How to properly organize this interaction so as not to block the execution of other tasks while this one then makes a call to an external REST service?
There was an idea to make something like a wrapper for working with an external service that will work in a separate thread and process tasks with a certain state of the WAIT_FOR_EXTERNAL_DATA type.
Those. when the time has come for the task to contact the external service, I change its state to WAIT_FOR_EXTERNAL_DATA, after which the main task processing thread moves on to the next task with the NEW status, and this task continues to process the service wrapper thread - it forms a request to the external service, waiting for a response, then it edits/updates the task data, changes its state to EXTERNAL_DATA_READY and then this task can continue when the main task processing thread takes a new task for execution (i.e. select the first task from the list with a certain state).
Am I looking in the right direction?
Answer the question
In order to leave comments, you need to log in
Alternatively, use the ThreadPoolTaskExecutor and check the Future (to monitor the execution of the task) in the controller using @ResponseBody on the url (example /task/{taskId}/check).
Several solutions are possible here.
First of all, transactional solutions. This will reduce the time without using third-party integrations.
Use orchestration by tasks integration. From the simplest, Quartz comes to mind. It allows you to work in different threads and within the cluster.
Ideally, you should pay attention to reactive frameworks. In the Spring stack, this is Flume.
I have been using Vertx for a very long time, it is very similar to node.js and has received support in Quarkus.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question