Monitoring RESTful web services using Jersey APIs

Jersey lets you register various event listeners to monitor the state of your JAX-RS application. Here are the two core listener interfaces that you may need to be aware of:

  • org.glassfish.jersey.server.monitoring.ApplicationEventListener: This is a Jersey-specific provider component that listens to application events such as the initialization of the application, the start and stop of the application, and so on. The implementation class can be registered as any standard JAX-RS provider.
  • org.glassfish.jersey.server.monitoring.RequestEventListener: The implementation of the interface will be called for request events when they occur. This is not a JAX-RS provider; an instance of RequestEventListener must be returned from ApplicationEventListener::onRequest(RequestEvent).

The following sample code illustrates the usage of ApplicationEventListener to log the application event states and invoke RequestEventListener:

package com.packtpub.rest.ch5.service.monitor;

import java.util.logging.Logger;
import javax.ws.rs.ext.Provider;
import org.glassfish.jersey.server.monitoring.ApplicationEvent;
import org.glassfish.jersey.server.monitoring.ApplicationEventListener;
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.server.monitoring.RequestEventListener;

@Provider
public class ApplicationEventListenerImpl implements ApplicationEventListener {
private static final Logger logger = Logger.getLogger(ApplicationEventListenerImpl.class.getName());

private int requestCount = 0;

@Override
public void onEvent(ApplicationEvent ae) {
logger.info("ApplicationEventListenerImpl::onEvent");

//Check the application event type and log the status
switch (ae.getType()) {
case INITIALIZATION_START:
logger.info("INITIALIZATION_START");
break;
case INITIALIZATION_APP_FINISHED:
logger.info("INITIALIZATION_APP_FINISHED");
break;
case INITIALIZATION_FINISHED:
logger.info("INITIALIZATION_FINISHED");
break;
case RELOAD_FINISHED:
logger.info("RELOAD_FINISHED");
break;
case DESTROY_FINISHED:
logger.info("DESTROY_FINISHED");
}
}

@Override
public RequestEventListener onRequest(RequestEvent re) {
//Increment the Request Counter for each request
requestCount++;
logger.fine("ApplicationEventListenerImpl::onRequest");
return new RequestEventListenerImpl(requestCount);
}
}

The following sample code illustrates the usage of  RequestEventListener to log the processing of each request:

package com.packtpub.rest.ch5.service.monitor;

import java.util.logging.Level;
import java.util.logging.Logger;
import org.glassfish.jersey.server.monitoring.RequestEvent;
import org.glassfish.jersey.server.monitoring.RequestEventListener;

public class RequestEventListenerImpl implements RequestEventListener {

private static final Logger logger = Logger.getLogger(RequestEventListenerImpl.class.getName());

private int requestNumber;

public RequestEventListenerImpl(int id)
{
this.requestNumber = id;
}

@Override
/*
* For each request event log the request being and end time
*/
public void onEvent(RequestEvent event) {
logger.info("RequestEventListenerImpl::onEvent");
switch (event.getType()) {
case RESOURCE_METHOD_START:
logger.log(Level.INFO, "Resource method {0} started for request {1}",
new Object[]{event.getUriInfo().getMatchedResourceMethod()
.getHttpMethod(), requestNumber});
break;
case FINISHED:
logger.log(Level.INFO, "Request {0} finished. ", requestNumber);
break;
}
}
}

Given ahead is the output of processing the REST API request via ApplicationEventListener and RequestEventListener:

Program Output:

Info: ApplicationEventListenerImpl::onEvent
Info: INITIALIZATION_START
Info: ApplicationEventListenerImpl::onEvent
Info: INITIALIZATION_APP_FINISHED
Info: ApplicationEventListenerImpl::onEvent
Info: INITIALIZATION_FINISHED
Info: ApplicationEventListenerImpl::onEvent
Info: DESTROY_FINISHED
Info: RequestEventListenerImpl::onEvent
Info: Resource /rest-chapter5-advfeatures/webresources/server/info method GET started for request 1
Info: Request 1 finished.

Jersey also comes with the MonitoringStatistics JMX bean to collect the application state, which can be enabled by setting the following configuration property:
jersey.config.server.monitoring.statistics.mbeans.enabled=true

To learn more about the monitoring framework in Jersey, visit the following chapter in the Jersey User Guide: 
https://jersey.github.io/documentation/latest/monitoring_tracing.html
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.14.134.130