在运行时从文件加载Apache Camel路由

2w3rbyxf  于 2022-11-07  发布在  Apache
关注(0)|答案(2)|浏览(265)

我试图在运行时从一个文件加载Apache Camel路由。
我的“route.yaml”文件如下所示。

- route:
    from: "timer:yaml?period=3s"
    steps:
      - set-body:
          simple: "Timer fired ${header.CamelTimerCounter} times"
      - to:
          uri: "log:yaml"

我发现过去可以通过以下方式加载一个路由xml文件,但现在却无法实现。

InputStream routesXml = new ByteArrayInputStream(routePropertyValue.toString().getBytes());
RoutesDefinition loadedRoutes = camelContext.loadRoutesDefinition(routesXml);
camelContext.addRouteDefinitions(loadedRoutes.getRoutes());

如何与目前的Apache Camel ?

disho6za

disho6za1#

我能够让它从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();
        }
    }
}
pftdvrlh

pftdvrlh2#

您也可以从类别路径载入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);
    }
  }

}

相关问题