The server is broken up into services and I already had a service that did auditing. I added two new methods to it recordClientLogin and recordCourseRetrieval. I wanted these methods to be run asynchronously but I wanted the asychronousness (if this isn't a word it should be) to be reusable and easily enabled or disabled (easier to unit test when disabled). This immediately let me to use AOP so I wrote an interceptor.
* Intecepts a call and makes it asynchronous. If there is an error it is
* simply logged.
public class AsynchronizeInterceptor implements MethodInterceptor
private static final Log LOG = LogFactory.
* This method will always return null since the method is
* invoked asynchronously.
* @see org.aopalliance.intercept.MethodInterceptor
public Object invoke(final MethodInvocation invocation) throws Throwable
Thread thread = new Thread()
public void run()
} catch (Throwable e)
LOG.warn("Asynchronous method invocation failed.", e);
So this inteceptor will make any method it intercepts asynchronous running the method on a separate thread and returning immediately. So now I just needed to add the point cut to the spring application context.
<!-- Turns methods calls into asynchronous method calls -->
<!-- Intercept caddie event recording method calls -->
<ref local="asynchronizeInterceptor" />
Now anytime I call com.scoreout.service.Audit.recordCourseRetrieval or com.scoreout.service.Audit.recordClientLogin the call will return immediately and the method will be run in the background. For testing I can easily make these methods synchronous by either removing them from the application context or providing a new application context that does not include this pointcut (I generally prefer the latter).