针对 Java 开发者的 Apache Camel 入门指南(2)


public class TimerMain {
static Logger LOG = LoggerFactory.getLogger(TimerMain.class);
public static void main(String[] args) throws Exception {
new TimerMain().run();
}
void run() throws Exception {
final CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(createRouteBuilder());
camelContext.setTracing(true);
camelContext.start();


Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
try {
camelContext.stop();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
});


waitForStop();
}
RouteBuilder createRouteBuilder() {
return new TimerRouteBuilder();
}
void waitForStop() {
while (true) {
try {
Thread.sleep(Long.MAX_VALUE);
} catch (InterruptedException e) {
break;
}
}
}
}

可以看到,我们在createRouteBuilder()方法中重用了已有的TimerRouteBuilder类。现在我们的主类对在什么时候创建、启动、停止CamelContext有了完全的控制。context(camelContext)对象允许你全局性地控制如何配置Camel,而不是在Route级。它的JavaDoc链接给出了所有setter方法,你可以研究下它都可以做些什么。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/37398be82d04a8b269f6a647cd5750de.html