Jsmith
Jsmith

Reputation: 360

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:

@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.

When viewing the files created, the \n is never added.

The equivalent idea in the Spring DSL worked before I switched to Java:

<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?

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.

Upvotes: 3

Views: 7165

Answers (2)

Alpesh Gediya
Alpesh Gediya

Reputation: 3794

Update : Camel version 3.x and above File component provides features to append your desired character.

As you are writing file using file component (producer)

appendChars (producer)

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.

Upvotes: 0

Jsmith
Jsmith

Reputation: 360

I found a solution, I'm never sure what can be translated almost word from word from Spring to Java. Apparently the transform/simple combination has some issue where it will not work for me in 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. And that solves my issue.

Upvotes: 4

Related Questions