DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Languages
  4. Timing interceptor for CDI/Weld

Timing interceptor for CDI/Weld

Adam Warski user avatar by
Adam Warski
·
Mar. 02, 11 · Interview
Like (0)
Save
Tweet
Share
5.93K Views

Join the DZone community and get the full member experience.

Join For Free

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:

Interceptor

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().

Extension

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 " +
event.getAnnotatedType().getJavaClass().getName());
AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>()
.readFromType(event.getAnnotatedType());
builder.addToClass(new TimedImpl());
event.setAnnotatedType(builder.create());
}
}

Proxy

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!

 

From http://www.warski.org/blog/?p=372

CDI Execution (computing) Requests Annotation Filter (software) Object (computer science) HotSpot (virtual machine) GitHub Spring Framework

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Test Design Guidelines for Your CI/CD Pipeline
  • Multi-Tenant Architecture for a SaaS Application on AWS
  • DeveloperWeek 2023: The Enterprise Community Sharing Security Best Practices
  • What’s New in Flutter 3.7?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: