简体   繁体   English

Apache Camel Java DSL 向正文添加换行符

[英]Apache Camel Java DSL add newline to body

So I have a netty4 socket route set up in Java DSL that looks like the following:所以我在 Java DSL 中设置了一个 netty4 套接字路由,如下所示:

@Override
public void configure() throws Exception {
    String dailyDataUri = "{{SOCKET.daily.file}}" + "&fileName=SocketData-${date:now:yyyyMMdd}.txt";
    from(socketLocation).routeId("thisRoute")
    .transform()
        .simple("${in.body}\n")
    .wireTap(dailyDataUri)
    .to(destination)
;

Where both the wireTap and the destination are sending their data to two separate files.窃听器和目的地都将它们的数据发送到两个单独的文件。 And the data collection in the destination file is separated by a \\n (line break)... or at least it should be.并且目标文件中的数据集合由\\n (换行符)分隔......或者至少应该如此。

When viewing the files created, the \\n is never added.查看创建的文件时,永远不会添加\\n

The equivalent idea in the Spring DSL worked before I switched to Java:在我切换到 Java 之前,Spring DSL 中的等效想法有效:

<transform>
    <simple>${in.body}\n</simple>
</transform>

After using that and opening the files created during the route, the lines of data that came in through the socket would be separated by a newline.在使用它并打开路由期间创建的文件之后,通过套接字进入的数据行将由换行符分隔。

What am I doing wrong in the Java DSL that doesn't allow the newline to be appended to the socket data as it comes in?我在 Java DSL 中做错了什么,不允许将换行符附加到传入的套接字数据中?

I feel like it's something obvious that I just don't see.我觉得这很明显,我只是看不到。

The data that is coming in is just a CSV-like line of text.传入的数据只是类似于 CSV 的文本行。

I found a solution, I'm never sure what can be translated almost word from word from Spring to Java.我找到了一个解决方案,我不确定从 Spring 到 Java 的几乎每个单词都可以翻译成什么。 Apparently the transform/simple combination has some issue where it will not work for me in Java DSL.显然,转换/简单组合存在一些问题,它在 Java DSL 中对我不起作用。

So a possible solution (there may be more solutions) is to do this:所以一个可能的解决方案(可能有更多的解决方案)是这样做的:

@Override
public void configure() throws Exception {
    String dailyDataUri = "{{SOCKET.daily.file}}" + "&fileName=SocketData-${date:now:yyyyMMdd}.txt";
    from(socketLocation).routeId("thisRoute")
    .transform(body().append("\n"))
    .wireTap(dailyDataUri)
    .to(destination)
;

Where instead of using the Simple language to manipulate the body, I just call on the body and append a String of \\n to it.我没有使用简单语言来操作正文,而是调用正文并将\\n字符串附加到它。 And that solves my issue.这解决了我的问题。

Update : Camel version 3.x and above File component provides features to append your desired character.更新:Camel 3.x 及更高版本的文件组件提供了附加所需字符的功能。

As you are writing file using file component (producer)当您使用文件组件(生产者)编写文件时

appendChars (producer) appendChars(生产者)

Used to append characters (text) after writing files.用于在写入文件后附加字符(文本)。 This can for example be used to add new lines or other separators when writing and appending new files or existing files.例如,这可用于在写入和附加新文件或现有文件时添加新行或其他分隔符。 To specify new-line (slash-n or slash-r) or tab (slash-t) characters then escape with an extra slash, eg slash-slash-n.要指定换行符(slash-n 或 slash-r)或制表符(slash-t)字符,然后用额外的斜杠转义,例如 slash-slash-n。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM