Thread View: weblogic.developer.interest.servlet
1 messages
1 total messages
Started by Dimitri Rakitine
Mon, 09 Oct 2000 19:29
How to send "processing" message to user while working
Author: Dimitri Rakitine
Date: Mon, 09 Oct 2000 19:29
Date: Mon, 09 Oct 2000 19:29
145 lines
3758 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads