Interview Questions

How can I setup initialization code for a service )?

SOAP Interview Questions and Answers


(Continued from previous question...)

How can I setup initialization code for a service )?

Q:
How can I setup initialization code for a service (ie. code that runs once only to prep a database connection for example)?


A:
One way to achieve this is to install another servlet which runs at your servlet engine startup time. This means that all initialization gets done at startup so you trap any errors before your services get called and the first client to call the soap service is not penalized in the case where your startup code is heavy (this can be the case if you need to do a database login for example).Taking tomcat as an example in the $TOMCAT_HOME/webapps/soap/WEB-INF directory you will see web.xml which will already contain your soap rpcrouter servlet - just add another servlet which will call a class that runs your initialization code. Ensure that the specifies "1" for true. Note that you can also specifiy parameters to pass to your initialization class so you do not have to hardcode anything (like database names for example). Example web.xml file is below.


<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>
rpcrouter
</servlet-name>
<servlet-class>
org.apache.soap.server.http.RPCRouterServlet
</servlet-class>
<init-param>
<param-name>ServicesStore
<param-value>DeployedServices.ds
</init-param>
</servlet>
<servlet>
<servlet-name>
myStartupServlet
</servlet-name>
<
com.mycompany.myproject.mypackagename.MyInitializationCodeClass
</servlet-class>
<init-param>
<param-name>-X
<param-value>myStartupParameterValue1
<param-name>-Y
<param-value>myStartupParameterValue2
<param-name>-Z
<param-value>myStartupParameterValue3
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
</web-app>

(Continued on next question...)

Other Interview Questions