Search code examples
initializationstartuprestletrestlet-2.0

What is the most appropriate place for placing (startup) initialization code in a Restlet application?


Where can I embed startup initialization code in a Restlet web application, without using a ServletContextListener?

I wish to have transparent deployment of my Restlet to a web server like JBoss/Tomcat and would like to get rid of the initialization logic in the Listener - so as to be able to conveniently deploy it outside of a web server, if the need be - definitely not for heavy production use, but it's valuable nevertheless.

Would inserting it into org.restlet.Component's constructor ensure that it'll only execute once? Is that the right place to put it?

public class MyComponent extends org.restlet.Component
{
  public MyComponent() //constructor
  {
    //insert initialization code here that should run ONLY ONCE?

    this.getDefaultHost().attach(new MyApplication()); // MyApplication extends org.restlet.Application
  }
}

I went through the docs and also looked at a similar post: RESTlet startup initialization deprecated? but I'm still not sure if it's the right way. I would like to remove the dependency on the Listener if at all possible.


Solution

  • Using the constructor of the Component is a good place for initialization processing and you can be sure that such processing are only executed once.

    You can notice that the method start / stop of the component can also be used in your case. Don't forget to call the super method in them. These methods are called when you start / stop your component that is commonly done once...

    Hope it helps you. Thierry