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.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
Vijay
  • 595
  • 1
  • 13
  • 27

2 Answers2

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

Pavel Bucek
  • 5,304
  • 27
  • 44
  • 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. – Vijay Sep 15 '11 at 17:34
  • 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"".
    Problem might be in Jersey version - please try it with latest version - 1.9 (1.7 or newer should be sufficient)
    – Pavel Bucek Sep 16 '11 at 00:02
  • 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 ? – Vijay Sep 16 '11 at 18:35
  • 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 .. – Pavel Bucek Sep 17 '11 at 10:24
  • This no longer seems to work with Jersey 2.11. How to achieve this with this version? – P Varga Aug 25 '14 at 22:38
  • 1
    Jersey 2.x is much different from 1.x branch; you might want to take a look at ResourceModelVisitor https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/model/ResourceModelVisitor.html (which should be equivalent of AbstractResourceModelListener) or you can try maybe better event listener for the original question: ApplicationEventListener. See https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/monitoring/ApplicationEventListener.html and https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/server/monitoring/ApplicationEvent.Type.html. – Pavel Bucek Aug 26 '14 at 09:06
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;
        }
    }
Matthew
  • 10,361
  • 5
  • 42
  • 54