Java球衣监测统计

Java jersey monitoring statistics

我正在尝试激活网络服务的监控统计信息。但是我得到空指针异常。如何正确激活?我不明白球衣的文档。这是我的代码:

package com.app.rest;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;
import org.glassfish.jersey.server.monitoring.MonitoringStatistics;
import org.glassfish.jersey.server.monitoring.TimeWindowStatistics;
@Path("/")
public class TestingEvents extends ResourceConfig {
public TestingEvents()
{
    ResourceConfig cnf =   new ResourceConfig(TestingEvents.class);
     cnf.property(ServerProperties.MONITORING_ENABLED, true);
}



@Inject
 Provider<MonitoringStatistics> monitoringStatisticsProvider;

@Path("/test")
@GET
public void test()
{
    System.out.println("Invoked.........");
    final MonitoringStatistics snapshot = monitoringStatisticsProvider.get();

    System.out.println(snapshot.getRequestStatistics().toString());
 //  final TimeWindowStatistics timeWindowStatistics = snapshot.getRequestStatistics().getTimeWindowStatistics().get(0);
    //System.out.println("request count: " + timeWindowStatistics.getRequestCount()+", average request processing [ms]: " + timeWindowStatistics.getAverageDuration());
}}

和我的网站 xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>RestCounter</display-name>
<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.app.rest</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>MyApplication</servlet-name>
    <url-pattern>/myApp/*</url-pattern>
</servlet-mapping>

例外情况:

SEVERE: Servlet.service() for servlet [MyApplication] in context with path     [/RestCounter] threw exception [java.lang.NullPointerException] with root cause
java.lang.NullPointerException
at com.app.rest.TestingEvents.test(TestingEvents.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.run(AbstractJavaResourceMethodDispatcher.java:143)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:160)
at org.glassfish.jersey.server.model.internal.VoidVoidDispatcherProvider$VoidToVoidDispatcher.doDispatch(VoidVoidDispatcherProvider.java:78)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:97)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime.run(ServerRuntime.java:303)
at org.glassfish.jersey.internal.Errors.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:286)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

已调用显示在控制台上,但随后程序崩溃了。请帮忙

首先了解一些配置方面...

ResourceConfig 是 web.xml 的替代品。它们 可以 一起使用,因为您可以配置 web.xml 以使用您的 ResourceConfig 子 class,但我很少看到有必要这样做。如果你想不带 web.xml,你可以做类似

的事情
@ApplicationPath("/myApp")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("com.app.rest");
    }
}

这相当于您声明 ServletContainer,设置要扫描的包,并将 url-映射设置为 /myApp/*。通过上面的配置,就可以完全去掉web.xml(假设你有jersey-container-servlet依赖,而且你在Servlet3.x环境下;在Servlet2.x环境,你需要使用 web.xml).

有一件事你不会做,就是让你的资源class扩展ResourceConfig。所以摆脱子classing(在你的TestingEvents class),绝对摆脱你在构造函数中拥有的东西。

至于设置监控属性...

如果您决定使用 ResourceConfig subclass...

你可以简单地做

@ApplicationPath("/myApp")
public class AppConfig extends ResourceConfig {
    public AppConfig() {
        packages("com.app.rest");
        property(ServerProperties.MONITORING_STATISTICS_ENABLED, true);
    }
}

如果您决定使用 web.xml...

你应该保留 javadoc of the ServerProperties handy. Each of the static fields, have String values, which for the most part can be configured in the web.xml as an <init-param> of the Jersey Servlet. If you look at the ServerProperties.MONITORING_STATISTICS_ENABLED,你会看到 String 值为

jersey.config.server.monitoring.statistics.enabled

所以要将其设置为初始化参数,请执行此操作

<servlet>
    <servlet-name>MyApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.app.rest</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.monitoring.statistics.enabled</param-name>
        <param-value>true</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>