🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: weblogic.developer.interest.servlet
Article #1

How to send "processing" message to user while working

#1
From: Dimitri Rakitine
Date: Mon, 09 Oct 2000 19:29
145 lines
3758 bytes
Here is a complete example of a servlet kicking off long-running 
task and displaying messages about it's progress (without creating 
new threads and without using JMS):

Test.java:

import java.io.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import weblogic.time.common.*;
import weblogic.common.*;
import weblogic.jndi.*;
import javax.naming.*;

/**
 * execute stuff on weblogic's execute threads
 * usage:
 * <pre>
 * class Foo implements Runnable {
 *  public void run() {
 *    ...
 *  }
 * }
 *
 * Foo foo = new Foo(...);
 * MyThread thr = new MyThread(foo);
 * thr.start();
 * </pre>
 */
class MyThread implements Schedulable, Triggerable {

    boolean  done     = false;
    Runnable runnable = null;
    ScheduledTriggerDef std;

    public void run() {
	if(runnable != null) {
	    runnable.run();
	}
    }

    public MyThread() {
    }

    public MyThread(Runnable runnable) {
	this.runnable = runnable;
    }

    public boolean start() {
	boolean ok = false;
	try {
	    T3ServicesDef t3 = (T3ServicesDef)(new InitialContext()).lookup("weblogic.common.T3Services");
	    std = t3.time().getScheduledTrigger(this, this);
	    std.schedule();
	    ok = true;
	} catch(NamingException ne) {
	    System.out.println(ne.getMessage());
	} catch(TimeTriggerException tte) {
	    System.out.println(tte.getMessage());
	}
	return ok;
    }

    public void trigger(Schedulable sched) {
	try {
	    run();
	} catch(Throwable t) {
	    System.out.println(t);
	}
	done = true;
    }

    public long schedule(long time) {
	return done ? 0 : time;
    }
}

class LongTask implements Runnable {

    public int status = 0;
    public long timeStarted;
    public long timeCompleted;
    
    int seconds;

    LongTask(int seconds) {
	this.seconds = seconds;
    }

    public void run() {
	status = 1;
	timeStarted = System.currentTimeMillis();
	try {
	    // simulate long running process
	    Thread.sleep(seconds * 1000);
	} catch(InterruptedException ie) {}
	timeCompleted = System.currentTimeMillis();
	status = 2;
    }

}

public class Test extends HttpServlet {

    public void service(HttpServletRequest req, HttpServletResponse res)
	throws ServletException, IOException {

	String msg = null;

	HttpSession session = req.getSession();

	LongTask longTask = (LongTask)session.getAttribute("longTask");
	if(longTask == null) {
	    session.setAttribute("longTask", longTask = new LongTask(10));
	    (new MyThread(longTask)).start();	    
	}

	switch(longTask.status) {
	case 0: 
	    msg = "Waiting"; 
	    break;
	case 1: 
	    msg = "In progress";
	    break;
	case 2: 
	    msg = "Done, started:" + longTask.timeStarted + " completed:" + longTask.timeCompleted;
	    session.removeAttribute("longTask");
	    break;
	}	
	
	PrintWriter out = new PrintWriter(new OutputStreamWriter(res.getOutputStream()));
	res.setContentType("text/html");
	out.println("<html><body>");
	out.println("Your request status:" + msg);
	out.println("</body></html>");
	out.flush();
	out.close();
    }
}

Dimitri
Technical Details
Message-ID: <39e20e6c$1@newsgroups.bea.com>
Path: arc1-ca.pugleaf.net!newsgroups.bea.com!209.10.235.131

Show Full Headers
Complete Article Headers
From: "Dimitri Rakitine" <dimitri@firstworld.net>
Sender: "Dimitri Rakitine" <dimitri@firstworld.net>
Reply-To: "Dimitri Rakitine" <dimitri@firstworld.net>
Subject: How to send "processing" message to user while working
Newsgroups: weblogic.developer.interest.servlet
X-User-Info: 208.227.212.117     
NNTP-Posting-Host: 209.10.235.131
Message-ID: <39e20e6c$1@newsgroups.bea.com>
Date: 9 Oct 2000 11:29:00 -0800
X-Trace: 9 Oct 2000 11:29:00 -0800, 209.10.235.131
Organization: BEA SYSTEMS Inc
Lines: 145
XPident: dr
Path: arc1-ca.pugleaf.net!newsgroups.bea.com!209.10.235.131