Effective monitoring of RESTful resources using Jersey listeners

Measure performance with event listeners

Learn on Udemy Today!

Have you ever felt the pain of measuring performance of your RESTful Java application? If your answer is “YES”, then you are one among the big crowd!

In our previous articles we have seen how to develop RESTful Java application using Jersey framework. Today we are going to see how we can measure performance numbers for each RESTful resources by using Jersey’s in-built listeners.

Jersey framework has introduced 2 event listeners to watch events during the REST resource execution. Which are,

  • Application event listener
  • Request event listener

Application event listener

This listener is applicable across our RESTful application which monitors various events like ApplicationEvent.INITIALIZATION_START, ApplicationEvent.INITIALIZATION_APP_FINISHED, ApplicationEvent.INITIALIZATION_FINISHED, ApplicationEvent.DESTROY_FINISHED, ApplicationEvent.RELOAD_FINISHED

First we have to write an application event listener like the below and it should be registered as a provider in our application.

Please note the method of “onRequest()” in the above sample code. We will see the purpose of the listener later.

Request event listener

This listener is applicable or triggered for each resource method call. In-order to monitor our resource method performance we need to provide an implementation of RequestEventListener interface.

We have to be little careful when we write our real time application. This has to be coded thread safely.

After these two implementations, when we execute any of our REST resource method we can get some output in the logger like below,

This way we can measure the performance numbers for each resource methods and optimize our application.


When you have no Request event listener, it is better to return null in our Application event listener instead of returning dummy request event listener. This will have some side effect when we have dummy request listener 🙁

I hope this article is useful for you to understand the event listeners in Jersey framework and how we can measure performance by using them. Listeners can also be useful when we want to do some operations based on various events which are happening during request. Keep watching this space for more articles!

Please share your feedback or comments if you have any! All the code samples are available under https://github.com/cloudskol/restskol

Also read,

Marshalling Java to JSON never be hard

Simple GET method implementation with RESTful Java

Previous articleMarshalling Java to JSON, never be hard anymore!
Next articleRESTful Java metering by Dropwizard Metrics
Thamizharasu is a Java developer living in India. He is writing about Java programming, RESTful web services and frameworks. He also loves working with Web API, Elastic search and Firebase. Currently he is a Madras JUG lead and you can follow him on Twitter @zarub2k