How to call a method on application load

Imagine that you need to do some work just before or a little after an application starts. It might be to prepare some configuration or to start some background services. There is a very simple solution to call some java method on an ADF Application boot time – It’s a servlet with “Load on application start” option.

Prepare Servlet class

Create class BootstrapServlet class which extends javax.servlet.http.HttpServlet.

adf_howto_01_bootstrap_01

Override the init method and paste your code. In my case is just to print some message.

Register in web.xml

Register Booststrap servlet in web.xml  file(1,2,3,4,5). Make sure the load order is right. In my case I changed(6) faces servlet order to 10 and BooststrapServlet to 5.

adf_howto_01_bootstrap_02

Full web.xml content:

Test

Run application. As you can see the BootstrapServlet started before faces servlet.

adf_howto_01_bootstrap_03

The use cases (examples)

  • You can start some background thread (service) in the Servlet init method which periodically do some work – For instance: asynchronous file processing.
  • You can prepare some configuration.
  • You can send a notification about application state.
Facebooktwittergoogle_plusredditlinkedinmail

Leave a Reply