Question

Every RPC in GWT is limited with 1 minute timeout, and this is not configurable.

I am consuming a SYNC METHOD from a third party provider which calls a WEBSERVICE.

Sometimes this METHOD (webservice) hangs for more than 1 minute, and crashes my RPC. The problem is that I cant catch this exception on the server side, which I need to rollback some flags (its a more complicated process this is just for a example)

try {

...my code goes here...

MYTHIRDPARTYWS ws = new MYTHIRDPARTYWS()
String RESULT = ws.run;

...my code needs to take action depending of the result...


} catch (Exception e) {

...my code needs to take action depending of the exception...

}

I need something like this:

try {

...my code goes here...

Process p = new Process() {
MYTHIRDPARTYWS ws = new MYTHIRDPARTYWS()
String RESULT = ws.run;

};

p.setTimeOut(40000);
p.run;

...my code needs to take action depending of the result...


} catch (Exception e) {

...my code needs to take action depending of the exception...

}

Any idea?

Was it helpful?

Solution

Use method ExecutorService.execute() to spawn some task in a background thread.

Steps to follow:

  • Read some init parameters from web.xml in servlet init() method such as timeout and threadpoolsize
    • timeout parameter is used to set the timeout of Async thread
    • threadpoolsize is used to create a pool of Async threads
  • Get AsyncContext by calling HTTP request.startAsync() in doGet() or doPost() method
  • Set timeout of AsyncContext
  • Attach listener to respond to lifecycle events of this AsyncContext such as onComplete(), onTimeout(), onError(), onStartAsync()
  • Call ExecutorService.execute() to spawn some task in a background thread

I have already posted the code here Asynchronous servlet not acting asynchronously

Please have look and let me know if there is any confusion. It really works. You have full control on each life cycle method of sub-process to handle any type of exception.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top