开发者

How to write a long init in a webapp

I use glassfish to run a webapp. I work with netbeans.

My webapp need a very long init.

I write that in a ServletContextListener.contextInitialized method.

I put in my code some logs.

If I write my init code inside the contextInitialized method, seems to me the init is interrupted by someone : the logs stop at a moment, I don't understand why.

If I write my code开发者_运维知识库 inside a sub thread, there no log at all.

What is the best way to do a long init in a webapp ?

Thanks.


ServeletContextListener is the good place than the servlet init. Incase, for some reason(resource shortage or memory management issue) container destroys the servlet and recreates, then this may screw up the data for other servlets inside the same app.

Try to investigate the reason for the stoppage and keep ServletContextListner as is


An option is to have a servlet load-on-startup. Make a servlet load on start-up like this

<web-app>

 <servlet>
    <servlet-name>InitProcess</servlet-name>
    <servlet-class>com.my.ServletName</servlet-class>
    <load-on-startup>1</load-on-startup>
 </servlet>

</web-app>

The servlet will be loaded at start-up and init(), method of the Servlet will be called by the container. You may have your code there.

The number in <load-on-startup> decides the order in which servlets are loaded. See here. This is supported by Tomcat and Weblogic, at least.


To get ServletContext use this:

 ServletContext ctx = getServletContext();

See this code block at Java2s

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜