struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jim Newsham <news...@hotu.com>
Subject Re: struts & generating progress messages
Date Fri, 05 Jan 2001 19:35:54 GMT
Kitching Simon wrote:

> Hi,
>
> I currently have a model-1 web application
> ie in which the .jsp pages are responsible
> for invoking the business logic.
>
> I am planning to move the webapp to struts,
> for all the obvious and traditional reasons.
> However, there is one nice feature of the
> current code that I can't see how to perform
> inside an MVC framework like struts:
>
> On submitting a particular form, there is a
> sequence of 4 operations to perform, each of
> which takes somewhere between 5 and 30
> seconds to perform. Currently, what is done is
> to generate an almost complete HTML page,
> and force it to be flushed to the browser; as
> each long-duration step starts and completes,
> a fragment of page containing CSS-positioned
> html/ javascript is flushed to the browser. The
> effect is very nice - a list of the steps appears
> on the screen, an hourglass appears next to
> each step as it starts, and a tick or error message
> appears next to each one as it completes.
>
> Does anyone have any idea how to generate the
> same sort of effect (essentially alternating between
> business logic and presentation output) in struts?
>
> Thanks in advance,
>
> Simon

How about creating a separate thread to perform the tasks, and
forwarding immediately to the jsp page in your main thread.  Create an
object which can wait for the tasks to complete, and attach that object
to the request before forwarding.

As an example, create a TaskMonitor (sample code below; this code is
untested, and I usually don't program threads so don't trust it to work
without some scrutiny).

Task thread.  Create a thread to perform your tasks and give it a
reference to this monitor.  Each time a task is complete, call
monitor.taskComplete(taskid).

Main thread.  Attach monitor to request.  Forward to jsp.  The jsp can
call monitor.waitForTask(taskid) for each task in sequence.

public class TaskMonitor {

  boolean[] taskComplete;

  public TaskMonitor(int taskCount) {
    taskComplete = new boolean[taskCount];
  }

  public synchronized waitForTask(int taskid) {
    if (taskid < 0 || taskid >= taskComplete.length
|| taskComplete[taskid]) return;
    while(true) {
      try {
        wait();
      }
      catch(InterruptedException ie) { }
      if (taskComplete[taskid]) return;
    }
  }

  public synchronized taskComplete(int taskid) {
    if (taskid > 0 && taskid < taskComplete.length) {
      taskComplete[taskid] = true;
      notifyAll();
    }
  }

}


Mime
View raw message