我开发了Simple Web-Application,它将Source path作为输入参数,并且基于Source路径,批处理执行在该路径中进行,并将输出消息作为成功或失败返回。 批处理执行是Assync所以我使用了Executors.newFixedThreadPool(3)。 目前正在执行但是返回消息无法获取并且正在获得零点异常,如下所示。请检查我的代码并指导我在哪里出错。
<struts>
<constant name="struts.devMode" value="false" />
<package name="metadataservice" extends="struts-default">
<action name="Runner"
class="org.ms.proj.RunnerAction"
method="init">
<result name="success">/Success.jsp</result>
</action>
</package>
</struts>
public class RunnerAction extends ActionSupport {
public String init()
{
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
String Sourcepath=request.getParameter("srcpath");
System.out.println("Source Path :"+Sourcepath);
ExecutorService executor = Executors.newFixedThreadPool(3);
Callable<String> callable = new MyExecuorThread(Sourcepath);
Future<String> future = executor.submit(callable);
ThreadMonitor addfutur=new ThreadMonitor(future);
addfutur.CheckThreadReturnValue();
return SUCCESS;
}
}
My Home.jsp
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="Runner" method="post">
<input type="text" value="" name="srcpath"/>
<input type="submit" value="Run">
</form>
public class ThreadMonitor {
private List<Future<String>> myfutrelist;
public ThreadMonitor(Future<String> future)
{
this.myfutrelist.add(future);
}
public void CheckThreadReturnValue()
{
for(Future<String> fut : myfutrelist){
try {
System.out.println("Thread Return Method :"+fut.get());
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public class MyExecuorThread implements Callable<String>{
private String srcpath;
private int exitValue=0;
public MyExecuorThread(String source)
{
this.srcpath=source;
}
@Override
public String call() throws Exception {
// TODO Auto-generated method stub
String mylog = "";
try {
final File batchFile = new File("D://Runner//bin//runner.bat");
ProcessBuilder builder = new ProcessBuilder();
builder.directory(new File(srcpath));
String[] cmd = { "cmd", "/c",batchFile.getAbsolutePath(),"-Dsonar.login="+"abc","-Dsonar.password="+"chn@27","-X"};
for (int x = 0; x < cmd.length; x++) {
System.out.println("Command :" + cmd[x]);
}
builder.command(cmd);
// builder.redirectErrorStream(true);
Process process;
process = builder.start();
InputStream is1 = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is1);
BufferedReader br = new BufferedReader(isr);
String line;
File logfile;
File logpath=new File(srcpath+File.separator+"LOG");
if(logpath.isDirectory())
{
logfile=new File(logpath+File.separator+"my.log");
}
else
{
logpath.mkdir();
logfile=new File(logpath+File.separator+"my.log");
}
logfile.createNewFile();
while ((line = br.readLine()) != null) {
System.out.println(line);
mylog+=line+"\n";
}
exitValue = process.exitValue();
FileUtils.writeStringToFile(logfile,mylog);
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();
process.destroy();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(exitValue==0)
{
//System.out.println("success");
return "Analysis Success";
}
else
{
//System.out.println("failed");
return "Analysis Failed";
}
}
}
java.lang.NullPointerException
org.ms.proj.ThreadMonitor.<init>(ThreadMonitor.java:12)
org.ms.proj.RunnerAction.init(RunnerAction.java:30)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
ognl.OgnlRuntime.invokeMethod(OgnlRuntime.java:891)
ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1293)
ognl.ObjectMethodAccessor.callMethod(ObjectMethodAccessor.java:68)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethodWithDebugInfo(XWorkMethodAccessor.java:117)
com.opensymphony.xwork2.ognl.accessor.XWorkMethodAccessor.callMethod(XWorkMethodAccessor.java:108)
ognl.OgnlRuntime.callMethod(OgnlRuntime.java:1369)
ognl.ASTMethod.getValueBody(ASTMethod.java:90)
ognl.SimpleNode.evaluateGetValueBody(SimpleNode.java:212)
ognl.SimpleNode.getValue(SimpleNode.java:258)
ognl.Ognl.getValue(Ognl.java:494)
ognl.Ognl.getValue(Ognl.java:458)
请让我知道如何解决这个问题,以及如何在批量执行后获取返回消息。请告诉我哪里出错了,无论如何都会对我有很大的帮助。