1
votes

Scenario:

  • CSV file is sent to my endpoint, Pojo transforms the data for java and message sent to one of my route lets say ("direct:consume") route, then a processor processes the file manipulating the message and creating a new output.

Issue:

  • file contains only one line the code breaks
  • file contains multiple lines the code works

Tried:

  • tried to find a way to determine the amount of record coming in the exchange.getIn().getBody()
  • read on stackoverflow
  • read camel documentation about exchange
  • check java codes for Object/Objects to List conversion without knowing record amount

Code:

    public void process(Exchange exchange) throws Exception {
    List<Cars> output = new ArrayList<Cars>();
    **List<Wehicle> rows = (List<Wehicle>) exchange.getIn().getBody(); <-- Fails**

    for (Wehicle row: rows) {
        output.add(new Cars(row));
    }

    exchange.getIn().setBody(output);
    exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
}

Wehicle

...
@CsvRecord(separator = ",", skipFirstLine = true, crlf = "UNIX")
public class Wehicle {
    @DataField(pos = 1)
    public String CouponCode;

    @DataField(pos = 2)
    public String Price;
}
...

Cars

@CsvRecord(separator = ",", crlf = "UNIX", generateHeaderColumns = true)
public class Cars {
    @DataField(pos = 1, columnName = "CouponCode")
    private String CouponCode;

    @DataField(pos = 2, columnName = "Price")
    private String Price;
    
    public Cars(Wehicle origin) {
        this.CouponCode = Utilities.addQuotesToString(origin.CouponCode);
        this.Price = origin.Price;
    }
}

Input:

"CouponCode","Price"
"ASD/785", 1900000
"BWM/758", 2000000

Question:

  • How to create dynamicall a List regardless if i get one object or multiple objects? -- exchange.getIn().getBody() returns object
  • How to check the amount of records from camel exchange message ? -- exchange.getIn().getBody() no size/length method
  • Any other way of doing this?

Haven't used java for a long time, plus quiet new to camel.

1

1 Answers

1
votes

After re checking the official documentation it seems the following changes are solving the issue.

Code:

    public void process(Exchange exchange) throws Exception {
    List<Cars> output = new ArrayList<Cars>();
    List records = exchange.getIn().getBody(List.class);
    (List<Wehicle>) rows = (List<Wehicle>) records;

    for (Wehicle row: rows) {
        output.add(new Cars(row));
    }

    exchange.getIn().setBody(output);
    exchange.getIn().setHeader("CamelOverruleFileName", "CarEntries.csv");
   }