Discussions

Web tier: servlets, JSP, Web frameworks: Can I use threads in servlets?

  1. Can I use threads in servlets? (2 messages)

    Currently the system on which I am working is built on CORBA platform. We are planning to migrate this application into websphere application server. During the startup of current application it starts 5 threads & each one loads the data into the cache( basically into hashmaps). Each thread will take atleast 15 minutes to complete its job. I want the similar functionality even in application server also. We are planning to use servlet as a start up class and load the data into cache. Can I use threads in servlet similar to what we have in CORBA environment? If I dont use threads to load the data and go in sequence then my server startup time goes more than an hour from 20 minutes. Is there any efficient way of solving this problem?
  2. hi,

    check out this article :


    Add concurrent processing with message-driven beans
    Process a request concurrently in the J2EE framework
    http://www.javaworld.com/javaworld/jw-07-2003/jw-0718-mdb.html

    Bhagvan K
    http://www.architectcorner.com
  3. Yes, you can use threads in servlets for this kind of initialization.

    Typical, servlet-based initialization is implemented in the "init()" method of some servlet (say, InitServlet), whose configuration in the web.xml file specifies a "load-on-startup" value. This forces the servlet to be loaded and initialized when the application starts.

    You could launch your thread logic from this init() method.