Read flat file and write to multiple writers which will write different objects

别等时光非礼了梦想. 提交于 2019-12-12 06:47:24

问题


i have a requirement to read a flatfile and create a TRADE java object.Now processor should create 3 different java objects based on TRADE java object. i have to write these 3 different java objects to 3 differnt xml files.

simply I want to one read 3 processors and 3 write to multiple xml files based on the read data.

I have tried compositeItemWriter but it will take the same object for writing.but i have 3 different object consumer,envelope,deliveryOrder

SampleInput :

11000000001F11000000062237      693553467588549
11000000001221000000030237      628525475867698
11000000031110000000562374      687598787668557
11000000001G21000000056237      687557575645346
11000000001F11000000062237      693553467588549
11000000001221000000030237      628525475867698

output 1.
    <consumers>
 <consumer>
            <sequenceNum>1481</sequenceNum>
            <sourceSystemBrandId>237</sourceSystemBrandId>
            <sourceCid>999000367588</sourceCid>
            <preferences>
                <preference>
                    <contentType>Statement</contentType>
                    <value>E</value>
                    <distributionChannel>test@gmail.com</distributionChannel>
                </preference>
            </preferences>
            <deliveryPoints>
                <dpvDeliveryPoint>
                    <dpv>1171783651</dpv>
                </dpvDeliveryPoint>
                <emailDeliveryPoint>
                    <email>test@gmail.com</email>
                </emailDeliveryPoint>
            </deliveryPoints>
  </consumer>
 </consumers>
output 2:
<envelopes>
<envelope>
    <sourceEnvId>xxxx</sourceEnvId>
    <sourceSystemBrandId>xxx</sourceSystemBrandId>
    <sourceCid>xxx</sourceCid>
    <envelopeData>
    <envelopeName>xxx</envelopeName>
    <contentType>statement</contentType>
    <statementData>
    <accountNumber>xxx</accountNumber>
    <accountName>xxx</accountName>
    <statementAmount/>
    <periodStartDate/>
    <periodEndDate/>
    <date>2017-09-04</date>
    </statementData>
<details><detail><name>Class_Email</name><value>xxx</value><type>xxx</type>
</detail></details>
    <envelopeDocuments>
    <envelopeDocument><docRef>Benhe</docRef></envelopeDocument>
    </envelopeDocuments>
    <printSuppressed>YES</printSuppressed>
    <historicalMail>NO</historicalMail>
    <asOfDate>2017-09-04</asOfDate>
    </envelopeData>

</envelope>
</envelopes>


output 3:

<deliveryOrder>
<consumer>
    <preferences>
        <!--1 or more repetitions:-->
        <preference>
            <contentType>string</contentType>
            <value>string</value>
            <!--Optional:-->
            <effectiveDate>2006-08-19T19:27:14+02:00</effectiveDate>
        </preference>
    </preferences>
    <!--Optional:-->
    <details>
        <detail>
            <name>string</name>
            <value>string</value>
        </detail>
        <detail>
            <name>string</name>
            <value>string</value>
        </detail>
        <detail>
            <name>string</name>
            <value>string</value>
        </detail>
    </details>
    <deliveryPoints>
        <unstructuredNameDeliveryPoint>
            <name>string</name>
        </unstructuredNameDeliveryPoint>
    </deliveryPoints>
</consumer>
<envelopeDocuments>
    <envelopeDocument>
        <docRef>string</docRef>
        <docFingerprint>string</docFingerprint>
    </envelopeDocument>
</envelopeDocuments>
<printSuppressed>NO</printSuppressed>
<historicalMail>Yes</historicalMail>
<asOfDate>2005-09-12+02:00</asOfDate>
</envelopeData>undefined</deliveryOrder>

Appreciate any help on this. Thanks.


回答1:


You're close with the CompositeItemWriter. However, there is a better option based on what you're describing which is the ClassifierCompositeItemWriter. This ItemWriter implementation allows you to inject a Classifier which you can implement to chose which ItemWriter your object ends up begin passed to. In fact, we even provide a Classifier implementation that should be useful for you, the SubclassClassifier. That takes a map of classes as the key and the ItemWriter is the value. This will allow you to map each object type to the appropriate ItemWriter.



来源:https://stackoverflow.com/questions/46510033/read-flat-file-and-write-to-multiple-writers-which-will-write-different-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!