Archive for the 'AspectJ' Category

Detached Service Methods with Spring AOP

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

public myServiceMethod() throws DeferredExecutionException {

This method would execute with the following sematics:

  1. The regular method call is wrapped in a thread
  2. The thread is executed
  3. If the thread does not finish within x seconds, raise a checked exception
  4. 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 »