• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

threads and struts

 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 8779
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
trupti nigam
Ranch Hand
Posts: 627
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 8779
81
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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]
 
Paolo Finamore
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone,

I'm developing something similar. Following your outline i deduce that Action2 needs a reference to thread generated by Action1. How can obtain the thread reference from Action2?

Thanks in advance

Junilu Lacar wrote: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.
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seeing as how the last topic in this thread is 12 years old, things have changed.  If you are using Struts 2 (and you should be), look at the ExecuteAndWaitInterceptor
 
Paolo Finamore
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I know, i've already work on several MVC framework (such as Struts2, JSF, Spring MVC...) and this would be easier using the latter. But now i'm working on an old large project built with Struts v1.3 and cannot overturn it because the lack of time.

Joe Ess wrote:Seeing as how the last topic in this thread is 12 years old, things have changed.  If you are using Struts 2 (and you should be), look at the ExecuteAndWaitInterceptor
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic