1

I currently have a Jersey webapp without a web.xml. It deploys nicely, but doesn't start up until it receives its first web request.

To get the webapp to load at startup, I could create a web.xml for the webapp and give a load-on-startup tag. However, I'd strongly prefer not to make a web.xml.

Is there a way to get a JAX-RS application to load at startup without web.xml? I'll even accept a solution that is specific to Jersey and/or Tomcat.

EDIT: I would also accept a solution that loads all apps in a Tomcat instance eagerly.

EDIT: Let me give a little more information on how the app is being deployed, per a comment.

The deployment process is not sophisticated.

The App will live on an EC2 instance running Ubuntu 12.04. I'm setting up one instance of the App by hand; once it works, I will make an AMI of the app and create additional copies of it as needed.

To deploy the app on the initial instance, I'm simply copying the WAR file to /var/lib/tomcat7/webapps and restarting Tomcat. No other webapps will be running on this Tomcat instance.

If any additional information would be useful, let me know! I'll happily add it.

EDIT: For clarity's sake, this is how my webapp Application class looks, at a high level:

@ApplicationPath("/")
public class App extends ResourceConfig {
    // ...
}

I'm using the Jersey-specific ResourceConfig class instead of the more general JAX-RS Application class because I'm using Jersey's built-in HK-2 to do some dependency injection.

Community
  • 1
  • 1
sigpwned
  • 6,957
  • 5
  • 28
  • 48
  • 1
    It would help a lot if you describe or show how you're deploying the app. – Ryan Stewart Oct 19 '13 at 15:47
  • I added some additional information about the deployment environment and process. If you want more information, or that's not the information you were looking for, let me know. – sigpwned Oct 19 '13 at 15:52

2 Answers2

1

The only way I can think of to do that is to switch to setting up the Jersey ServletContainer yourself and set its "load on startup" value to something greater than zero. You might use a ServletContainerInitializer (no relation--the naming is just a coincidence) to do it. If you happen to be using Spring, its WebApplicationInitializer offers the same mechanism with a slightly more convenient interface.

Another, rather hacky, way would be to write a class that extends ServletContainer and give it an appropriate Servlet 3.0 annotation, something like @WebServlet(value="/", loadOnStartup=1)

Community
  • 1
  • 1
Ryan Stewart
  • 126,015
  • 21
  • 180
  • 199
1

One solution would be to force a first request to the app by simply adding a call to curl or wget to your deployment script. It has the additional advantage of warming up any caches. And it can be used for testing if the deployment and the app really work. (Just check HTTP status code or some text on the response page...)

Robert Jack Will
  • 10,333
  • 1
  • 21
  • 29