title: spring batch Classifier: Writer must be open before it can be written to
body: <p>I'm new to spring and which to split database to 3 csv files.</p>
  
  <p>My actual code: (I'm not using xml and don't want to)</p>
  
  <p>I've tried many solutions but nothing work.</p>
  
  <pre><code>    class MyClassifier {
  
          @Classifier
          public String classify(Pojo Pojo) {
  return String.valueOf(Pojo.getYear());
          }
      }
  
      @Bean(name = "PojoWriter") 
      @StepScope
      public ItemWriter&lt;Pojo&gt; itemWriter() throws Exception {
          BackToBackPatternClassifier classifier = new BackToBackPatternClassifier();
          classifier.setRouterDelegate(new MyClassifier());
  
          classifier.setMatcherMap(new HashMap&lt;String, FlatFileItemWriter&lt;? super Pojo&gt;&gt;() {
              FlatFileItemWriter&lt;Pojo&gt; writerH;
              {
              writerH = writer("2017",null,null); put("2017", writerH); writerH.close(); 
              writerH = writer("2018",null,null); put("2018", writerH); writerH.close();
              writerH = writer("2019",null,null); put("2019", writerH); writerH.close();
              }
          });
          ClassifierCompositeItemWriter&lt;Pojo&gt; writer = new ClassifierCompositeItemWriter&lt;Pojo&gt;();
          writer.setClassifier(classifier);
          return writer;
      }
  </code></pre>
  
  <p>I classify with year, and 3 files are created</p>
  
  <pre><code>//  @Bean(name = "FlatWriter") 
  //  @StepScope
      public FlatFileItemWriter&lt;Pojo&gt; writer( final String name,
              @Value("#{stepExecutionContext['fromId']}") final String fromId,
              @Value("#{stepExecutionContext['toId']}") final String toId) throws Exception {
  
          FlatFileItemWriter&lt;Pojo&gt; writer = new FlatFileItemWriter&lt;Pojo&gt;();
  
          writer.setResource(new FileSystemResource(name + ".csv"));
          writer.setShouldDeleteIfExists(true);
          writer.open(new ExecutionContext());
          writer.setHeaderCallback(new FlatFileHeaderCallback() {
              @Override
              public void writeHeader(Writer writer) throws IOException {
                  writer.write(
                          "Id, number, name");
              }
          });
          writer.setLineAggregator(new DelimitedLineAggregator&lt;Pojo&gt;() {
              {
  
                  setDelimiter(",");
                  setFieldExtractor(new BeanWrapperFieldExtractor&lt;Pojo&gt;() {
                      {
                          setNames(new String[] { "Id", "number", "name" });
                      }
                  });
              }
          });
  
          return writer;
      }
  
      @Bean
      @StepScope
      public Step step1() throws Exception {
          return stepBuilderFactory.get("step1").&lt;Foo, Pojo&gt;chunk(10)
                  .reader(reader())
                  .processor(processor()) 
                  .writer(itemWriter())
                  .build();
      }
  
      @Bean
      public Job exportUserJob() throws Exception {
          return jobBuilderFactory.get("exportUserJob")
                  .flow(step1()).end().build();   
      }
  </code></pre>
  
  <p>Parameters :
      <code>@Component</code>
      <code>@EnableBatchProcessing</code>
      <code>@Configurable</code>
   and <code>spring.batch.job.enabled=false</code>    </p>
  
  <p>stacktrace of error :</p>
  
  <blockquote>
    <p>2019-07-15 11:13:44.666 ERROR 12632 --- [   Spring Shell] o.s.batch.core.step.AbstractStep         : Encountered an error executing step step1 in job exportUserJob</p>
    
    <p>org.springframework.batch.item.WriterNotOpenException: Writer must be open before it can be written to
            at org.springframework.batch.item.file.FlatFileItemWriter.write(FlatFileItemWriter.java:255) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.item.support.ClassifierCompositeItemWriter.write(ClassifierCompositeItemWriter.java:66) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) ~[spring-tx-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
            at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302) [spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
            at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208) [spring-aop-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at com.sun.proxy.$Proxy80.run(Unknown Source) [na:na]
            at com.viviane.commands.CsvGenerator.transactionalCsvBuilder(CsvGenerator.java:700) [classes/:na]
            at com.viviane.commands.Scanner.createFiles(Scanner.java:93) [classes/:na]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_161]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_161]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_161]
            at org.springframework.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:216) [spring-core-4.2.6.RELEASE.jar:4.2.6.RELEASE]
            at org.springframework.shell.core.SimpleExecutionStrategy.invoke(SimpleExecutionStrategy.java:64) [spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE]
            at org.springframework.shell.core.SimpleExecutionStrategy.execute(SimpleExecutionStrategy.java:57) [spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE]
            at org.springframework.shell.core.AbstractShell.executeCommand(AbstractShell.java:127) [spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE]
            at org.springframework.shell.core.JLineShell.promptLoop(JLineShell.java:533) [spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE]
            at org.springframework.shell.core.JLineShell.run(JLineShell.java:179) [spring-shell-1.1.0.RELEASE.jar:1.1.0.RELEASE]
            at java.lang.Thread.run(Unknown Source) [na:1.8.0_161]</p>
  </blockquote>