3

I have camel route to read a file as below:

@Component
public class MessageRoute extends RouteBuilder {

    public static final String ROUTE_ID = "message.route";
    public static final String ROUTE_URI = "{{message.route.uri}}";

    @Override
    public void configure() throws Exception {

        from("file:://target/test.txt")
                .convertBodyTo(String.class)
                .process(exchange -> {
                    log.info("Body {}", exchange.getIn().getBody(String.class));
                });
    }
}

Now, the question is how to make a call to this route? My end goal is to call from producerTemplate and process the file content.

I couldn't find anything about this on Camel Docs

Also, I tried to use pollEnrich as mentioned in this answer, but while debugging, execution doesn't get there at all to aggregator.

I would be million dollars thankful for Any solution, suggestion or idea.

4

3 回答 3

3

我不得不做类似的事情。以下适用于骆驼 2.18+ -

rest("/load")
.get("/sampleFile")
.to("direct:readFromSampleFile")
    ;
from("direct:readFromSampleFile")
.pollEnrich("file://c:/folder?fileName=sample.txt&noop=true&idempotent=false") // idempotent to allow re-read, no-op to keep the file untouched
.convertBodyTo(String.class)
.log("Read ${body}")
.unmarshal().json(JsonLibrary.Jackson)
.setHeader("Content-Type").constant("application/json")
.log("Returned ${body}")
;
于 2020-06-11T08:05:27.653 回答
1

我实际上是试图从另一条路线调用这条路线或在一条路线中级联它。我发现这个工作:

public static final String FILE_ROUTE_ID = "file.route";
public static final String FILE_ROUTE_URI = "{{file.route.uri}}";

@Override
public void configure() throws Exception {

    from(FILE_ROUTE_URI)
            .routeId(FILE_ROUTE_ID)
            .log(LoggingLevel.INFO, "Header {}", String.valueOf(simple("${header.purpose}")))
            .from("file:apache-camel-spring-boot?fileName=printing.key&noop=true")
            .convertBodyTo(String.class)
            .process(exchange -> {
                log.info("Processing file . . .");
                KeyBody keyBody = new KeyBody(exchange.getIn().getBody(String.class));
                exchange.getIn().setBody(keyBody);
            });
}

谢谢大家关注这个!!干杯!

于 2018-06-29T21:13:19.070 回答
0

你到底想测试什么?文件组件已经过 Camel 测试。为了测试您的路线中涉及的处理器和bean,您基本上不需要文件组件,因此用fromdirect:start通过建议您的路线替换部分可能是推荐的方式。

如果您坚持测试文件组件,它适用于文件或目录,您应该将测试文件写入测试中的测试目录,然后启动您的路由并查看文件是否被正确使用和处理。JUnit 提供了一个TemporaryFolder可以帮助您创建和清理测试目录的工具。您可以查看我对类似问题的回答,了解如何使用 Camel 完成此操作。

于 2018-06-29T21:09:25.227 回答