In a typical spring based server application there is a service layer responsible for infrastructure tasks and communication with the clients. Service methods can be long running and execution would normally block the user-interface. Putting the client side end of the call in a thread solves the blocking but leaves the network connection open while the server side of the call is active. This can lead to all sorts of time-out issues and seems to be the wrong end to fix the problem .
Using spring AOP it is remarkably easy to declaratively enable service methods to execute detached from the regular call context.
The Goal
@DeferredExecution
public myServiceMethod() throws DeferredExecutionException {
}
This method would execute with the following sematics:
- The regular method call is wrapped in a thread
- The thread is executed
- If the thread does not finish within x seconds, raise a checked exception
- If it does finish in time, return the result of the method
This way clients of this method are forced to handle detached execution (i.e. show a progress dialog querying the server) and the client side of the call not langer lasts longer than a set time.
Read more »