简体   繁体   中英

Access log is empty with Grizzly server and Jersey

I am unable to make Grizzly server write an access log.

The simplest setup is as follows:

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.grizzly.http.server.accesslog.AccessLogBuilder;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.ServerProperties;

import java.net.URI;
import java.util.HashMap;

public class App {
    public static void main(String[] args) throws Exception {
        URI uri = new URI("http://localhost:12987/");
        ResourceConfig rc = new ResourceConfig().registerClasses(Greeter.class);
        HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc);
        new AccessLogBuilder("hi.access.log").instrument(server.getServerConfiguration());
        server.start();
    }
}

Code of resource:

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/")
public class Greeter {
    @GET
    @Path("hi")
    public String hi() { return "hi!"; }
}

Gradle script containing dependency descriptions and versions:

buildscript {
    ext.java_version = '1.8'
    ext.jersey_version = '2.25.1'

    repositories {
        mavenCentral()
    }
}

apply plugin: 'java'
sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile "javax.servlet:servlet-api:2.5"
    compile "org.glassfish.jersey.core:jersey-server:$jersey_version"
    compile "org.glassfish.jersey.containers:jersey-container-servlet-core:$jersey_version"
    compile "org.glassfish.jersey.containers:jersey-container-grizzly2-http:$jersey_version"
}

I observe an unexpected behavior where the access-log file is created with the start of the server but nothing is written to it when requests are made. The server sends responses and works fine in every other aspect.

I was trying to debug the thing and did not help because Jersey relies on tons of reflection and dynamic loading. I was also trying to add properties for monitoring, specifically ServerProperties.MONITORING_ENABLED but again that did not change anything.

What should I add or configure to get access log working?

It turns out that the only needed change is

HttpServer server = GrizzlyHttpServerFactory.createHttpServer(uri, rc, false);

Note the third arguments false which tells the factory not to start server immediately. Otherwise any configurations of the server (after it has been started) do not affect the behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM