2

I am currently trying to upgrade from camel-bindy-2.12.1 to camel-bindy-2.16.2 and am running into a problem when trying to apply a model consisting of multiple classes to a data set resulting in a single text file.

I had a number of classes in a package (com.sample.package) that I could use for marshaling by using the following code (Camel Spring DSL):

<bean id="bindyFixedLengthDataformat"  class="org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat">
    <constructor-arg value="com.sample.package" />
</bean>

and then reference the bean when marshaling:

<marshal ref="bindyFixedLengthDataformat" />

This bean call would apply all the classes in the package to the data being marshaled resulting in a single file.

It worked great with camel-bindy-2.12.1 but the constructor above is no longer available with camel-bindy-2.16.2.

I have been unable to find any examples that would achieve the same functionality with the removed constructor.

Has anyone come across this situation? If so, any suggestions/examples would be greatly appreciated.

Thank you

user1803311
  • 101
  • 8

1 Answers1

1

This is an old post, but I did figure out a solution. The idea is that you have to create a separate bindy formatter for each class...then apply each of the formatters to the body of the camel message:

/* BEGIN Spring Boot Bean Config */

import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat;

@Autowired
CamelContext camelContext;

@Bean (name="bindyFixedLengthDataformat_Part1")
public BindyFixedLengthDataFormat bindyFixedLengthDataformat_Part1 () {

    BindyFixedLengthDataFormat bindy = new BindyFixedLengthDataFormat (com.xyz.Part1.class);

    bindy.setCamelContext(camelContext);

    return bindy;
}

@Bean (name="bindyFixedLengthDataformat_Part2")
public BindyFixedLengthDataFormat bindyFixedLengthDataformat_Part2 () {

    BindyFixedLengthDataFormat bindy = new BindyFixedLengthDataFormat (com.xyz.Part2.class);

    bindy.setCamelContext(camelContext);

    return bindy;
}
...
@Bean (name="bindyFixedLengthDataformat_PartN")
public BindyFixedLengthDataFormat bindyFixedLengthDataformat_PartN () {

    BindyFixedLengthDataFormat bindy = new BindyFixedLengthDataFormat (com.xyz.PartN.class);

    bindy.setCamelContext(camelContext);

    return bindy;
}

@Bean (name="bindyConverter")
BindyConverterUtil bindyConverter () {

    BindyConverterUtil bindyConverter = new BindyConverterUtil () ;             //This is a custom bean containing all the converters you need. Implements method process (Exchange)

    bindyConverter.setBindyFixedLengthDataformat_Part1(bindyFixedLengthDataformat_Part1());
    bindyConverter.setBindyFixedLengthDataformat_Part2(bindyFixedLengthDataformat_Part2());

...

bindyConverter.setBindyFixedLengthDataformat_Part5(bindyFixedLengthDataformat_PartN());

    return bindyConverter;
}

@Bean (name="createObjectArrayListWithDataClasses")
public ListWithDifferentDataClasses createObjectArrayListWithDataClasses () {

    ListWithDifferentDataClasses lineValues = new ListWithDifferentDataClasses();           // This is a custom bean that basically creates an array list of classes containing the data you want to marshal 
                                                                                                // Implements method process(Exchange). 
                                                                                                // Adds classes using: exchange.getIn().setBody(<ArrayList of classes>);

    return lineValues;
}

/* END Spring Boot Bean Config */ 

/* Bindy Converter Util */

package com.xyz;

import java.io.OutputStream;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat;


public class BindyConverterUtil implements Processor {

    /* (non-Javadoc)
     * @see org.apache.camel.Processor#process(org.apache.camel.Exchange)
     */
    private BindyFixedLengthDataFormat bindyFixedLengthDataformat_Part1;
    private BindyFixedLengthDataFormat bindyFixedLengthDataformat_Part2;

    private BindyFixedLengthDataFormat bindyFixedLengthDataformat_PartN;

    public void setBindyFixedLengthDataformat_Part1 (BindyFixedLengthDataFormat bindyFormatter) {

        this.bindyFixedLengthDataformat_Part1 = bindyFormatter;
    }

    public void setBindyFixedLengthDataformat_Part2 (BindyFixedLengthDataFormat bindyFormatter) {

        this.bindyFixedLengthDataformat_Part2 = bindyFormatter;
    }

    public void setBindyFixedLengthDataformat_PartN (BindyFixedLengthDataFormat bindyFormatter) {

        this.bindyFixedLengthDataformat_PartN = bindyFormatter;
    }

    @SuppressWarnings({ "unchecked", "rawtypes" })
    @Override
    public void process(Exchange exchange) throws Exception {
        // TODO Auto-generated method stub

        ArrayList <HashMap> contents = (ArrayList <HashMap>) exchange.getIn().getBody();        //From the createObjectArrayListWithDataClasses bean.

        OutputStream outputStream = new ByteArrayOutputStream();

        for (HashMap item: contents) {

            if ( null != item.get(Part1.class.getName()) ) {

                bindyFixedLengthDataformat_Part1.marshal(exchange, item.get(Part1.class.getName()), outputStream);
            }
            else if ( null != item.get(Part2.class.getName()) ) {

                bindyFixedLengthDataformat_Part2.marshal(exchange, item.get(Part2.class.getName()), outputStream);
            }
...
            else if ( null != item.get(PartN.class.getName()) ) {

                bindyFixedLengthDataformat_PartN.marshal(exchange, item.get(PartN.class.getName()), outputStream);
            }
            else {
                throw new Exception ("Bindy Converter - Cannot marshal record. Format is not recognized. ");
            }
        }

        exchange.getIn().setBody(outputStream);
    }
}

/* End Bindy Converter Util */


/* Camel Route (XML) */

<route id="xyxz" >
    <from uri="timer...or whatever" />

    <to uri="createObjectArrayListWithDataClasses" />

    <to uri="bindyConverter" />

    <to uri="file://somepath/?fileName=outputfileName_${date:now:yyyyMMddhhmmss}.txt" />
</route>

/* End Camel Route (XML) */
user1803311
  • 101
  • 8