Timing interceptor for CDI/Weld

Lately we did some performance tuning, and in order to find out which methods are the hotspots I wrote a simple timing interceptor (it uses some code from a Seam forum post by Tobias Hill). Apart from timing the execution time of single methods, it gathers all results from a request and prints out a short sorted summary at the end containing the number of invocations, average time and so on.

You can find the source code and some documentation on github (softwaremill-debug module of the softwaremill-common project, licensed under Apache2), and the artifacts in our maven repository.

There are in fact four parts:


The interceptor is a regular CDI interceptor which times the execution of a method and stores the result in a thread-local storage. It must be enabled in beans.xml and can be applied via an annotation (@Timed).

Servlet filter

The servlet filter prints out the summary after a request completes. You just need to add it in your web.xml. It is also possible to print out the current stats at any point by invoking TimingResults.dumpCurrent().


The portable extension can save you some time if you want to intercept a lot of methods. Instead of adding the annotation manually, it automatically adds the interceptor to all beans for which the full class name (including the package) includes one of the elements of the timed.autoadd system property.

The extension is also a good example on how to automatically add interceptors in CDI (using Seam Solder); in essence it boils down to the following:

public <T> void processAnnotatedType(
      @Observes ProcessAnnotatedType<T> event) {
   if (interceptAnnotatedType(event.getAnnotatedType())) {
      log.info("Adding the timing interceptor for " + 
      AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>()
      builder.addToClass(new TimedImpl());


For timing methods on objects which aren’t managed by the CDI container, there’s a timing proxy. Simple wrap your object with TimingProxy.createFor and any invocations will be timed and added to the current results (of the current request).

Hope you’ll find it useful!


  • Perhaps rather use Javassist in TimingProxy,
    so one can wrap even beans / objects that don’t have proper business interface(s).

  • True, would be much better, so far as a quick way of timing it worked – although I always had an interface.


  • Nik
  • Nice! Didn’t know it existed – just shows a need for a good CDI extensions directory.
    Thanks for the link.


  • >> just shows a need for a good CDI extensions directory

    Good idea.
    Can you push this to our weld-dev ml?
    Hmmm, perhaps seam-dev would be a better place?

    Or, the best approach, start a wiki page with this “directory”, and we’ll link it from both project pages.

  • In fact, there is a directory (on the seamframework.org page, choose CDI extensions and then in the text there’s a link to: http://groups.diigo.com/group/cdi-extensions).
    Not sure if it’s the best format (hard to categorize), or maybe it should just be exposed more.


  • Hello,
    I’m testing this in WAS8 but I can’t manage to add extra annotations (Interceptor) to an EJB.

    The extension works ok, but later, I caught the following exception:

    [24/08/12 13:09:43:088 CEST] 00000025 BeansDeployer E BeansDeployer deploy
    at org.apache.webbeans.component.creation.AbstractBeanCreator.defineStereoTypes(AbstractBeanCreator.java:165)
    at org.apache.webbeans.ejb.common.util.EjbUtility.fireEvents(EjbUtility.java:88)
    at org.apache.webbeans.ejb.WSEjbPlugin.defineSessionBean(WSEjbPlugin.java:397)
    at org.apache.webbeans.config.EJBWebBeansConfigurator.defineEjbBean(EJBWebBeansConfigurator.java:71)
    at org.apache.webbeans.config.BeansDeployer.defineEnterpriseWebBean(BeansDeployer.java:907)
    at org.apache.webbeans.config.BeansDeployer.deploySingleAnnotatedType(BeansDeployer.java:550)
    at org.apache.webbeans.config.BeansDeployer.deployFromClassPath(BeansDeployer.java:488)
    at org.apache.webbeans.config.BeansDeployer.deploy(BeansDeployer.java:171)
    at org.apache.webbeans.lifecycle.AbstractLifeCycle.startApplication(AbstractLifeCycle.java:124)
    at org.apache.webbeans.web.lifecycle.WebContainerLifecycle.startApplication(WebContainerLifecycle.java:78)

    Any ideas?


  • Never tried the WebBeans implementation (only Weld) or WAS, but if you’ll find the answer, please post it here as well :)