如何将价值从休息服务传递到骆驼路线?

时间:2018-10-30 13:46:22

标签: rest spring-boot apache-camel spring-camel

我公开了以下服务

restConfiguration().component("servlet").bindingMode(RestBindingMode.json);
    rest("/batchFile").consumes("application/json").post("/routeStart").type(BatchFileRouteConfig.class).to("startRouteProcessor");

根据休息服务的请求,我将在处理器中启动骆驼路线,如下所示

@Component("startRouteProcessor")
public class StartRouteProcessor implements Processor {


    public void process(Exchange exchange) throws Exception {
        BatchFileRouteConfig config = exchange.getIn().getBody(BatchFileRouteConfig.class);
        String routeId = config.getRouteId();
        String sourceLocation = config.getSourceLocation();
        exchange.getContext().startRoute(routeId);
    }
}

我需要将sourceLocation从上面的bean传递到下面的路线

@Component
public class FileReaderRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("file:sourceLocation")
       .log("File Reader Route route started");
    }

}

上面是示例代码。.请您帮助我将源位置 StartRouteProcessor 传递到 FileReaderRoute

1 个答案:

答案 0 :(得分:0)

这是不可能的,因为在您的示例中,FileReaderRoute在调用batchFile端点时已经启动。

您可以用略有不同的方式来做。

FileReaderRoute提取到direct。像这样:

@Component
public class FileReaderRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        from("direct:fileReaderCommon")
       .log("File Reader Route route started");
    }
}

然后您可以在运行时创建新路由:

@Component("startRouteProcessor")
public class StartRouteProcessor implements Processor {
    public void process(Exchange exchange) throws Exception {
        BatchFileRouteConfig config = exchange.getIn().getBody(BatchFileRouteConfig.class);

        exchange.getContext().addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("file:"+config.getSourceLocation())
                    .routeId(config.getRouteId())
                    .to("direct:fileReaderCommon");
            }
        });
    }
}

请不要忘记对输入进行足够的清理,因为您允许用户根据用户输入创建文件使用方。在您的方法中,存在路径遍历攻击的高风险。