Vijay
Vijay

Reputation: 597

Jersey app-run initialization code on startup to initialize application

I have an app built with Jersey.I need to do some initialization on startup of the webapp/war in the Tomcat 7 container by running an application specific login/code.

What is the best way to do this with Jersey ? I have used ContextListener with contextInitialized()before in a Servlet environment. I need to make sure the Jersey resources are loaded before I make this call.

Upvotes: 8

Views: 8631

Answers (2)

Matthew
Matthew

Reputation: 11347

For Jersey 2.x you can do the following:

    @Provider
    private static class MyFeature implements Feature {


        @Override
        public boolean configure(FeatureContext context) {
             //code goes here
             return true;
        }
    }

Upvotes: 2

Pavel Bucek
Pavel Bucek

Reputation: 5324

Not sure what you mean by "Jersey resources are loaded before", but if you want to really plug in into Jersey init process.. Jersey has several "monitoring" plugin points (not widely advertised or documented) and what I'm going to describe is being called after initialization of AbstractResourceModel - so right after app startup.

Try this:

@Provider
public class Listener implements AbstractResourceModelListener {

    @Override
    public void onLoaded(AbstractResourceModelContext modelContext) {
        System.out.println("##### resource model initiated");
    }
}

It should happen only once per app lifecycle, I'm not very sure about reloading, but you don't need to bother by it if you are not using that feature (anyway, you should put some check there to avoid multiple invocation if could cause some issues).

Upvotes: 15

Related Questions