*
The moose likes Threads and Synchronization and the fly likes Thread not running Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Thread not running " Watch "Thread not running " New topic
Author

Thread not running

Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31
We have a web application running on Oracle Application server. I have designed a jsp page where it gets name and email id then when submitting the page, server sends a mail to mail id with some report (just data). The report generation happens in java side, so on form submit i call another jsp where it calls the java class to send the report. everything worked as expected, but generation report took long time to complete, so thought of doing it in thread. I have moved report creation code in thread's run method, but when i submitted the page, it is not calling the thread, but simply java class runs and terminated.
Code Snippet:



i could see in log file that jsp page is submitted and "ReportUtil_2" is called, but this class is not calling "new ReportProcessThread(request, report) ;". But when i run this as a stand alone Java application, everythg works fine. I hope some issue with Oracle server, but not getting any clue.

Please feed me with some points where i can look for!
Thanks for your time.
lavnish lalchandani
Ranch Hand

Joined: Feb 28, 2007
Posts: 79
Hi
I am not an expert at threads ... just adding my 2 cents, so that we can explore this further
(1)
For jsp/servlets each request that comes is handled in a seperate thread , this new thread is started and stopped by container, and you are scheduling ( not starting ) another thread inside it
Here is why i think it wont work :- your new report request will eventually run inside doPost() method. As long as you are in this method, the container keeps the connection open. Once you return from that method (and if you decide to handle report generation in a separate thread, doPost() will finish early) the container assumes you are done with the request and will close the connection. From the client perspective the report generation was interrupted by the server. And because of the asynchronous nature of threads the interruption will occur in random moment while generating report ( here i think before starting the report thread doPost completes hence your thread doesnt run)

(2) Can you add some debug / log message in constructor of ReportProcessThread, see if its called ( should be don't see an issue there )

(3) probably before your report thread gets a chance to run , container thread completes ... try modifying your code ... ( this MAY start report generation , but i will not be surprised if it stops in between as explained in point 1)
3.1) Remove the start() from constructor of ReportProcessThread , let it just implement runnable
3.2) Modify it to

4) Moreover it is a bad idea to start new thread per request as this scales poorly (and it is even prohibited by some specifications). What you can do is to use Servlet 3.0 asynchronous request and handle report generation asynchronously, but preferably using some pool of threads

Let me know did it work for you ? eager to understand / explore myself


lavnish.blogspot.com
Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31
Thank you for your points and sorry for posting updates very late.
As you said, I tried your option number 3, but still my thread is not starting.
I do nothing but below thing:


In jsp I had done the following i.e once the submit button is pressed , i call below jsp to start thread


I forward to "xxibeWebpCustomerRetentionReport.jsp" page to show message as "Thank you, you will receive your report in mail shortly" .

I tried many scenarios to call the thread, Below are some of my observations once I press submit:
1) After I submit for the above code, the same page retains for 5 to 6 seconds and goes to forwarded page and shows message. but thread is not called. I have put logs message, but none of messages are logged, even though 5 to 6 sec of thread sleep. [because Thread.currentThread().sleep(5000); Thread.sleep(1000);] i.e Thread sleep is called from run() only, but my log messages are not logged prior and after to Thread.sleep();

2) If i make Thread.sleep(55000), it takes so much time, but still my thread is not started. I hope Thread.sleep(55000) is container Thread and when i tried to get name of it, it says "AJPRequestHandler-HTTPThreadGroup-3"

3) I tried making all in one page, like show message and below to that , start thread in as "t.start();" in jsp itself. even this dint help.

or anyone please guide me how can i do the following:
As soon as, user clicks on "submit", the page should redirect to " "Thank you, you will receive your report in mail shortly"page and I want to start report processing., what happens when i tried as below


It takes around 2mins to complete the java processing for report and shows the Thanks you message. Is there any way to show the Thanks you message as soon as user clicks on "Submit" and then start java processing for reports.


For option 4) What I have to do? Please share some of sample codes or some url to start with.
I really appreciate some help on this.

Thanks.

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Dhamayanthi Karuppanan wrote:...
1) After I submit for the above code, the same page retains for 5 to 6 seconds and goes to forwarded page and shows message. but thread is not called. I have put logs message, but none of messages are logged, even though 5 to 6 sec of thread sleep. [because Thread.currentThread().sleep(5000); Thread.sleep(1000);] i.e Thread sleep is called from run() only, but my log messages are not logged prior and after to Thread.sleep();

2) If i make Thread.sleep(55000), it takes so much time, but still my thread is not started. I hope Thread.sleep(55000) is container Thread and when i tried to get name of it, it says "AJPRequestHandler-HTTPThreadGroup-3"


Are you saying that if you change the sleep commands in lines 8&9 of the first code snippet that your response takes longer to execute? This indicates that the thread IS being called. If you don't see the Log output then you are likely reviewing the wrong log files.

...
or anyone please guide me how can i do the following:
As soon as, user clicks on "submit", the page should redirect to " "Thank you, you will receive your report in mail shortly"page and I want to start report processing., what happens when i tried as below


It takes around 2mins to complete the java processing for report and shows the Thanks you message. Is there any way to show the Thanks you message as soon as user clicks on "Submit" and then start java processing for reports.


You would have to flush your response stream before starting the report processing. Note that this could lead to heavy load on your system as your request threads would remain in use during the length of the report process, which could prevent further requests from being processed.


Steve
Dhamayanthi Karuppanan
Ranch Hand

Joined: Oct 20, 2009
Posts: 31

Are you saying that if you change the sleep commands in lines 8&9 of the first code snippet that your response takes longer to execute? This indicates that the thread IS being called. If you don't see the Log output then you are likely reviewing the wrong log files.


Yes, If i change to 55 sec (55000), it takes that much time, but still my report generation is not started. I'm very sure, that I'm checking correct log file. Run() method did not log any of my messages, bt thread sleeps for specified time. really not knowing what happens behind the scene. I'm sure something is wrong, but not knowing what it is.

You would have to flush your response stream before starting the report processing.


I tried flushing my message, but it flushes only after report generation.

Any other ideas?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4164
    
  21

Dhamayanthi Karuppanan wrote:

Are you saying that if you change the sleep commands in lines 8&9 of the first code snippet that your response takes longer to execute? This indicates that the thread IS being called. If you don't see the Log output then you are likely reviewing the wrong log files.


Yes, If i change to 55 sec (55000), it takes that much time, but still my report generation is not started. I'm very sure, that I'm checking correct log file. Run() method did not log any of my messages, bt thread sleeps for specified time. really not knowing what happens behind the scene. I'm sure something is wrong, but not knowing what it is.


Check your assumptions, because one of them is wrong. If your application sleeps because of the sleep commands in your run() method, then your run() method is running. If you don't see any log output then the logging is not working as you expect it to, so check that out. As for why your report isn't being made - no idea but hopefully once you figure your logs out that will explain itself.


You would have to flush your response stream before starting the report processing.


I tried flushing my message, but it flushes only after report generation.

Any other ideas?

You have to put the response stream flush before starting the report process. Some browsers may react differently (internet explorer) and may not paint until the response is closed or some ie-specific buffer is filled - so a good thing to try would be close the response as well.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread not running
 
Similar Threads
Threads Doubt
Question about _jspService thread
Java Mail on Websphere .. plz Help
about class not found exception in eclipse
Thread mechanism for Request in web container