Model 如何使用多个BindyFixedLengthDataFormat模型(camel-bindy-2.16)封送数据

Model 如何使用多个BindyFixedLengthDataFormat模型(camel-bindy-2.16)封送数据,model,apache-camel,marshalling,bindy,Model,Apache Camel,Marshalling,Bindy,我目前正试图从camel-bindy-2.12.1升级到camel-bindy-2.16.2,并且在尝试将由多个类组成的模型应用于一个数据集时遇到了一个问题,导致了一个文本文件 我在一个包(com.sample.package)中有许多类,可以使用以下代码(Camel-Spring DSL)进行封送处理: 然后在封送处理时引用bean: <marshal ref="bindyFixedLengthDataformat" /> 这个bean调用将把包中的所有类应用于被封送的数

我目前正试图从camel-bindy-2.12.1升级到camel-bindy-2.16.2,并且在尝试将由多个类组成的模型应用于一个数据集时遇到了一个问题,导致了一个文本文件

我在一个包(com.sample.package)中有许多类,可以使用以下代码(Camel-Spring DSL)进行封送处理:


然后在封送处理时引用bean:

<marshal ref="bindyFixedLengthDataformat" />

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

它在camel-bindy-2.12.1中工作得非常好,但是上面的构造函数在camel-bindy-2.16.2中不再可用

我一直无法找到任何例子,将实现相同的功能与删除的构造函数

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


谢谢

这是一篇老文章,但我确实想出了一个解决办法。其思想是,您必须为每个类创建一个单独的bindy格式化程序……然后将每个格式化程序应用于消息体:

/* 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) */
/*开始Spring引导Bean配置*/
导入org.apache.camel.dataformat.bindy.fixed.bindyfixedlingthdataformat;
@自动连线
CamelContext和CamelContext;
@Bean(name=“bindyFixedLengthDataformat\u Part1”)
公共BindyFixedLengthDataFormat BindyFixedLengthDataFormat_第1部分(){
BindyFixedLengthDataFormat bindy=新的BindyFixedLengthDataFormat(com.xyz.Part1.class);
bindy.setCamelContext(camelContext);
返回bindy;
}
@Bean(name=“bindyFixedLengthDataformat\u Part2”)
公共BindyFixedLengthDataFormat BindyFixedLengthDataFormat_第2部分(){
BindyFixedLengthDataFormat bindy=新的BindyFixedLengthDataFormat(com.xyz.Part2.class);
bindy.setCamelContext(camelContext);
返回bindy;
}
...
@Bean(name=“bindyFixedLengthDataformat\u PartN”)
公共BindyFixedLengthDataFormat BindyFixedLengthDataFormat_PartN(){
BindyFixedLengthDataFormat bindy=新的BindyFixedLengthDataFormat(com.xyz.PartN.class);
bindy.setCamelContext(camelContext);
返回bindy;
}
@Bean(name=“bindyConverter”)
bindyConverter直到bindyConverter(){
BindyConverterUtil bindyConverter=new BindyConverterUtil();//这是一个自定义bean,包含您需要的所有转换器。实现方法进程(Exchange)
bindyConverter.setBindyFixedLengthDataformat_Part1(bindyFixedLengthDataformat_Part1());
bindyConverter.setBindyFixedLengthDataformat_Part2(bindyFixedLengthDataformat_Part2());
...
bindyConverter.setBindyFixedLengthDataformat_Part5(bindyFixedLengthDataformat_PartN());
返回bindyConverter;
}
@Bean(name=“createObjectArrayListWithDataClasses”)
具有不同数据类的公共列表createObjectArrayListWithDataClasses(){
ListWithDifferentDataClasses lineValues=new ListWithDifferentDataClasses();//这是一个自定义bean,它基本上创建一个包含要封送的数据的类的数组列表
//实现方法流程(Exchange)。
//使用以下命令添加类:exchange.getIn().setBody();
返回行值;
}
/*结束Spring引导Bean配置*/
/*Bindy转换器Util*/
包com.xyz;
导入java.io.OutputStream;
导入java.io.ByteArrayOutputStream;
导入java.util.ArrayList;
导入java.util.HashMap;
导入org.apache.camel.Exchange;
导入org.apache.camel.Processor;
导入org.apache.camel.dataformat.bindy.fixed.bindyfixedlingthdataformat;
公共类BindyConverterUtil实现处理器{
/*(非Javadoc)
*@see org.apache.camel.Processor#process(org.apache.camel.Exchange)
*/
专用BindyFixedLengthDataFormat BindyFixedLengthDataFormat_第1部分;
专用BindyFixedLengthDataFormat BindyFixedLengthDataFormat_第2部分;
专用BindyFixedLengthDataFormat BindyFixedLengthDataFormat_PartN;
公共无效设置BindyFixedLengthDataFormat_第1部分(BindyFixedLengthDataFormat bindyFormatter){
this.bindyFixedLengthDataformat_Part1=bindyFormatter;
}
公共无效设置BindyFixedLengthDataFormat_第2部分(BindyFixedLengthDataFormat bindyFormatter){
this.bindyFixedLengthDataformat_Part2=bindyFormatter;
}
公共无效设置BindyFixedLengthDataFormat_PartN(BindyFixedLengthDataFormat bindyFormatter){
this.bindyFixedLengthDataformat\u PartN=bindyFormatter;
}
@SuppressWarnings({“unchecked”,“rawtypes”})
@凌驾
公共作废进程(Exchange)引发异常{
//TODO自动生成的方法存根
ArrayList contents=(ArrayList)exchange.getIn().getBody();//来自CreateObjectArrayList WithDataClasses bean。
OutputStream OutputStream=新建ByteArrayOutputStream();
for(HashMap项:目录){
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(交换,item.get(PartN.class.getName()),outputStream);
}
否则{
抛出新异常(“Bindy Converter-无法封送记录。无法识别格式”);
}
}
exchange.getIn().setBody(outputStream);
}
}
/*End Bindy转换器Util*/
/*骆驼路线(XML)*/
/*结束骆驼肉
/* 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) */