Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to run server-side program on background

 
Raymond Fish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All ^_^
i want to run a perl script by IE browser, and it may takes 5-10 minutes. So i think it would be better to run it on background. How to write the JSP code??
btw, the script is "phredPhrap",which is a bioinformatics freeware. ^_^
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see if I understand what you want - you want to do a request from a browser that starts a long process on the server and then come back later and pick up the results?
Bill
 
Raymond Fish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets see if I understand what you want - you want to do a request from a browser that starts a long process on the server and then come back later and pick up the results?
yes...
i write the following code :
<%@ page contentType="text/html" session="false" import="java.io.*" %>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html">
</head>
<body>
<%
Runtime runtime = Runtime.getRuntime();
Process process =null;
String command = "./assemble.sh"
try
{
process =runtime.exec(command);

}
catch(IOException e )
{
out.println(e);
runtime.exit(1);
}
%>
</body>
</html>

assemble.sh is a shell script and it calls some perl & C program, the shell script is:
/usr/local/bin/phredPhrap&

"phredPhrap" is a perl script and it calls some C program
because it may takes 10-20 min, so i run it on background. the program proceed about 20 sec and then the process is gone.
if i write the shell script like:
/usr/local/bin/phredPhrap
it will run but hang at a C program
this problem really drives me crazy, please give me a hand...thanx

[ April 18, 2003: Message edited by: Raymond Fish ]
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should not try to do the actual exec in a JSP - the server will timeout the connection etc etc. Instead, write a "helper" class (typically people use the JavaBean convention) with its own Thread to start the exec and handle the results. The bean could have session scope and your JSP can ask it for progress reports every time your user makes another request.
One great advantage of this approach is that you can develop and debug the bean off line, thus avoiding all the complications of the web app environment and enabling you to test more thoroughly.
Bill
 
Raymond Fish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One great advantage of this approach is that you can develop and debug the bean off line, thus avoiding all the complications of the web app environment and enabling you to test more thoroughly.
Bill
Thanks for your advise, Bill
but it still not work......
i write a java program names "runrun.java" :
import java.io.*;
public class runrun{
String thistime = null;
String lib = null;
String line=null;
InputStream is =null;
InputStreamReader isr=null;
BufferedReader br =null;
public void setthistime(String s){
thistime = s;
}
public void setlib(String s){
lib = s;
}
public String getexec(){
try{
String command1 = "/usr/local/jakarta-tomcat-4.1.18/bin/assemble.sh " + thistime + " " + lib;
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(command1);
is = process.getInputStream();
isr=new InputStreamReader(is);
br =new BufferedReader(isr);
while( (line = br.readLine()) != null )
{
System.out.println(line);
}
is.close();
isr.close();
br.close();
return "execute!!";
}catch(Exception e){

}finally{
return "done!";
}
}
public static void main(String args[]){
runrun r = new runrun();
r.setthistime("20030421");
r.setlib("mps07");
r.getexec();
}
}
and the result is just like what i do in JSP...
please check what wrong i done
or there are another ways to execute a program except using "runtime.exec()"?
thanks for your great help
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yaknow, when I see code like this:

I just want to scream!
You are asking what is wrong but you are throwing away exactly what you need to find out what is wrong!
e.printStackTrace( System.out );
will very likely answer your question.
Bill
 
Raymond Fish
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i use try-catch-finally this way because this code is no error in complie time and runtime.
of course, it's a bad demo......
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13058
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IF you get a compile time error, the compiler is trying to tell you something important. Listen to your compiler, the compiler is your friend. If you get a runtime exception, the JVM is trying to tell you something important. Listen to your JVM, the JVM is your friend.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This technique will very likely hang if the output is large.
You need to handle Process.getErrorStream() as well as .getInputStream(). If you don't read this, and if the C program or shell script writes anything to STDERR, your program will hang when you read the inputstream from .getInputStream().
Your execed process will be attempting to write to STDERR, which has a full buffer, so will wait until the other end reads some of the buffer. Your Java process will be attempting to read from the execed STDOUT, which is empty. So the Java process waits until some more input comes.
This is very likely what is happening. It might be that the C program writes all its output to STDERR. It might be that you have not properly set up the C program -- I don't think it will inherit environment variables in the same way as when you type the command from the shell prompt.
You might try to redirect STDOUT and STDERR to a temporary file in your shell script.
Also, to clarify: I think William Brogden wants you to put something in your catch block, like

But since you say that the program is hanging, I doubt that an Exception is actually being thrown.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic