Java 自动路由发现骆驼
在我的应用程序中,有一些xml文件存储在一个文件夹中,用于声明我的路由。我想在应用程序引导中上传我的所有路由,并将它们存储在我的camel上下文中。换句话说,我想自动发现存储在这些xml文件中的路由 下面是一个包含路由的文件示例Java 自动路由发现骆驼,java,xml,spring,apache-camel,Java,Xml,Spring,Apache Camel,在我的应用程序中,有一些xml文件存储在一个文件夹中,用于声明我的路由。我想在应用程序引导中上传我的所有路由,并将它们存储在我的camel上下文中。换句话说,我想自动发现存储在这些xml文件中的路由 下面是一个包含路由的文件示例 <?xml version="1.0" encoding="UTF-8"?> <routeContext id="myRoute" xmlns="http://camel.apache.org/schema/spring"> <ro
<?xml version="1.0" encoding="UTF-8"?>
<routeContext id="myRoute" xmlns="http://camel.apache.org/schema/spring">
<route>
<from uri="file:C:/LocalFTPServer" />
<log message="Got a file!" loggingLevel="INFO" loggerRef="myLogger" />
<choice>
<when>
<simple>${file:ext} == 'csv'</simple>
<log message="I'm going to email you!" loggingLevel="INFO"
loggerRef="myLogger" />
</when>
<otherwise>
<log message="File extension wrong." loggingLevel="WARN"
loggerRef="myLogger" />
</otherwise>
</choice>
</route>
</routeContext>
${file:ext}=='csv'
这是我的应用程序上下文
<?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">
<import resource="beans/beans.xml" />
<camelContext xmlns="http://camel.apache.org/schema/spring">
</camelContext>
</beans>
如果要在某些事件上添加路由,只需使用
上下文。addRoutedDefinitions
方法
例如:
public class ContextStartEventListener extends EventNotifierSupport implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Override
public void notify(EventObject event) throws Exception {
if (event instanceof CamelContextStartedEvent) {
try {
CamelContextStartedEvent startedEvent = (CamelContextStartedEvent) event;
DefaultCamelContext context=(DefaultCamelContext)startedEvent.getContext();
Resource[] xmlResources=applicationContext.getResources("classpath*:net/**/route.xml");
for (int i=0;i<xmlResources.length;i++) {
InputStream is = xmlResources[i].getInputStream();
RoutesDefinition routes = context.loadRoutesDefinition(is);
context.addRouteDefinitions(routes.getRoutes());
}
} catch (Throwable ex) {
// do something on error
}
}
}
...
公共类ContextStarteEventListener扩展事件通知支持实现ApplicationContextAware{
私有应用程序上下文应用程序上下文;
@凌驾
public void setApplicationContext(ApplicationContext ApplicationContext){
this.applicationContext=applicationContext;
}
@凌驾
public void notify(EventObject事件)引发异常{
if(CamelContextStartedEvent的事件实例){
试一试{
CamelContextStartedEvent startedEvent=(CamelContextStartedEvent)事件;
DefaultCamelContext=(DefaultCamelContext)startedEvent.getContext();
Resource[]xmlResources=applicationContext.getResources(“classpath*:net/**/route.xml”);
对于(int i=0;i关于
的内容是什么?通过这种方式可以导入文件,但不会在camel上下文中“注册”其中包含的路由。我正在寻找一些方法来实现这一点。