Having a "worker" in Java

373 Views Asked by At

I have a REST API created in Java with the Spark framework, but right now a lot of work is being done on the request thread that is significantly slowing down requests.

I'm wanting to solve this by creating some kind of background worker/queue that will do all the needed work off of the request thread. The response from the server contains data that the client will need (it's data that will be displayed). In these examples the client is a web browser.

Here's what the current cycle looks like

  1. API request from client to server
  2. Server does blocking work; Response from server after several seconds/minutes
  3. Client receives response. It has all the data it needs in the response

Here's what I would like

  1. API request from client to server
  2. Server does work off-thread
  3. Client receives response from server almost instantly, but it doesn't have the data it needs. This response will contain some ID (Integer or UUID), which can be used to check the progress of the work being done
  4. Client regularly checks the status of the work being done, the response will contain a status (like a percentage or time estimate). Once the work is done, the response will also contain the data we need

What I dislike about this approach is that it will significantly complicate my API. If I want to get any data, I will have to make two requests. One to initiate the blocking work, and another to check the status (and get the result of the blocking work). Not only will the API become more complicated, but the backend will too.

Is this efficient, or is there a better way to implement what I want to accomplish?

1

There are 1 best solutions below

0
On

Neither way is more efficient than the other since the same amount and time of work will be done in either case. In the first case it will be done on the request thread, the client will not know of progress and the request will take as long as it takes to run the task. This has the client wait on the reply.

In the second case you need to add complexity, but you get progress status and possibly other advantages depending on the task. This has the client poll on the reply.

You can use async processing to perform work on non-request threads, but that probably won't make any difference if most of your requests are long running ones. So it's up to you to decide what you want, the client will have to wait the same amount anyway.