Camel 从数据对象编组导致空文件

pdsfdshx  于 9个月前  发布在  Apache
关注(0)|答案(1)|浏览(105)

在我们的一个遗留应用程序中,我们将创建包含来自SQL表的数据的数据对象,然后我们将使用XML DSL中的Apache Camel路由和BeanIO格式化库对它们进行编组。这一招很管用。在我们的新应用程序中,我试图模拟相同的行为,但使用Java DSL路由。我使用Apache Camel 4.0.0、Spring 4.0.0和BeanIO 3.4.0。
路线看起来像这样:

BeanIODataFormat dataFormat = new BeanIODataFormat("path/beanioFormats/beanio_format.xml", "FormatSourceName");

from("{{cron.process}}")
        .log(LoggingLevel.INFO, "Creating File")
        .transform().method("fileGeneratorClass", "generatorMethod")
        .to("direct:marshalFile")
        .end();

from("direct:marshalFile")
        .marshal(dataFormat)
        .to("file:{{output.file.path}}")
        .end();

而beanio格式文件看起来非常标准,与我们之前的文件类似,如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">

    <stream name="FormatSourceName" format="fixedlength">

        <group name="fileWrapperObject" class="path.to.wrapper.class">
            <record name="fileHeader" class="path.to.header.class" minOccurs="1" maxOccurs="1">
                <field name="fileRecordIdentifier" rid="true" length="3" required="true" literal="000"/>
            </record>
    ... etc

最后,将数据对象设置到交换中的generatorMethod如下:

public void generatorMethod(Exchange exchange) {

        FileWrapperObject fileWrapperObject = new fileWrapperObject();

        // fill it with data

        LOG.info("Wrapper object created");

        exchange.getIn().setBody(wrapper);

        String fileName = "fileName";
        exchange.getIn().setHeader(Exchange.FILE_NAME, fileName);
    }

我确信数据对象不为空,它的任何字段也不为空,但生成的文件总是空的。有什么线索能告诉我哪里做错了吗?我的直觉告诉我,这是我在routes文件中编写编组的方式,但我不确定从哪里开始,因为BeanIO文档相当缺乏。
我在基于XML DSL的方法中看到的主要区别是,数据库将在<camelContext>中定义,然后通过ID访问,但对我来说,我所写的似乎是正确的。

vhipe2zx

vhipe2zx1#

最后,我使用BeanIO库提供的BeanWriter类封送到输出文件,尽管是从我的处理器方法而不是从我的路由。我基本上复制了关于编组的文档:https://beanio.github.io/docs/

相关问题