0
votes

I need to build a csv file based on incoming messages. I do this by appending to the file with:

.toD("file://" + OUTPUT_PATH + "?FileName=${exchangeProperty.OUTPUT_FILENAME}" + "&FileExist=Append")

While this works fine I've run into one problem. I also need to include a header row in the CSV file. Right now I'm marshalling into CSV format with .mashall().csv() but that omits the header.

While I can create a CSV format with header with:

CsvDataFormat csvFormatWithHeader = new CsvDataFormat();
csvFormatWithHeader.setHeader(Arrays.asList(new String[] { "A", "B", "C", "D" }.clone()));

.marshall(csvFormatWithHeader)

That will add the header row for each row I add. So what I want to achieve is to add the header only when a new file is created.

I've been trying two approaches but haven't gotten any to work:

  1. Check if the file exists in the route and apply the csv format accordingly
  2. Set the marshall dataformat with a bean or method

As a final option I could add the header when the file is closed but that feels inefficient as I don't know how big that file might become.

How can I achieve either of these approaches with Apache Camel 2.23.2.

1

1 Answers

1
votes

Ok, ultimately using a header I was able to get option 1 to work. This is what my route looks like now:

.setProperty("OUTPUT_FILENAME",method(this, "determineOutputFilename()"))
.setHeader("fileExists", method(this, "outputFileExists"))
.choice()
  .when(header("fileExists"))
    .marshal().csv()
  .endChoice()
  .otherwise()
    .marshal(csvFormatWithHeader)
  .endChoice()
.end()

The file check logic is (implemented within the route class):

public boolean outputFileExists(@ExchangeProperty("OUTPUT_FILENAME") String fileName){
    boolean fileExists = new File(PROCESSING_PATH + "/" + fileName).exists();
    return fileExists;
}