Discussions

Web tier: servlets, JSP, Web frameworks: Lifecycle of Servlet Thread....

  1. Lifecycle of Servlet Thread.... (4 messages)

    Does any one know what happens to the Servlet Thread(I mean the Thread executing for one user for the Servlet) after it returns .

    essentially I am trying to do somethinglike this...

    /*
     * AsynServlet.java
     *
     * Created on June 2, 2001, 12:33 PM
     */

    package com.nj.util;

    import java.util.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;

    public class AsynServlet extends javax.servlet.http.HttpServlet implements Runnable {

       
       private static int _idGen = 0;
       
       private Hashtable _readerMap = null ;
       private ObjectFIFO _queue = null;
       private Thread _reader = null;
       
       
        public void init(javax.servlet.ServletConfig servletConfig) throws javax.servlet.ServletException {
           this._queue = new ObjectFIFO(100);
           this._readerMap = new Hashtable();
           this._reader = new Thread(this);
           this._reader.start();
           
           
        }
        
        public void service(javax.servlet.ServletRequest servletRequest,javax.servlet.ServletResponse servletResponse) throws javax.servlet.ServletException, java.io.IOException {
           int i = getID();
           MessageContainer con = new MessageContainer(i);
           this._readerMap.put( new Integer (i) ,servletResponse);
           try{
           _queue.add(con);
           }catch(java.lang.InterruptedException ie)
           {
            ie.printStackTrace();
           }
        }
        
        public static int getID()
        {
         return _idGen ++;
        }
         
        public void run() {
            MessageContainer mc = null;
           try{
            mc = (MessageContainer)this._queue.remove();
           }catch(java.lang.InterruptedException ex)
           {
            ex.printStackTrace();
           }
           ServletResponse response = (ServletResponse)this._readerMap.get(new Integer(mc.getId()));
           
           String str = mc.getResponse().toString();
           
           try {
              PrintWriter pw = response.getWriter();
             pw.println("<HTML>");
             pw.println("<BODY>");
             pw.println(str);
             pw.println("</BODY>");
              pw.println("</HTML>");
             pw.flush();
             pw.close();

    //out.flush();
    } catch (IOException e) {
                   System.out.println(" ERROR " + e.getMessage());
                   e.printStackTrace();
                }
        }
        
        
    }


    This Async Servlet does not seem to be working ... What happens to the ServletResponse when the Thread is completed with its execution ..

    Help Appreciated...

    Niraj

  2. I forgot to mention ... Esseentially I am trying to make an Asynchronous Servlet ... Does anyone have a better idea

    And Also the ObjectFIFO is a FIFO Queue and there is a timelag (some value ) between a add and remove to queue

    ~niraj
  3. Ok... first off... you can trigger asynchonous operations with a http servlet... but the servlet itself is synchronous...this is becaus http is a request-response protocol. There are several alternatives to what you are doing.... have a separate thread handle the asynchronous behaviour.... if this process is context sensetive, geta handle and store it in the session. If this thread needs to be share register it with a directory / naming service

    Another approch is to use async. messaging.... drop messages to a topic, another process picks the messages up from the topic and takes care processing it

    cheers
    adi
  4. Adi ,

    That is exactly what I am doing . I am having my my own Asynbc Thread... The Servlet implemts Runnable ...

    But this program does not seem to work... The Response Object is probably destroyed or is junk after the Servlet Thread returns control..

    Niraj
  5. I meant to say have a diiferent class for the thread...
    Also the user will not get the response you are trying to write out, since the servlet container will close the i/o stream to the servlet service() has finished execution.

    another thing.... don't use the service method, use doGet or doPost...just call one from the other....
    doGet(....){
    doPost(req, res);
    }
    .
    .
    .
    doPost{
      //your code
    }

    the continer puts initilization and cleanup code in service to manage resources.

    I donot see the point of what you are trying to accomplish here...... Async messaging on a Req-Resp wire protocol(http)!!