tomcat - RequestDispatcher instantiates servlet which has already been instantiated via load-on-startup -
i have servlet annotated this:
@webservlet(value="/myserviceservlet", loadonstartup=1)
this results in servlet being instantiated on application start , init()
method being called. perfect! (in init()
method, thread started perform db maintenance runs each hour)
now, on end user actions, reach servlet using getservletcontext().getrequestdispatcher("/myserviceservlet").include(request, response)
trigger other db stuff, first time try this, servlet gets instantiated 1 more time , init()
method called (of course), results in 2 similar threads running. not perfect!
it seems if loadonstartup=1
not put servlet in servlet context, hence when try reach request dispatcher needs instantiated.
how can be? how fix 1 instance of servlet? need started along application, service started in init()
should run immediately.
the application deployed on tomcat 7.0.57.
(in init() method, thread started perform db maintenance runs each hour)
o lord, wonderful, believe in memory leak? running thread in servlet?! need context-listener(javax.servlet.servletcontextlistener
), not directly, it's not safe.
public class run_thread implements servletcontextlistener{ @override public void contextinitialized(servletcontextevent sce) {system.out.println("init: daemon stuffs");/*run teh trhead*/} @override public void contextdestroyed(servletcontextevent sce) { system.out.println("hult: daemon stuffs");/*hult teh trhead*/ } }
and don't forge register bi web.xml
<listener> <listener-class>pack.run_thread</listener-class> </listener>
more better solution let external application db maintain, or @ least invoke servlet path intervally.
now, on end user actions, reach servlet using
what user action? how? using servlet? requets.getrequestdispatcher()
instead of getservletcontext()
you may share servlet code, appreciated.
Comments
Post a Comment