Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 892
Vijay Avatar asked Sep 15 '11 00:09

Vijay


1 Answers

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).

like image 113
Pavel Bucek Avatar answered Oct 01 '22 19:10

Pavel Bucek