caprica
caprica

Reputation: 4146

How to force an embedded Grizzly web application context to start

I use an embedded Grizzly web server to host RESTful web-services with Jersey. This is all working correctly.

My question is how to force the web application context to eagerly initialise when I start the server rather than waiting for the first incoming client request. This is a minor problem, but one I would like to solve.

I start the embedded server like this:

public final class TestApplication {

    public TestApplication() throws Exception {
        HttpServer httpServer = GrizzlyHttpServerFactory.createHttpServer(
            "http://0.0.0.0:8888",
            new ResourceConfig()
                .registerInstances(
                    new TestBinder(),
                )
                .registerClasses(
                    JacksonJsonProvider.class,
                )
                .packages(
                    AbstractResource.class.getPackage().getName()
                ),
            true
        );
    }
}

The "TestBinder" configures the dependency injection that I need and that class looks like this:

final class TestBinder extends AbstractBinder {

    @Override
    protected void configure() {
        bind(CatalogManager.class).to(CatalogManager.class).in(Singleton.class);
    }
}

Finally, "CatalogManager" is a singleton that is used to pre-load and cache all of the static data that my application exposes via the RESTful web-services.

The essence of "CatalogManager" is this:

public final class CatalogManager {

    @PostConstruct
    public void initialise() {
        // Load and cache a large application data-set here...
    }
}

So the problem is that the dependency injection and consequently the @PostConstruct method do not run when the server starts up, instead it waits until the first application request and the first user of my application gets a long delay.

Upvotes: 1

Views: 470

Answers (0)

Related Questions