Java – How to handle multiple calls to webservice

javamultithreadingweb services

I got a REST webservice and it has a simple method like;

public void processItem(Item item);

Problem is processItem method takes too long to complete nearly 1 minute) and there can be multiple calls in a short time.

How can I handle these requests efficiently?

Webservice creates a new thread for every request and it holds new requests when thread limit is exceeded, as far as I know. But I don't want clients to get timeout exceptions.

Do I need to use something like LinkedBlockingQueue or is there any other efficient way?

LinkedBlockingQueue<Item> items = new LinkedBlockingQueue();

public void processItem(Item item){
    items.put(item);
}

Best Answer

Your immediate issue might be fixed with a better webserver or reconfiguring the api to work on an async model and not eat up threads.

However, going forward with long requests you should switch to a queue model which can cope with indefinite delays.

Depending on your infrastructure, perhaps your client can post directly to a queue and listen on a a response queue.

Or you can continue posting to the HTTP api but have it simply save the job for another worker app to process later and provide a query api so the client can poll for updates.

Related Topic