Get current resource name using MultiResourceItemReader Spring batch

前端 未结 3 1646
忘掉有多难
忘掉有多难 2020-11-29 13:42

I am using MultiResourceItemReader in Spring Batch for reading multiple XML files and I want to get current resource.Here is my configuration:

p         


        
相关标签:
3条回答
  • 2020-11-29 14:11
    public class CpsFileItemProcessor implements ItemProcessor<T, T> {
    
        @Autowired
        MultiResourceItemReader multiResourceItemReader;
    
        private String fileName;
    
        @Override
        public FileDetailsEntityTemp process(T item) {
            if(multiResourceItemReader.getCurrentResource()!=null){
                fileName =  multiResourceItemReader.getCurrentResource().getFilename();
            }
        item.setFileName(fileName);
            return item;
        }
    }
    
    0 讨论(0)
  • 2020-11-29 14:20

    There is a specific interface for this problem called ResourceAware: it's purpouse is to inject current resource into objects read from a MultiResourceItemReader.
    Check this thread for further information.

    0 讨论(0)
  • 2020-11-29 14:35

    I tried it with a simple Listener for logging the current resource from a injected {@link MultiResourceItemReader}. Saves the value to the StepExecutionContext.

    To get it working with a step scoped MultiResourceItemReader i access the proxy directly, see http://forum.springsource.org/showthread.php?120775-Accessing-the-currently-processing-filename, https://gist.github.com/1582202 and https://jira.springsource.org/browse/BATCH-1831.

    public class GetCurrentResourceChunkListener implements ChunkListener, StepExecutionListener {
    
        private StepExecution stepExecution;
        private Object proxy;
        private final List<String> fileNames = new ArrayList<>();
    
        public void setProxy(Object mrir) {
            this.proxy = mrir;
        }
    
        @Override
        public void beforeStep(StepExecution stepExecution) {
            this.stepExecution = stepExecution;
        }
    
        @Override
        public ExitStatus afterStep(StepExecution stepExecution) {
            return stepExecution.getExitStatus();
        }
    
        @Override
        public void beforeChunk(ChunkContext cc) {
            if (proxy instanceof Advised) {
                try {
                    Advised advised = (Advised) proxy;
                    Object obj = advised.getTargetSource().getTarget();
                    MultiResourceItemReader mrirTarget = (MultiResourceItemReader) obj;
                    if (mrirTarget != null
                            && mrirTarget.getCurrentResource() != null
                            && !fileNames.contains(mrirTarget.getCurrentResource().getFilename())) {
                        String fileName = mrirTarget.getCurrentResource().getFilename();
                        fileNames.add(fileName);
                        String index = String.valueOf(fileNames.indexOf(fileName));
                        stepExecution.getExecutionContext().put("current.resource" + index, fileName);
                    }
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    
        @Override
        public void afterChunk(ChunkContext cc) {
        }
    
        @Override
        public void afterChunkError(ChunkContext cc) {
        }
    }
    

    see https://github.com/langmi/spring-batch-examples-playground for a working example - look for "GetCurrentResource..."

    0 讨论(0)
提交回复
热议问题