tomcat - How to kill a java program with java program -
i have scenario, need kill java process.
here scenario:
i have web application in web application have implemented listener
class below
public class mylistener extends servletcontextlistener { java.lang.process ps = null; public void contextinitialized(servletcontextevent arg0) { // excuted when web application executed string command= "cmd.exe start maybatch";// command execute batch program triggers java program ps = runtime.getruntime().exec(command); } public void contextdestroyed(servletcontextevent arg0) { //this part executed when server shutdown happens. // here want close java process triggered when project deployed. if( ps !=null) ps.destroy(); } }
}
what requirement close java process when tomcat shutting down , start java program when web application deployed.
everything working fine till start java process when project deployed.
but dont know how close java process triggered when project deployed
any appreciated. in advance.
sorry not clear.. mybatch
lanches new java program
.
first if want informed shutdown of web application , kill subprocess should use contextdestroyed() method instead of contextinitialized() in code snippet. need swap content of methods.
public class mylistener extends servletcontextlistener { java.lang.process ps = null; public void contextinitialized(servletcontextevent arg0) { // excuted when web application executed string command= "cmd.exe start maybatch";// command execute batch program triggers java program ps = runtime.getruntime().exec(command); } public void contextdestroyed(servletcontextevent arg0) { //this part executed when server shutdown happens. // here want close java process triggered when project deployed. if( ps !=null) ps.destroy(); } } }
upd
is possible avoid usage of batch file ? if yes able shutdown spawned java application. ps found link quite useful:
start java process (using runtime.exec / processbuilder.start) low priority?
Comments
Post a Comment