本文介绍了Spring Batch Classifier Composite Item Writer错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上必须为每种货币的每个file_id(即usd,zar等)生成多个xml文件,这些交易都在1个DB表中。我是否为每种货币创建复合编写器,并且在项目处理器上针对从数据库中读取的每种不同货币进行过滤。或是否可以对每个file_id的每种货币使用多个步骤?我一直在努力寻找围绕此的Springbatch解决方案。

I have to basically produce multiple xml files for each file_id per currency ( ie. usd,zar ect) these transactions are all in 1 DB table. Do I create a composite writer for each currency and on my Item Processor I filter for each different currency that I read from the DB. or Can I use multiple steps for each currency per file_id ? I have been struggling to find a Springbatch solution around this.

每个文件和币种的文件名资源都会不同。例如,我可以接收到file_id = 1 currency = USD需要为1个文件 USD20051701,文件序列为01。我还可以得到两个文件file_id 1&货币= ZAR为2,并且必须为两个文件 ZAR20051701和& ‘ZAR20051702’01& 02个文件序列。

The filename resource will be different for each file and currency. For example I can recieve file_id=1 currency=USD needs to be 1 file 'USD20051701 with 01 the file sequence'. I can also get two files file_id 1 & 2 for Currency ='ZAR' and those need to be two files 'ZAR20051701' & 'ZAR20051702' 01 & 02 file sequences.

我从其中一个帖子中使用此链接作为指南。

I was using this link as a guide from one of the posts. https://stackoverflow.com/a/53388876/13056119

我正在获取此跟踪日志

@Bean
public ClassifierCompositeItemWriter<Settlement> classifierCompositeItemWriter
        (       ItemWriter<Settlement> ZMWItemWriter,
                ItemWriter<Settlement> USDItemWriter,
                ItemWriter<Settlement> ZARItemWriter
    ) {
    ClassifierCompositeItemWriter<Settlement> classifierCompositeItemWriter = new ClassifierCompositeItemWriter<>();
    classifierCompositeItemWriter.setClassifier(new Classifier<Settlement, ItemWriter<? super Settlement>>() {

        @Override
        public ItemWriter<? super Settlement> classify(Settlement settlement) {

            List<SettlementHeader> settlementheader= new ArrayList<SettlementHeader>();
            SettlementHeader header = new SettlementHeader ();
            settlementheader.add(header);
            settlement.setSettlementHeader(settlementheader);

        if (header.getCurrency().equalsIgnoreCase("ZMW")) {
            return ZMWItemWriter;
        }
        else if (header.getCurrency().equalsIgnoreCase("USD")) {
            return USDItemWriter;
        }
        else {
            return ZARItemWriter;}
        }
    });
    return classifierCompositeItemWriter;
}

    @Qualifier ("USDItemWriter")
    @Bean(destroyMethod="")
   public NoRootStaxEventItemWriter<Settlement> USDItemWriter() throws Exception {
    NoRootStaxEventItemWriter<Settlement> ItemWriter = new NoRootStaxEventItemWriter<>();
    FileSystemResource resource = new FileSystemResource("FileUSD1.xml");
    ItemWriter.setName("USDItemWriter");
       ItemWriter.setResource(resource);
       marshaller.setPackagesToScan("com.model");
       ItemWriter.setMarshaller(marshaller);
       ItemWriter.afterPropertiesSet();
       return ItemWriter;
   }

@Primary
@Qualifier("ZARItemWriter")
@Bean(destroyMethod="")
   public NoRootStaxEventItemWriter<Settlement> ZARItemWriter() throws Exception {
    NoRootStaxEventItemWriter<Settlement> ItemWriter = new NoRootStaxEventItemWriter<>();
    FileSystemResource resource = new FileSystemResource("FileZAR1.xml");
    ItemWriter.setName("ZARItemWriter");
       ItemWriter.setResource(resource);
       marshaller.setPackagesToScan("com.model");
       ItemWriter.setMarshaller(marshaller);
       ItemWriter.afterPropertiesSet();
       return ItemWriter;
   }
    @Qualifier("ZMWItemWriter")
    @Bean(destroyMethod="")
   public NoRootStaxEventItemWriter<Settlement> ZMWItemWriter() throws Exception {
    NoRootStaxEventItemWriter<Settlement> ItemWriter = new NoRootStaxEventItemWriter<>();
    FileSystemResource resource = new FileSystemResource("FileZMW1.xml");
    ItemWriter.setName("ZMWItemWriter");
    ItemWriter.setResource(resource);
    marshaller.setPackagesToScan("com.model");
    ItemWriter.setMarshaller(marshaller);
    ItemWriter.afterPropertiesSet();
      return ItemWriter;
   }


推荐答案

请检查下面的示例,然后看看是否适合您。您创建Writers的 Map 以避免每次实例化一个新Writer。

Please check the example below and see if it works for you. You create a Map of Writers to avoid instantiating a new Writer each time.

Writer由(FileId,ProcessDate,Detail Currency唯一标识) ):

A Writer is identified uniquely by (FileId, ProcessDate, Detail Currency):

String fileNameKey = item.getHeader().getFileId() + item.getHeader().getProcessDate().toString() + settlementDetail.getCurrency();

以下是作者的代码:

@Component
public class SettlementWriter implements ItemStream, ItemWriter<Settlement> {

    private final Map<String, StaxEventItemWriter<Settlement>> writers = new HashMap<>();

    private ExecutionContext executionContext;

    @Autowired
    private Marshaller marshaller;

    @Override
    public void open(ExecutionContext executionContext) throws ItemStreamException {
        this.executionContext = executionContext;
    }

    @Override
    public void update(ExecutionContext executionContext) throws ItemStreamException {
    }

    @Override
    public void close() throws ItemStreamException {
        for (StaxEventItemWriter f : writers.values()) {
            f.close();
        }
    }

    @Override
    public void write(List<? extends Settlement> items) throws Exception {
        for (Settlement item : items) {
            for (SettlementDetail det : item.getDetails()) {
                StaxEventItemWriter<Settlement> detailWriter = getFlatFileItemWriter(item, det);
                detailWriter.write(Arrays.asList(item));
            }
        }
    }

    public StaxEventItemWriter<Settlement> getFlatFileItemWriter(Settlement item, SettlementDetail settlementDetail) {
        String fileNameKey = item.getHeader().getFileId() + item.getHeader().getFilename() + settlementDetail.getCurrency();

        StaxEventItemWriter<Settlement> itemWriter = writers.get(fileNameKey);

        if (itemWriter == null) {

            itemWriter = new StaxEventItemWriter<>();

            try {

                FileSystemResource resource = new FileSystemResource(fileNameKey+".xml");

                itemWriter.setName(fileNameKey);
                itemWriter.setResource(resource);
                marshaller.setPackagesToScan("com.model");
                itemWriter.setMarshaller(marshaller);
                itemWriter.afterPropertiesSet();

                itemWriter.open(executionContext);

            } catch (Exception e) {
                e.printStackTrace();
            }
            writers.put(fileNameKey, itemWriter);
        }
        return itemWriter;
    }
}

希望这会有所帮助。

这篇关于Spring Batch Classifier Composite Item Writer错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:23