aspose file tools*
The moose likes Struts and the fly likes threads and struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "threads and struts" Watch "threads and struts" New topic
Author

threads and struts

trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
hello all,

Can the action class implement the Runnable interface and have a thread inside which queries the DB after every 2 seconds and then sleeps.
Depending on the value of the db column the actions class forwords the result to respective jsp page?

and if above is possible can someone please let me give some idea as to how to implement it? I mean do I have to use normal thread concept like start method and run method?


thanks,
trupti
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Can the action class implement the Runnable interface and have a thread inside which queries the DB after every 2 seconds and then sleeps.

What exactly are you trying to achieve? While it is technically possible to extend Action and implement Runnable, IMO doing so doesn't make sense architecturally or logically. The Struts framework does not treat Actions as Runnables so unless you are creating your own ActionServlet as well and overriding the normal request-response cycle logic, I don't know how making an Action Runnable serves any purpose in the context of the Struts framework. If you want to spawn another thread from an Action, create a separate Runnable implementation class.

Depending on the value of the db column the actions class forwords the result to respective jsp page?

Are you trying to create a page that automatically refreshes? If so, you can't do that from the server side. You have to do it with an HTML meta refresh tag.


Junilu - [How to Ask Questions] [How to Answer Questions]
trupti nigam
Ranch Hand

Joined: Jun 21, 2001
Posts: 613
junilu,

I am having a JSP page which has "yes" and "no" buttons .
When the user presses the yes button,from this jsp he gets forworded to another waiting JSP (That i have to write, the action class for it and the action form).
Now the requirement is that..This JSP should display something like "your request is being processed" and in the mean time the action class should have a logic that will query the database and retrieve one column value and based on the value he has to dislay some error message if it's an error or forword it to another jsp page.(this should be done after every 2 seconds..)hence I am thiking about having a thread which does this and then sleeps for some time.
The waiting JSp will timeout if the whole process takes more than 2 minutes.
Can you recommend something ,how this can be implemented?

Thanks,
trupti
Junilu Lacar
Bartender

Joined: Feb 26, 2001
Posts: 4757
    
    7

Yes, this can be done but not by making the Action into a Runnable. As I said, you can use a meta-refresh HTML tag in conjunction with a couple of Actions and a helper class.

Here's an outline:

WorkerRunnable - a class that implements Runnable. This class will have some way of reporting it's progress.

Action1 - Linked to your "Yes" button. Kicks off the processing by spawning a thread with WorkerRunnable. This Action does not wait for the WorkerRunnable to finish but immediately forwards to a Status page.

Status page - has HTML meta refresh tag that hits Action2 every X seconds.

Done page - page that gets displayed when process is completed successfully or times out.

Action2 - Query WorkerRunnable status, creates appropriate message, then forwards to either the Status page or the Done page.
john fuhr
Greenhorn

Joined: Feb 27, 2001
Posts: 16
Originally posted by Junilu Lacar:
Yes, this can be done but not by making the Action into a Runnable. As I said, you can use a meta-refresh HTML tag in conjunction with a couple of Actions and a helper class.

Here's an outline:

WorkerRunnable - a class that implements Runnable. This class will have some way of reporting it's progress.

Action1 - Linked to your "Yes" button. Kicks off the processing by spawning a thread with WorkerRunnable. This Action does not wait for the WorkerRunnable to finish but immediately forwards to a Status page.

Status page - has HTML meta refresh tag that hits Action2 every X seconds.

Done page - page that gets displayed when process is completed successfully or times out.

Action2 - Query WorkerRunnable status, creates appropriate message, then forwards to either the Status page or the Done page.
john fuhr
Greenhorn

Joined: Feb 27, 2001
Posts: 16
(the above was my fault for not entering stuff right)

i did very much the same as above as Junilu:
however i used struts to handle my query
afterwards a bottom frame opened up which ran another servlet. inside that servlet, i grabbed an instance of a Runnable class, "Listener", which checked on the data already inputted. this Listener class was a singleton class - which could only be initialized once.
however, any other class (a servlet, an Action class) could read the data collection process which Listener responded to.
the process:
the first Action class (on submit) initialized the Listener class, the db process could then grab that instance and report to it ('record one', 'record two'...) the reporting servlet (bottom frame) would then simply report what Listener was reporting as String values

the above kepr a brwser window open for as along as necessary...



Originally posted by john fuhr:
[QB][/QB]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: threads and struts