emad885
emad885

Reputation: 75

Can camel generate File from java object

I need to generate a large file which is going to be done in Java, so a method is hit and the service goes off to the repository returns a list of type X as Java object. I then need to place this list in a file and send this off to an ftp server.

I know I can put files on ftp servers using camel, but wanted to know if it possible for camel to generate the file from the Java object and then place on the ftp server?

My code would look like this:

 List<ObjectX> xList = someRepo.getListOfx();

So I need to write xList to a file and place on the ftp server.

Upvotes: 1

Views: 1959

Answers (1)

Cy&#228;egha
Cy&#228;egha

Reputation: 4251

Generally speaking, to convert your POJO messages to/from a text (or binary) based representation, you can use a Camel Dataformat. In your route, you will use the marshall and unmarshall keywords to perform the conversion.

There are several Camel dataformats available to marshall/unmarshal CSV, including the CSV dataformat or the Bindy dataformat (but there are a few others listed on the Dataformat page, under the "Flat data structure marshalling" header). One advantage of Bindy is that it can also support other unstructured formats (such as fixed width records)

Also note :

  • With Bindy, you will have to add annotations to your model class (ObjectX)
  • With CSV, you will have to convert you model objects (of type ObjectX) to Maps (or register an appropriate TypeConverter with Camel to do this conversion automatically)
  • If you check the other available dataformats, they may have different requirements too

Here is a very simple example with bindy:

package mypackage;

@CsvRecord(separator = ",")
public Class MyPojo {
    @DataField(pos = 1) // first column in the CSV file
    private int foo;
    @DataField(pos = 2) // second column in the CSV file
    private String bar;
    // Plus constructors, accessors, etc.
}

// In the RouteBuilder :
DataFormat bindy = new BindyCsvDataFormat("mypackage");

from("...") // Message received as a List<MyPojo>
    .marshal(bindy)
    .to("ftp:...");

Upvotes: 2

Related Questions