Apache camel 如何通过命令行将参数传递给apachecamel?
我用它来启动我的Camel应用程序。我需要我的应用程序读取命令行参数来设置一些参数。因此,我不能使用属性文件 目前,我可以通过JVM系统属性传递参数,它工作得很好:Apache camel 如何通过命令行将参数传递给apachecamel?,apache-camel,Apache Camel,我用它来启动我的Camel应用程序。我需要我的应用程序读取命令行参数来设置一些参数。因此,我不能使用属性文件 目前,我可以通过JVM系统属性传递参数,它工作得很好: Application.java 公共类应用程序扩展org.apache.camel.spring.Main{ 公共静态void main(字符串[]args)引发异常{ 应用程序app=新应用程序(); 实例=应用程序; 应用程序运行(args); } } camel context.xml 我使用java com.exam
Application.java
公共类应用程序扩展org.apache.camel.spring.Main{
公共静态void main(字符串[]args)引发异常{
应用程序app=新应用程序();
实例=应用程序;
应用程序运行(args);
}
}
camel context.xml
我使用java com.example.Application-DinputFile=C:/absolute/path/to/watch
运行应用程序,一切正常:
…
FileEndpoint INFO Using default memory based idempotent repository with cache max size: 1000
InternalRouteStartupManager INFO Route: route1 started and consuming from: file://C:/absolute/path/to/watch
AbstractCamelContext INFO Total 1 routes, of which 1 are started
…
但是我想进行一些输入验证,使应用程序更易于使用,因为-D
可能会让非Java用户感到困惑。所以我更改了Application.java
:
公共类应用程序扩展org.apache.camel.spring.Main{
私有文件输入文件;
公共静态void main(字符串[]args)引发异常{
应用程序app=新应用程序();
实例=应用程序;
应用程序运行(args);
}
公共应用程序(){
addOption(新参数option(“i”、“inputFile”、“输入文件”、“inputFile”){
@凌驾
受保护的void doProcess(字符串arg、字符串参数、LinkedList remainingArgs){
File File=FileUtils.getFile(参数);
//一些业务验证
setInputFile(文件);
}
});
}
私有void setInputFile(文件inputFile){
this.inputFile=inputFile;
}
}
然后,我可以使用以下命令运行应用程序:java com.example.application-inputFile C:/absolute/path/to/watch
如何将我的inputFile
字段用于骆驼路线?在doProcess
方法中调用addProperty(字符串键、字符串值)
。然后可以通过{{key}}
符号访问它
MyApplication:
public final class MyApplication extends Main {
private MyApplication() {
super();
addCliOption("g", "greeting", "Greeting");
addCliOption("n", "name", "Who to greet");
}
public static void main(String[] args) throws Exception {
MyApplication app = new MyApplication();
app.configure().addRoutesBuilder(MyRouteBuilder.class);
app.run(args);
}
private void addCliOption(String abbrevation, String parameterName, String description) {
addOption(new ParameterOption(abbrevation, parameterName, description, parameterName) {
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
addProperty("console." + parameterName, parameter);
}
});
}
}
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() throws Exception {
from("quartz:foo")
.log("{{console.greeting}} {{console.name}}");
}
}
java org.apache.camel.example.MyApplication-问候Hello-姓名Morgan
我无法使它与SpringXML一起工作 因此,我将我的
camel context.xml
切换到RouteBuilder
。我感谢你的帮助。
23:10:25.862 [DefaultQuartzScheduler-MyCoolCamel_Worker-1] INFO route1 - Hello Morgan
23:10:26.832 [DefaultQuartzScheduler-MyCoolCamel_Worker-2] INFO route1 - Hello Morgan
23:10:27.829 [DefaultQuartzScheduler-MyCoolCamel_Worker-3] INFO route1 - Hello Morgan