在运行时从文件加载 Apache Camel 路由
Load a Apache Camel route at runtime from a file
我正在尝试在运行时从文件加载 Apache Camel 路由。这意味着
我的“route.yaml”文件如下
- route:
from: "timer:yaml?period=3s"
steps:
- set-body:
simple: "Timer fired ${header.CamelTimerCounter} times"
- to:
uri: "log:yaml"
我看到过去可以执行以下操作来加载路由 xml 文件,但现在不能这样做了。例如,我在 Unable to start Camel Routes which are loaded from external XML
处看到 loadRoutesDefinition
InputStream routesXml = new ByteArrayInputStream(routePropertyValue.toString().getBytes());
RoutesDefinition loadedRoutes = camelContext.loadRoutesDefinition(routesXml);
camelContext.addRouteDefinitions(loadedRoutes.getRoutes());
如何使用当前的 Apache Camel 来实现?
我能够通过 yaml
的字符串将其加载到 RouteBuilder 中
package org.acme;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;
import org.apache.camel.support.ResourceHelper;
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() {
CamelContext context = getContext();
String myRoute = "- route:\n" +
" from: \"timer:yaml?period=3s\"\n" +
" steps:\n" +
" - set-body:\n" +
" simple: \"Timer fired ${header.CamelTimerCounter} times\"\n" +
" - to:\n" +
" uri: \"log:yaml\"\n" +
"\n";
try {
ExtendedCamelContext extendedCamelContext = context.adapt(ExtendedCamelContext.class);
RoutesLoader loader = extendedCamelContext.getRoutesLoader();
Resource resource = ResourceHelper.fromString("any.yaml", myRoute);
loader.loadRoutes(resource);
} catch (Exception e) {
e.printStackTrace();
}
}
}
您还可以从类路径加载 XML 文件。
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.impl.engine.DefaultResourceResolvers;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class RouteLoader implements CamelContextAware {
private CamelContext camelContext;
@Override
public CamelContext getCamelContext() {
return camelContext;
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
@PostConstruct
void loadRoutes() {
loadRoute("camel-routes.xml");
}
private void loadRoute(String name) {
ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
RoutesLoader loader = extendedCamelContext.getRoutesLoader();
try (DefaultResourceResolvers.ClasspathResolver resolver = new DefaultResourceResolvers.ClasspathResolver()) {
resolver.setCamelContext(camelContext);
Resource resource = resolver.resolve("classpath:" + name);
loader.loadRoutes(resource);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
我正在尝试在运行时从文件加载 Apache Camel 路由。这意味着
我的“route.yaml”文件如下
- route:
from: "timer:yaml?period=3s"
steps:
- set-body:
simple: "Timer fired ${header.CamelTimerCounter} times"
- to:
uri: "log:yaml"
我看到过去可以执行以下操作来加载路由 xml 文件,但现在不能这样做了。例如,我在 Unable to start Camel Routes which are loaded from external XML
处看到 loadRoutesDefinitionInputStream routesXml = new ByteArrayInputStream(routePropertyValue.toString().getBytes());
RoutesDefinition loadedRoutes = camelContext.loadRoutesDefinition(routesXml);
camelContext.addRouteDefinitions(loadedRoutes.getRoutes());
如何使用当前的 Apache Camel 来实现?
我能够通过 yaml
的字符串将其加载到 RouteBuilder 中package org.acme;
import org.apache.camel.CamelContext;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;
import org.apache.camel.support.ResourceHelper;
public class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() {
CamelContext context = getContext();
String myRoute = "- route:\n" +
" from: \"timer:yaml?period=3s\"\n" +
" steps:\n" +
" - set-body:\n" +
" simple: \"Timer fired ${header.CamelTimerCounter} times\"\n" +
" - to:\n" +
" uri: \"log:yaml\"\n" +
"\n";
try {
ExtendedCamelContext extendedCamelContext = context.adapt(ExtendedCamelContext.class);
RoutesLoader loader = extendedCamelContext.getRoutesLoader();
Resource resource = ResourceHelper.fromString("any.yaml", myRoute);
loader.loadRoutes(resource);
} catch (Exception e) {
e.printStackTrace();
}
}
}
您还可以从类路径加载 XML 文件。
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.impl.engine.DefaultResourceResolvers;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.RoutesLoader;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class RouteLoader implements CamelContextAware {
private CamelContext camelContext;
@Override
public CamelContext getCamelContext() {
return camelContext;
}
@Override
public void setCamelContext(CamelContext camelContext) {
this.camelContext = camelContext;
}
@PostConstruct
void loadRoutes() {
loadRoute("camel-routes.xml");
}
private void loadRoute(String name) {
ExtendedCamelContext extendedCamelContext = camelContext.adapt(ExtendedCamelContext.class);
RoutesLoader loader = extendedCamelContext.getRoutesLoader();
try (DefaultResourceResolvers.ClasspathResolver resolver = new DefaultResourceResolvers.ClasspathResolver()) {
resolver.setCamelContext(camelContext);
Resource resource = resolver.resolve("classpath:" + name);
loader.loadRoutes(resource);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}