Possible to initialize a bindy (Apache Camel DataFormat - FixedLength and use it in the same route

932 Views Asked by At

My input file consists of several type of FixedLengthRecord, so I have lots of FixedLengthDataFormat to unmarshal each post.

  1. I split the body per row
  2. for the first I should realize which DataFormat I should use, and create an object
  3. Then unmarshal

Something like this one:

from(myURI)
    .split().tokenize("\n")
        .process(initializeMyBindyDataFormat)
        .unmarshal(bindy)
    .end();

But my problem is, I get NPE for that bindy object when I initilize it via a process. But if I create a bindy object before my route definition (before from) it will be work fine. My bindy object is depended on body and I cannot initialize it before route definition. Actually Apache Camel process initialization of bindy object before starting the route

2

There are 2 best solutions below

0
On BEST ANSWER

The answer is using .inout Since I want to have unmarshalling in another route, a simple example should be as below:

from(myURI)
    .split().tokenize("\n")
        .inout("direct:unmarshalSpecificRow")
    .end();

from(direct:unmarshalSpecificRow")
    .choice()
        .when(firstPredicate)
           unmarshal(new BindyFixedLengthDataFormat(package1)
        .when(secondPredicate)
           unmarshal(new BindyFixedLengthDataFormat(package1)
        .when(thirdPredicate)
           unmarshal(new BindyFixedLengthDataFormat(package1)
        .otherwise()
           .throwException(new IllegalArgumentException("Unrecognised post")
    .end();

Thanks jakub-korab for his help.

1
On

In this case I think it is better to divide your processing in two seps.

  1. A main route which receives the different data. Here you define the predicate rules that determine what kind of body it is. Check the start of the body, or something that determines it is of this type or that. Add a choice() when() and based on which predicate gets set to true set it to separate route.

  2. In the secondary routes add the specific bindy format and do your marshal/unmarshal work.

An example from the the documentation:

Predicate isWidget = header("type").isEqualTo("widget");
from("jms:queue:order")
   .choice()
      .when(isWidget).to("bean:widgetOrder")
      .when(isWombat).to("bean:wombatOrder")
   .otherwise()
      .to("bean:miscOrder")
   .end();  

http://camel.apache.org/predicate.html