ku916
asked on
Servlet with thread
I have this servlet that executes a system cmd (like to run a perl script). Some of the perl scripts can take a very long time to finish and it would hold my servlet would not come back until they are done.
So I'm trying to use Thread, start() and run(). But all the examples I've found are Thread running applet.
Here is what I want to do
public class ExecServlet extends HttpServlet {
...
...
...
private void launchExecution(String runType, int statusID,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException , IOException
{
StringBuffer launchStr = new StringBuffer();
launchStr.append("perl long_running.pl");
String retString = doServerCommand(launchStr. toString() , request);
if (retString.indexOf("Succes sfully Running") == -1) {
msgBean.setPrimaryMsg("Err or Launching execution"
forward("/exec_err_msg.jsp ", request, response);
} else {
StringBuffer s = new StringBuffer("Execution Launched!");
int strIdx = retString.indexOf("Log File is");
if (strIdx > -1) {
s.append("<P>" + retString.substring(strIdx ) + "<P>");
}
msgBean.setPrimaryMsg(s.to String());
forward("/exec_msg.jsp", request, response);
}
private synchronized String doServerCommand(String cmd, HttpServletRequest request)
{
...
...
Process cmdProc = Runtime.getRuntime().exec( cmd);
// get its output (your input) stream
//
BufferedReader cmdIn = new BufferedReader(new InputStreamReader(cmdProc. getInputSt ream()));
BufferedReader cmdErr = new BufferedReader(new InputStreamReader(cmdProc. getErrorSt ream()));
try {
String inStr;
while (( inStr = cmdIn.readLine()) != null) {
cmdStdOutput.append(inStr + "\n");
}
while (( inStr = cmdErr.readLine()) != null) {
cmdErrOutput.append(inStr + "\n");
}
}
...
...
So I'm trying to use Thread, start() and run(). But all the examples I've found are Thread running applet.
Here is what I want to do
public class ExecServlet extends HttpServlet {
...
...
...
private void launchExecution(String runType, int statusID,
HttpServletRequest request,
HttpServletResponse response)
throws ServletException , IOException
{
StringBuffer launchStr = new StringBuffer();
launchStr.append("perl long_running.pl");
String retString = doServerCommand(launchStr.
if (retString.indexOf("Succes
msgBean.setPrimaryMsg("Err
forward("/exec_err_msg.jsp
} else {
StringBuffer s = new StringBuffer("Execution Launched!");
int strIdx = retString.indexOf("Log File is");
if (strIdx > -1) {
s.append("<P>" + retString.substring(strIdx
}
msgBean.setPrimaryMsg(s.to
forward("/exec_msg.jsp", request, response);
}
private synchronized String doServerCommand(String cmd, HttpServletRequest request)
{
...
...
Process cmdProc = Runtime.getRuntime().exec(
// get its output (your input) stream
//
BufferedReader cmdIn = new BufferedReader(new InputStreamReader(cmdProc.
BufferedReader cmdErr = new BufferedReader(new InputStreamReader(cmdProc.
try {
String inStr;
while (( inStr = cmdIn.readLine()) != null) {
cmdStdOutput.append(inStr + "\n");
}
while (( inStr = cmdErr.readLine()) != null) {
cmdErrOutput.append(inStr + "\n");
}
}
...
...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
-- Points for CEHJ
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TimYates
EE Cleanup Volunteer
I will leave a recommendation in the Cleanup topic area that this question is:
-- Points for CEHJ
Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
TimYates
EE Cleanup Volunteer
http://java.sun.com/docs/books/tutorial/essential/threads/index.html
You need to decide what happens if someone invokes the servlet before the process has finished, typically you would tell the user that the process is still executing. So you need to track if the thread is running or not.