How to call a method on application load

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.

package com.bettercoding.adf.howto01.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

public class BootstrapServlet extends HttpServlet {
    public BootstrapServlet() {
        super();
    }

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        super.init(servletConfig);
        System.out.println(">> BootstrapServlet run!");
    }
}

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

...
 <servlet>
    <servlet-name>BootstrapServlet</servlet-name>
    <servlet-class>com.bettercoding.adf.howto01.servlet.BootstrapServlet</servlet-class>
    <load-on-startup>5</load-on-startup>
  </servlet>
...

Full web.xml content:

<?xml version = '1.0' encoding = 'UTF-8'?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 version="3.0">
 <servlet>
 <servlet-name>Faces Servlet</servlet-name>
 <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
 <load-on-startup>10</load-on-startup>
 </servlet>
 <servlet>
 <servlet-name>resources</servlet-name>
 <servlet-class>org.apache.myfaces.trinidad.webapp.ResourceServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>BIGRAPHSERVLET</servlet-name>
 <servlet-class>oracle.adf.view.faces.bi.webapp.GraphServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>BIGAUGESERVLET</servlet-name>
 <servlet-class>oracle.adf.view.faces.bi.webapp.GaugeServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>MapProxyServlet</servlet-name>
 <servlet-class>oracle.adf.view.faces.bi.webapp.MapProxyServlet</servlet-class>
 </servlet>
 <servlet>
 <servlet-name>BootstrapServlet</servlet-name>
 <servlet-class>com.bettercoding.adf.howto01.servlet.BootstrapServlet</servlet-class>
 <load-on-startup>5</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>Faces Servlet</servlet-name>
 <url-pattern>/faces/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>resources</servlet-name>
 <url-pattern>/adf/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>resources</servlet-name>
 <url-pattern>/afr/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>BIGRAPHSERVLET</servlet-name>
 <url-pattern>/servlet/GraphServlet/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>BIGAUGESERVLET</servlet-name>
 <url-pattern>/servlet/GaugeServlet/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>MapProxyServlet</servlet-name>
 <url-pattern>/mapproxy/*</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
 <servlet-name>resources</servlet-name>
 <url-pattern>/bi/*</url-pattern>
 </servlet-mapping>
 <filter>
 <filter-name>trinidad</filter-name>
 <filter-class>org.apache.myfaces.trinidad.webapp.TrinidadFilter</filter-class>
 </filter>
 <filter>
 <filter-name>ServletADFFilter</filter-name>
 <filter-class>oracle.adf.share.http.ServletADFFilter</filter-class>
 </filter>
 <filter-mapping>
 <filter-name>trinidad</filter-name>
 <servlet-name>Faces Servlet</servlet-name>
 <dispatcher>FORWARD</dispatcher>
 <dispatcher>REQUEST</dispatcher>
 <dispatcher>ERROR</dispatcher>
 </filter-mapping>
 <filter-mapping>
 <filter-name>ServletADFFilter</filter-name>
 <servlet-name>Faces Servlet</servlet-name>
 <dispatcher>FORWARD</dispatcher>
 <dispatcher>REQUEST</dispatcher>
 </filter-mapping>
 <context-param>
 <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
 <param-value>client</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
 <param-value>false</param-value>
 </context-param>
 <context-param>
 <description>If this parameter is true, there will be an automatic check of the modification date of your JSPs, and saved state will be discarded when JSP's change. It will also automatically check if your skinning css files have changed without you having to restart the server. This makes development easier, but adds overhead. For this reason this parameter should be set to false when your application is deployed.</description>
 <param-name>org.apache.myfaces.trinidad.CHECK_FILE_MODIFICATION</param-name>
 <param-value>false</param-value>
 </context-param>
 <context-param>
 <description>Whether the 'Generated by...' comment at the bottom of ADF Faces HTML pages should contain version number information.</description>
 <param-name>oracle.adf.view.rich.versionString.HIDDEN</param-name>
 <param-value>true</param-value>
 </context-param>
 <context-param>
 <description>Security precaution to prevent clickjacking: bust frames if the ancestor window domain(protocol, host, and port) and the frame domain are different. Another options for this parameter are always and never.</description>
 <param-name>org.apache.myfaces.trinidad.security.FRAME_BUSTING</param-name>
 <param-value>differentOrigin</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
 <param-value>true</param-value>
 </context-param>
 <context-param>
 <param-name>oracle.adf.view.rich.geometry.DEFAULT_DIMENSIONS</param-name>
 <param-value>auto</param-value>
 </context-param>
 <context-param>
 <param-name>oracle.adf.view.rich.SYNCROWS</param-name>
 <param-value>enable</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
 <param-value>true</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.FACELETS_DECORATORS</param-name>
 <param-value>oracle.adfinternal.view.faces.facelets.rich.AdfTagDecorator</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
 <param-value>oracle.adfinternal.view.faces.facelets.rich.AdfFaceletsResourceResolver</param-value>
 </context-param>
 <context-param>
 <param-name>javax.faces.FACELETS_VIEW_MAPPINGS</param-name>
 <param-value>*.jsf;*.xhtml</param-value>
 </context-param>
 <mime-mapping>
 <extension>swf</extension>
 <mime-type>application/x-shockwave-flash</mime-type>
 </mime-mapping>
 <mime-mapping>
 <extension>amf</extension>
 <mime-type>application/x-amf</mime-type>
 </mime-mapping>
</web-app>

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.

Leave a Reply

avatar
  Subscribe  
Notify of
Close Menu