1

I am using the JerseyServletContainer mechanism in order to deploy my REST web services. The configuration in the web.xml looks like below:

<servlet>
    <servlet-name>MyServlet</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.myservlet.classes
        </param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>

What i would like to achieve is that my REST Resource class to be constructed before the first HTTP call to the REST service.

At the moment, even though i have set

    <load-on-startup>0</load-on-startup>

seems like the actual class' constructor is called by Jersey's ServletContainer only when a HTTP request is firstly made to that resource.

Is there a way to achieve that?

I need to add that, at the moment i have just added a ServletContextListener which makes a HTTP call to itself. But i would like to avoid that hacky way.

Any help/advice much appreciated?

EDIT: I need to add that i am using @Singleton annotation on my Resource class as i need only one instance throughout the application

nikkatsa
  • 1,751
  • 4
  • 26
  • 43
  • Possibly this should give you more idea - http://stackoverflow.com/questions/28917768/load-on-startup-and-jersey – aksappy Nov 23 '15 at 10:02

1 Answers1

0

You can use the @Immediate scope as mentioned in this answer. You should check out the "related issue" link. There are some issues related to memory leaks with the Immediate thread. It seems the issue has been resolved. so you should make sure to use the latest version to get the patch.

The linked answer uses a web.xml-less configuration. But if you want to keep your web.xml (with the @ApplicationPath), you can declare the ResourceConfig class in the web.xml if you want.

public class JerseyApplication extends ResourceConfig {

    @Inject
    public JerseyApplication(ServiceLocator locator) {
        ServiceLocatorUtilities.enableImmediateScope(locator);
        packages("com.myservlet.classes");
    }
}

<servlet>
    <servlet-name>MyServlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.foo.JerseyApplication</param-value>
    </init-param>
    <load-on-startup>0</load-on-startup>
</servlet>
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720