CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-glassfish-jersey-core--jersey-server

Jersey core server implementation for building RESTful Web Services with JAX-RS.

Pending
Overview
Eval results
Files

monitoring.mddocs/

Monitoring and Statistics

Comprehensive monitoring system with application-level and request-level event handling, statistics collection, JMX integration, and performance metrics. Provides detailed insights into application performance, resource usage, and operational behavior.

Capabilities

Event System

Application and request event handling for monitoring Jersey server operations and performance.

/**
 * Application event interface representing application-level events.
 */
public interface ApplicationEvent {
    
    /**
     * Event type enumeration for application events.
     */
    enum Type {
        INITIALIZATION_START,
        INITIALIZATION_FINISHED,
        DESTROY_FINISHED,
        RELOAD_FINISHED
    }
    
    /**
     * Get the event type.
     * @return Event type
     */
    Type getType();
    
    /**
     * Get the resource config associated with this event.
     * @return ResourceConfig instance
     */
    ResourceConfig getResourceConfig();
}

/**
 * Application event listener for monitoring application lifecycle.
 */
public interface ApplicationEventListener {
    
    /**
     * Called when an application event occurs.
     * @param event Application event
     */
    void onEvent(ApplicationEvent event);
    
    /**
     * Called when a request event occurs, returns request event listener.
     * @param requestEvent Initial request event
     * @return RequestEventListener for this request or null
     */
    RequestEventListener onRequest(RequestEvent requestEvent);
}

/**
 * Request event interface representing request-level events.
 */
public interface RequestEvent {
    
    /**
     * Event type enumeration for request events.
     */
    enum Type {
        START,
        MATCHING_START,
        MATCHING_FINISHED,
        REQUEST_MATCHED,
        REQUEST_FILTERED,
        RESOURCE_METHOD_START,
        RESOURCE_METHOD_FINISHED,
        RESP_FILTERS_START,
        RESP_FILTERS_FINISHED,
        ON_EXCEPTION,
        FINISHED,
        EXCEPTION_MAPPING_FINISHED
    }
    
    /**
     * Get the event type.
     * @return Event type
     */
    Type getType();
    
    /**
     * Get the container request.
     * @return ContainerRequest for this event
     */
    ContainerRequest getContainerRequest();
    
    /**
     * Get the container response.
     * @return ContainerResponse for this event or null if not available
     */
    ContainerResponse getContainerResponse();
    
    /**
     * Get exception if this is an exception event.
     * @return Exception or null
     */
    Throwable getException();
}

/**
 * Request event listener for monitoring individual request processing.
 */
public interface RequestEventListener {
    
    /**
     * Called when a request event occurs.
     * @param event Request event
     */
    void onEvent(RequestEvent event);
}

Usage Examples:

import org.glassfish.jersey.server.monitoring.*;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.ConcurrentHashMap;

// Custom application event listener
public class CustomApplicationEventListener implements ApplicationEventListener {
    
    private final AtomicLong requestCounter = new AtomicLong();
    private final Map<String, AtomicLong> resourceCallCounts = new ConcurrentHashMap<>();
    
    @Override
    public void onEvent(ApplicationEvent event) {
        switch (event.getType()) {
            case INITIALIZATION_START:
                System.out.println("Jersey application initializing...");
                break;
            case INITIALIZATION_FINISHED:
                System.out.println("Jersey application initialized successfully");
                break;
            case DESTROY_FINISHED:
                System.out.println("Jersey application destroyed");
                break;
            case RELOAD_FINISHED:
                System.out.println("Jersey application reloaded");
                break;
        }
    }
    
    @Override
    public RequestEventListener onRequest(RequestEvent requestEvent) {
        requestCounter.incrementAndGet();
        
        return new RequestEventListener() {
            private long startTime;
            
            @Override
            public void onEvent(RequestEvent event) {
                switch (event.getType()) {
                    case START:
                        startTime = System.currentTimeMillis();
                        break;
                        
                    case REQUEST_MATCHED:
                        // Track resource usage
                        ContainerRequest request = event.getContainerRequest();
                        String path = request.getPath(false);
                        resourceCallCounts.computeIfAbsent(path, k -> new AtomicLong()).incrementAndGet();
                        break;
                        
                    case FINISHED:
                        long duration = System.currentTimeMillis() - startTime;
                        System.out.println("Request completed in " + duration + "ms");
                        break;
                        
                    case ON_EXCEPTION:
                        Throwable exception = event.getException();
                        System.err.println("Request exception: " + exception.getMessage());
                        break;
                }
            }
        };
    }
}

// Register the listener
ResourceConfig config = new ResourceConfig()
    .packages("com.example.resources")
    .register(CustomApplicationEventListener.class);

Statistics Collection

Core monitoring statistics interfaces providing detailed performance and usage metrics.

/**
 * Core monitoring statistics interface providing access to all monitoring data.
 */
public interface MonitoringStatistics {
    
    /**
     * Get request execution statistics.
     * @return ExecutionStatistics for all requests
     */
    ExecutionStatistics getRequestStatistics();
    
    /**
     * Get response statistics.
     * @return ResponseStatistics for all responses
     */
    ResponseStatistics getResponseStatistics();
    
    /**
     * Get URI-specific statistics.
     * @return Map of URI patterns to ResourceStatistics
     */
    Map<String, ResourceStatistics> getUriStatistics();
    
    /**
     * Get exception mapper statistics.
     * @return Map of exception types to ExceptionMapperStatistics
     */
    Map<Class<?>, ExceptionMapperStatistics> getExceptionMapperStatistics();
    
    /**
     * Get monitoring statistics snapshot timestamp.
     * @return Timestamp when statistics were captured
     */
    Date getSnapshot();
}

/**
 * Execution statistics providing timing and performance metrics.
 */
public interface ExecutionStatistics {
    
    /**
     * Get time window size for statistics.
     * @return Time window size
     */
    long getTimeWindowSize();
    
    /**
     * Get time window size unit.
     * @return TimeUnit for the time window
     */
    TimeUnit getTimeWindowSizeUnit();
    
    /**
     * Get time window statistics.
     * @return TimeWindowStatistics for the current window
     */
    TimeWindowStatistics getTimeWindowStatistics();
    
    /**
     * Get last finished execution timestamp.
     * @return Timestamp of last finished execution
     */
    Date getLastStartTime();
}

/**
 * Time window statistics providing metrics over a specific time period.
 */
public interface TimeWindowStatistics {
    
    /**
     * Get time window size.
     * @return Time window size in milliseconds
     */
    long getTimeWindow();
    
    /**
     * Get minimum execution time in the window.
     * @return Minimum execution time in milliseconds
     */
    long getMinimumDuration();
    
    /**
     * Get maximum execution time in the window.
     * @return Maximum execution time in milliseconds
     */
    long getMaximumDuration();
    
    /**
     * Get average execution time in the window.
     * @return Average execution time in milliseconds
     */
    long getAverageDuration();
    
    /**
     * Get total number of requests in the window.
     * @return Request count
     */
    long getRequestCount();
    
    /**
     * Get requests per second rate.
     * @return Requests per second
     */
    double getRequestsPerSecond();
}

Resource Statistics

Detailed statistics for individual resources and their methods.

/**
 * Resource-level statistics providing metrics for individual resources.
 */
public interface ResourceStatistics {
    
    /**
     * Get execution statistics for this resource.
     * @return ExecutionStatistics for all resource method calls
     */
    ExecutionStatistics getRequestExecutionStatistics();
    
    /**
     * Get resource method statistics.
     * @return Map of method signatures to ResourceMethodStatistics
     */
    Map<String, ResourceMethodStatistics> getResourceMethodStatistics();
    
    /**
     * Get resource path.
     * @return Resource path pattern
     */
    String getPath();
}

/**
 * Resource method statistics providing method-level performance metrics.
 */
public interface ResourceMethodStatistics {
    
    /**
     * Get execution statistics for this method.
     * @return ExecutionStatistics for this specific method
     */
    ExecutionStatistics getRequestExecutionStatistics();
    
    /**
     * Get HTTP method.
     * @return HTTP method (GET, POST, etc.)
     */
    String getMethod();
    
    /**
     * Get method path within the resource.
     * @return Method-specific path pattern
     */
    String getPath();
    
    /**
     * Get total invocation count.
     * @return Number of times this method was invoked
     */
    long getInvocationCount();
}

Response Statistics

HTTP response statistics and metrics.

/**
 * Response statistics providing HTTP response metrics.
 */
public interface ResponseStatistics {
    
    /**
     * Get response code statistics.
     * @return Map of status codes to their occurrence counts
     */
    Map<Integer, Long> getResponseCodes();
    
    /**
     * Get count for specific response code.
     * @param responseCode HTTP response code
     * @return Count of responses with this code
     */
    Long getResponseCodeCount(int responseCode);
    
    /**
     * Get total response count.
     * @return Total number of responses
     */
    long getTotalResponseCount();
    
    /**
     * Get last response timestamp.
     * @return Timestamp of last response
     */
    Date getLastResponseTime();
}

JMX Integration

JMX MBean interfaces for monitoring through standard JMX tools.

/**
 * Application MXBean for JMX monitoring of application-level metrics.
 */
public interface ApplicationMXBean {
    
    /**
     * Get application name.
     * @return Application name
     */
    String getApplicationName();
    
    /**
     * Get application class name.
     * @return Fully qualified application class name
     */
    String getApplicationClass();
    
    /**
     * Get initialization timestamp.
     * @return Application initialization timestamp
     */
    Date getStartTime();
    
    /**
     * Get registered resource classes.
     * @return Set of resource class names
     */
    Set<String> getRegisteredClasses();
    
    /**
     * Get registered resource instances.
     * @return Set of resource instance class names
     */
    Set<String> getRegisteredInstances();
    
    /**
     * Get registered providers.
     * @return Set of provider class names
     */
    Set<String> getProviders();
    
    /**
     * Get configuration properties.
     * @return Map of configuration property names to values
     */
    Map<String, Object> getProperties();
}

/**
 * Resource MXBean for JMX monitoring of resource-level metrics.
 */
public interface ResourceMXBean {
    
    /**
     * Get resource path.
     * @return Resource path pattern
     */
    String getPath();
    
    /**
     * Get resource class name.
     * @return Fully qualified resource class name
     */
    String getResourceClass();
    
    /**
     * Get average request duration.
     * @return Average duration in milliseconds
     */
    long getAverageRequestDuration();
    
    /**
     * Get minimum request duration.
     * @return Minimum duration in milliseconds
     */
    long getMinRequestDuration();
    
    /**
     * Get maximum request duration.
     * @return Maximum duration in milliseconds
     */
    long getMaxRequestDuration();
    
    /**
     * Get total request count.
     * @return Total number of requests
     */
    long getRequestCount();
    
    /**
     * Get requests per second.
     * @return Current requests per second rate
     */
    double getRequestRate();
}

/**
 * Resource method MXBean for JMX monitoring of method-level metrics.
 */
public interface ResourceMethodMXBean {
    
    /**
     * Get HTTP method.
     * @return HTTP method string
     */
    String getHttpMethod();
    
    /**
     * Get method path.
     * @return Method path pattern
     */
    String getPath();
    
    /**
     * Get method invocation count.
     * @return Number of method invocations
     */
    long getInvocationCount();
    
    /**
     * Get average execution time.
     * @return Average execution time in milliseconds
     */
    long getAverageExecutionTime();
    
    /**
     * Get minimum execution time.
     * @return Minimum execution time in milliseconds
     */
    long getMinExecutionTime();
    
    /**
     * Get maximum execution time.
     * @return Maximum execution time in milliseconds
     */
    long getMaxExecutionTime();
}

Statistics Listener

Interface for receiving statistics updates and notifications.

/**
 * Monitoring statistics listener for receiving statistics updates.
 */
public interface MonitoringStatisticsListener {
    
    /**
     * Called when monitoring statistics are updated.
     * @param event Statistics event containing updated data
     */
    void onStatistics(MonitoringStatisticsEvent event);
    
    /**
     * Monitoring statistics event containing updated statistics data.
     */
    interface MonitoringStatisticsEvent {
        
        /**
         * Get the updated monitoring statistics.
         * @return Current MonitoringStatistics snapshot
         */
        MonitoringStatistics getStatistics();
        
        /**
         * Get the event timestamp.
         * @return When the statistics were captured
         */
        Date getTimestamp();
    }
}

Usage Examples:

import org.glassfish.jersey.server.monitoring.*;

// Custom statistics listener
public class CustomStatisticsListener implements MonitoringStatisticsListener {
    
    @Override
    public void onStatistics(MonitoringStatisticsEvent event) {
        MonitoringStatistics stats = event.getStatistics();
        
        // Log overall request statistics
        ExecutionStatistics execStats = stats.getRequestStatistics();
        TimeWindowStatistics windowStats = execStats.getTimeWindowStatistics();
        
        System.out.println("=== Request Statistics ===");
        System.out.println("Total requests: " + windowStats.getRequestCount());
        System.out.println("Requests/sec: " + windowStats.getRequestsPerSecond());
        System.out.println("Avg duration: " + windowStats.getAverageDuration() + "ms");
        System.out.println("Min duration: " + windowStats.getMinimumDuration() + "ms");
        System.out.println("Max duration: " + windowStats.getMaximumDuration() + "ms");
        
        // Log resource-specific statistics
        Map<String, ResourceStatistics> uriStats = stats.getUriStatistics();
        for (Map.Entry<String, ResourceStatistics> entry : uriStats.entrySet()) {
            String uri = entry.getKey();
            ResourceStatistics resourceStats = entry.getValue();
            
            System.out.println("=== Resource: " + uri + " ===");
            ExecutionStatistics resExecStats = resourceStats.getRequestExecutionStatistics();
            TimeWindowStatistics resWindowStats = resExecStats.getTimeWindowStatistics();
            System.out.println("  Requests: " + resWindowStats.getRequestCount());
            System.out.println("  Avg: " + resWindowStats.getAverageDuration() + "ms");
            
            // Method-level statistics
            Map<String, ResourceMethodStatistics> methodStats = resourceStats.getResourceMethodStatistics();
            for (Map.Entry<String, ResourceMethodStatistics> methodEntry : methodStats.entrySet()) {
                ResourceMethodStatistics methodStat = methodEntry.getValue();
                System.out.println("    " + methodStat.getMethod() + " " + methodStat.getPath() + 
                                 ": " + methodStat.getInvocationCount() + " calls");
            }
        }
        
        // Log response statistics
        ResponseStatistics responseStats = stats.getResponseStatistics();
        Map<Integer, Long> responseCodes = responseStats.getResponseCodes();
        System.out.println("=== Response Codes ===");
        for (Map.Entry<Integer, Long> entry : responseCodes.entrySet()) {
            System.out.println("  " + entry.getKey() + ": " + entry.getValue() + " responses");
        }
        
        // Log exception statistics
        Map<Class<?>, ExceptionMapperStatistics> exceptionStats = stats.getExceptionMapperStatistics();
        if (!exceptionStats.isEmpty()) {
            System.out.println("=== Exception Statistics ===");
            for (Map.Entry<Class<?>, ExceptionMapperStatistics> entry : exceptionStats.entrySet()) {
                System.out.println("  " + entry.getKey().getSimpleName() + " exceptions handled");
            }
        }
    }
}

// Register statistics listener
ResourceConfig config = new ResourceConfig()
    .packages("com.example.resources")
    .register(CustomStatisticsListener.class);

Install with Tessl CLI

npx tessl i tessl/maven-org-glassfish-jersey-core--jersey-server

docs

async-processing.md

configuration-properties.md

index.md

monitoring.md

request-processing.md

resource-configuration.md

resource-model.md

spi.md

wadl.md

tile.json