你如何编组多个BindyFixedLengthDataFormat模型(camel-bindy-2.16)

时间:2016-03-02 20:13:39

标签: model apache-camel marshalling bindy

我目前正在尝试从 camel-bindy-2.12.1 升级到 camel-bindy-2.16.2 ,并在尝试应用时遇到问题由多个类组成的模型,产生一个文本文件。

我在一个包(com.sample.package)中有许多类,我可以使用以下代码(Camel Spring DSL)来编组:

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

然后在编组时引用bean:

<marshal ref="bindyFixedLengthDataformat" />

这个bean调用会将包中的所有类应用于被封送的数据,从而产生一个文件。

camel-bindy-2.12.1 效果很好,但上面的构造函数不再适用于 camel-bindy-2.16.2

我无法找到任何可以与已删除的构造函数实现相同功能的示例。

有没有人遇到过这种情况?如果是这样,我们将非常感谢任何建议/示例。

谢谢

1 个答案:

答案 0 :(得分:0)

这是一篇旧文章,但是我确实找到了解决方案。这个想法是,您必须为每个类创建一个单独的绑定格式器...然后将每个格式器应用于骆驼消息的正文:

/* 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) */