Starting a new Thread in JSP


Web tier: servlets, JSP, Web frameworks: Starting a new Thread in JSP

  1. Starting a new Thread in JSP (6 messages)

    I am writting JSP pages that interact with a Database. Some of the DB commits are fairly lengthy (5 minutes) and I was wondering if I could spawn a new thread and return a page back to the user saying that there request is being processed and they will be notified when it is complete.

    The new thread could then update the DB and when finished notify the user that it has been completed.

    Does anyone have any advice on how I could do somethiing like this?


    Threaded Messages (6)

  2. Starting a new Thread in JSP[ Go to top ]

    test, please ignore
  3. Starting a new Thread in JSP[ Go to top ]

    Your question breaks down into two pieces:
    1. Kick off the commit process while allowing the user to continue with the application.
    2. Notify the user when the commit process is complete.

    If piece 1. were simply - "Kick off the commit process and tell the user to sit there and wait" then you could:
    1. write "Please wait for five minutes or more" to the jsp out.
    2. flush the buffer with out.flush(), which should display the message on the browser even though the JSP isn't finished
    3. start the commit process.
    4. when the commit process completes, write "All done" or spit out some javascript to load a new page.
    a. the http stream is held open for the length of the commit.
    b. the user has to sit there and wait.

    If piece 1. remains as originally stated (allow the user to continue working), then you will have to kick off a thread to do the processing. The complicated part is notifying the user that the processing is finished. The only way to do that in a HTML based application would be to have the processing thread put a message somewhere in a queue when the commit is complete. The user application would have to check that queue periodically to see if it is time to notify the user. If the user application uses a central "controller" servlet for all http requests, checking the queue can be accomplished in one place. If there is no central controller, in other words, processing is handled by individual JSP pages, then each JSP page presented to the user must check the queue to see if there are any messages for the user regarding the commit process. The queue itself could be easily implemented as a table in a database, or (with less scalability) as an object in the user's servlet session or as an object in the application scope.

    Tom Cox
  4. Starting a new Thread in JSP[ Go to top ]

    You stated
    >If piece 1. remains as originally stated (allow the user >to continue working), then you will have to kick off a >thread to do the processing.

    I do not know how to kick off a thread to do the processing. OR... if it is good practice to do so. Can you please explain further?

  5. Starting a new Thread in JSP[ Go to top ]

    Mark, I think Tom's suggestion is a good one for solving your particular problem. You should consult Java documentation to become familiar with using Threads. They're a challenge. I'll add these additional thoughts.

    Are you certain it's safe to let the user continue working when the outcome of your db update is unkown? You'll have to take care in your design that the user does not inadvertantly kick off the same request a second time (using back button, for instance).

    Another option is to write an HTML page that refreshes itself every few seconds to check back on the progress of your update. It won't allow your user to continue with work, but it will at least provide them with feedback. If you can partition your db update into parts, you could design this self-refreshing page to indicate how many parts of the whole have been processed.

    If you're unfamiliar with how to have a page refresh itself, look for documentation on the <meta http-equiv="Refresh" content="..."> tag.


  6. Starting a new Thread in JSP[ Go to top ]

    Hello ,

    Yes I am also facing the same problem the problem is I am creating a Catalogue which is 4000k and thetime it is taking is 10 minutes ..

    Tom as you sugested I started new thread from my jsp and i showed some message to user ..
    Thanks ! you will be notified by email like that ...
    But the problem is ...the thread started properly and went to the class which implements Runnable and then from there it is calling a session bean which inturn returning datas from d/b I haad successfully shown the page like thanks and user clsed the windo also ..but in my server side the thread i started and running ..that is aslo ok .
    but when it try to get the Remote object of session bean
    it is throwing Exception ..means I got the home object ..then home.create() is throwing Exception...
    BUt one thing I am wondered is ..without thread ..just i called tyhe call method to create the full catalogue is getting the handle and 10-15 minutes after comleteing the process successfully ..but in this case the problem i faced the page in which i am calling the class method which in turn calling session beans method got browser ...see the exception..
    FYI:I am using weblogic 5.1.0 and bean is statless session bean...the thread is like this...
    Thread tr = new Thread(Object of the class which Implements runnable);
    in the class where I override
    Public void run (){
    call to class function()which in turn calling bean and creating catalogue.

    Does anyone can any suggetion to solve this..?
  7. Ridiculous design[ Go to top ]


     if its taking 10-15 mins for a transaction, u better check ur design and code..looks ridiculous to me.