mule 带项目启动

1)bat执行程序:

@ECHOOFF
SETLOCALENABLEDELAYEDEXPANSION
cd..
SETclasspath=classes/.
title
SETjvmargs=-Xms64m-Xmx512m-XX:+UseParallelGC-XX:ParallelGCThreads=%NUMBER_OF_PROCESSORS%
SETmainClass=com/flow/MuleStart
java-Duser.timezone=Asia/Shanghai-Xms64m-Xmx512m%jvmargs%-classpath%classpath%%mainClass%
endlocal
PAUSE

2)

java的main类:

packagecom.flow;

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileFilter;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.lang.reflect.Method;
importjava.net.MalformedURLException;
importjava.net.URL;
importjava.net.URLClassLoader;

importorg.mule.MuleServer;
importorg.mule.api.MuleContext;
importorg.mule.api.MuleException;
importorg.mule.api.context.MuleContextBuilder;
importorg.mule.config.DefaultMuleConfiguration;
importorg.mule.config.builders.AutoConfigurationBuilder;
importorg.mule.context.DefaultMuleContextBuilder;
importorg.mule.context.DefaultMuleContextFactory;

/**
* 项目 启动mule的方法
*/
publicclassMuleStart{

publicstaticvoidmain(Stringargs[])throwsException{
//先加载mule的jar包和项目所需要的jar包
JarLoaderjarLoader=newJarLoader((URLClassLoader)ClassLoader.getSystemClassLoader());
loadjar(jarLoader,System.getProperty("user.dir")+"/mulelib");
loadjar(jarLoader,System.getProperty("user.dir")+"/lib");

args=newString[]{"sms"};
System.out.println("执行"+args[0]+"的mule任务");
MuleServermuleServer=newMuleServer("./sms_"+args[0]+".xml");
muleServer.start(true,true);
System.out.println("成功执行"+args[0]+"的mule任务");
System.out.println("等待stop指令结束"+args[0]+"的mule任务");

InputStreamReaderis=newInputStreamReader(System.in);
BufferedReaderbr=newBufferedReader(is);
booleanflag=true;
System.out.println("等待stop指令结束"+args[0]+"的mule任务");
while(flag){
try{
Thread.sleep(1000);
Stringname=br.readLine();
if("stop".equals(name)){
muleServer.shutdown();
flag=false;
System.out.println("成功结束"+args[0]+"的mule任务,ctrl+c退出cmd窗口");
}elseif(name.length()>0){
System.out.println("停止服务,请输入stop命令!");
}
}catch(Exceptione){
System.out.println("系统错误!");
e.printStackTrace();
}
}
try{
is.close();
br.close();
}catch(IOExceptione){
System.out.println("关闭流发生错误!");
e.printStackTrace();
}
}

privatestaticclassJarLoader{
privateURLClassLoaderurlClassLoader;
publicJarLoader(URLClassLoaderurlClassLoader){
this.urlClassLoader=urlClassLoader;
}

publicvoidloadJar(URLurl)throwsException{
MethodaddURL=URLClassLoader.class.getDeclaredMethod("addURL",URL.class);
addURL.setAccessible(true);
addURL.invoke(urlClassLoader,url);
}
}

privatestaticvoidloadjar(JarLoaderjarLoader,Stringpath)throwsMalformedURLException,Exception{
Filelibdir=newFile(path);
if(libdir!=null&&libdir.isDirectory()){

File[]listFiles=libdir.listFiles(newFileFilter(){

@Override
publicbooleanaccept(Filefile){
//TODOAuto-generatedmethodstub
returnfile.exists()&&file.isFile()&&file.getName().endsWith(".jar");
}
});

for(Filefile:listFiles){
jarLoader.loadJar(file.toURI().toURL());
}

}else{
System.out.println("[ConsoleMessage]Directory["+path+"]doesnotexsit,pleasecheckit");
System.exit(0);
}
}

}

你可能感兴趣的:(mule)