title: Spring batch conditional step, Not executing properly
body: <p>I'm basically new to spring batch. For my first step I am getting a file from an SFTP server and transferring it to a local directory using JSch. For the second step, I read the file in my local directory and write the contents in my DB. The problem is that there are instances wherein only <strong>saveWithdrawalTransaction</strong> (<strong>step2</strong>) executes and not <strong>getRemoteFile</strong> (<strong>step1</strong>)</p>
  
  <p><a href="https://i.stack.imgur.com/vdlc8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vdlc8.png" alt="execution from db"></a></p>
  
  <p>Configuration Class:</p>
  
  <pre><code>@Qualifier("writer")
  @Autowired
  JdbcBatchItemWriter&lt;TopUpTransaction&gt; writer;
  
  @Qualifier("reader")
  @Autowired
  FlatFileItemReader&lt;TopUpTransaction&gt; reader;
  
  @Bean(name="importWithdrawalTransactionJob")
  public Job importWithdrawalTransactionJob() throws Exception {
      return jobBuilderFactory.get("importWithdrawalTransactionJob")
              .incrementer(new RunIdIncrementer())
              .start(getRemoteFile()).on(ExitStatus.FAILED.getExitCode()).end()
              .next(saveWithdrawalTransaction()).end()
              .build();
  }
  
  @Bean
  public GetRemoteFileTasklet sFtpGetRemoteFilesTasklet() {
      return new GetRemoteFileTasklet();
  }
  
  @Bean
  public Step getRemoteFile() throws Exception {
      return stepBuilderFactory.get("getRemoteFile")
              .tasklet(sFtpGetRemoteFilesTasklet())
              .build();
  }
  
  @Bean
  public Step saveWithdrawalTransaction() throws Exception {
      return stepBuilderFactory.get("saveWithdrawalTransaction")
              .&lt;TopUpTransaction, TopUpTransaction&gt; chunk(1)
              .reader(reader)
              .processor(processor())
              .writer(writer)
              .build();
  }
  </code></pre>
  
  <p>Reader: </p>
  
  <pre><code>@Bean
  public FlatFileItemReader&lt;TopUpTransaction&gt; reader() throws Exception {
  
  File folder = new File(csvFilePath);
  File filesToProcess[] = folder.listFiles();
  FlatFileItemReader&lt;TopUpTransaction&gt; reader = new FlatFileItemReader&lt;TopUpTransaction&gt;();
  
  if (filesToProcess.length &gt; 0) {
  for (File file : filesToProcess) {
      if (file.isFile()) {
          System.out.println(file.getName());
          reader.setResource( new FileSystemResource(file.getAbsolutePath()));
          reader.setLineMapper(new DefaultLineMapper&lt;TopUpTransaction&gt;() {{
              setLineTokenizer(new DelimitedLineTokenizer() {{
                  setNames(new String[] { "lineIdentifier","transactionDate", "beneAccNum", "beneAccName", "senderName", "senderInfo",
                          "creditedAmountWithCurrency", "originalAmountWithCurrency", "netAmountWithCurrency", 
                          "charges", "otherInfo1", "otherInfo2",
                          "ubTransactionId"});
                  setDelimiter("|");
              }});
              setFieldSetMapper(new CustomTopUpMapper());          
          }});
          reader.setRecordSeparatorPolicy(new CustomReaderPolicy());
          System.out.println("end read");
          file.renameTo(new File(file.getName() + "-processed"));
      }
  }
  </code></pre>
  
  <p>Writer:</p>
  
  <pre><code>@Bean(name="writer")
  public JdbcBatchItemWriter&lt;TopUpTransaction&gt; writer() {
  
      StringBuffer insertWithdrawalTransaction = new StringBuffer();
      insertWithdrawalTransaction.append("INSERT INTO top_up_transactions (customer_id,txn_date, bene_acc_num, bene_acc_name,");
      insertWithdrawalTransaction.append("sender_name, sender_info, credited_amount, credited_currency,");
      insertWithdrawalTransaction.append("original_amount, original_currency,");
      insertWithdrawalTransaction.append("net_amount, net_currency, charges, other_info_1, other_info_2,");
      insertWithdrawalTransaction.append("ubp_txn_id,status) ");
      insertWithdrawalTransaction.append("VALUES (:customerId, :transactionDate,:beneAccNum,");
      insertWithdrawalTransaction.append(":beneAccName,:senderName,:senderInfo,:creditedAmount,:creditedAmountCurrency,");
      insertWithdrawalTransaction.append(":originalAmount,:originalAmountCurrency,");
      insertWithdrawalTransaction.append(":netAmount,:netAmountCurrency,:charges,:otherInfo1,:otherInfo2,");
      insertWithdrawalTransaction.append(":ubTransactionId,:status)");
      System.out.println("start write");
      JdbcBatchItemWriter&lt;TopUpTransaction&gt; writer = new JdbcBatchItemWriter&lt;TopUpTransaction&gt;();
      writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider&lt;TopUpTransaction&gt;());
      writer.setSql(insertWithdrawalTransaction.toString());
      writer.setDataSource(dataSource);
      System.out.println("end write");
  
      return writer;
  }
  </code></pre>
  
  <p>Tasklet:</p>
  
  <pre><code>public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception
  {
      Session session = null;
      Channel channel = null;
      //deleteLocalFiles();
      System.out.println("=====start tasklet=====");
      try {
          JSch ssh = new JSch();
          session = ssh.getSession("username", "host", 22);
          session.setPassword("password");
          session.setConfig("StrictHostKeyChecking", "no");
          session.connect();
          channel = session.openChannel("sftp");
          channel.connect();
          ChannelSftp sftp = (ChannelSftp) channel;
          sftp.get("*.rpt", "src/main/resources/files/");
      } catch (JSchException e) {
          e.printStackTrace();
      } catch (SftpException e) {
          e.printStackTrace();
      } finally {
          if (channel != null) {
              channel.disconnect();
          }
          if (session != null) {
              session.disconnect();
          }
      }
  </code></pre>
  
  <p>I don't have an idea how to go about this... I have been trying out stuff I found from everywhere. Please help thank you!  </p>