Jersey app-run initialization code on startup to initialize application
Asked Answered
B

2

8

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.

Barrada answered 15/9, 2011 at 0:8 Comment(0)
L
15

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

Libido answered 15/9, 2011 at 8:22 Comment(6)
Thanks,Pavel for the hint.I put a Provider class in and put the package in web.xml but it doesn't execute the onLoaded method though the provider class sems to be loaded. jersey resources="Root resource classes found by ScanningResourceConfig" at application startup.Barrada
log should look like: ""INFO: Root resource classes found: class com.sun.jersey.samples.helloworld.resources.HelloWorldResource Sep 15, 2011 11:21:23 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Provider classes found: class com.sun.jersey.samples.helloworld.resources.CustomContextResolver class com.sun.jersey.samples.helloworld.resources.Listener"".<br />Problem might be in Jersey version - please try it with latest version - 1.9 (1.7 or newer should be sufficient)Libido
is there any way to run this initialization code after the server has initialized the web app fully ? I am running into a catch-22 problem in that I need the web app fully started by Tomcat before I can run my login call once ?Barrada
login "call" like service is registering itself to another service? It depends on what you consider fully started app, you might want to wait for first incoming request ..Libido
This no longer seems to work with Jersey 2.11. How to achieve this with this version?Sismondi
Jersey 2.x is much different from 1.x branch; you might want to take a look at ResourceModelVisitor jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/… (which should be equivalent of AbstractResourceModelListener) or you can try maybe better event listener for the original question: ApplicationEventListener. See jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/… and jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/….Libido
T
2

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;
        }
    }
Telpher answered 17/9, 2021 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.