Main
来启动我的 Camel 应用程序。我需要我的应用程序读取命令行参数来设置一些参数。所以,我不能使用属性文件。
目前,我可以通过 JVM 系统属性传递参数,而且效果很好:
Application.java
public class Application extends org.apache.camel.spring.Main {
public static void main(String[] args) throws Exception {
Application app = new Application();
instance = app;
app.run(args);
}
}
camel-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
<bean id="shutdownBean" class="com.example.ShutdownBean" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:{{inputFile}}?noop=true"/>
<to uri="bean:shutdownBean" />
</route>
</camelContext>
</beans>
我用
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
:
public class Application extends org.apache.camel.spring.Main {
private File inputFile;
public static void main(String[] args) throws Exception {
Application app = new Application();
instance = app;
app.run(args);
}
public Application() {
addOption(new ParameterOption("i", "inputFile", "The input file", "inputFile") {
@Override
protected void doProcess(String arg, String parameter, LinkedList<String> remainingArgs) {
File file = FileUtils.getFile(parameter);
// some business validation
setInputFile(file);
}
});
}
private void setInputFile(File inputFile) {
this.inputFile = inputFile;
}
}
然后,我可以使用以下命令来运行应用程序:
java com.example.Application -inputFile C:/absolute/path/to/watch
如何将我的
inputFile
字段用于我的 Camel 路线?
在
addProperty(String key, String value)
方法中调用 doProcess
。然后可以通过 {{key}}
符号访问它。
我的申请:
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 -greeting Hello -name Morgan
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
我使用 name=zxc 作为选项运行相同的应用程序,但使用 Camel 4.0 我得到了
Unknown option: name=zxc
未知选项:zxc
Apache Camel Runner 采用以下选项
-h 或 -help = 显示帮助屏幕 -r 或 -routers = 设置路由器构建器类>将在启动camel上下文时加载 -d 或 -duration = 设置应用程序在终止之前运行的持续时间(秒)。 -dm 或 -durationMaxMessages = 设置应用程序在终止之前将处理的最大消息数的持续时间。 -di 或 -durationIdle = 设置空闲持续时间(秒)>应用程序在终止之前可以空闲的持续时间。 -t 或 -trace = 启用跟踪 -ts 或 -traceStandby = 启用跟踪备用状态 -e 或 -exitcode = 设置退出代码(如果达到持续时间) -pl 或 -propertiesLocation = 设置加载属性的位置,例如从类路径或文件系统。 -ac 或 -applicationContext = 设置基于 >spring ApplicationContext 的类路径 -fa 或 -fileApplicationContext = 设置基于>文件系统的 spring ApplicationContext -ac 或 -applicationContext = 设置基于 >spring ApplicationContext 的类路径 -fa 或 -fileApplicationContext = 设置>基于文件系统的 spring ApplicationContext