0
@Configuration
public class BookStorePoliciesJobConfiguration {

    private static final String CRON_EXPRESSION_FOR_JOB = "0 0/10 * 1/1 * ? *";//change
    private static final String JOB_NAME = "bookStorePolicyJob";
    private static final String JOB_STEP_NAME = JOB_NAME + "Step";

    @Autowired
    private DataSource nonXAcrmDataSource;

    @Autowired
    private JtaTransactionManager transactionManager;

    @Bean
    public CanerScheduledJobFactoryBean bookStorePoliciesScheduledJob() {
        MafScheduledJobFactoryBean bean = new MafScheduledJobFactoryBean();
        bean.setBatchJobName(JOB_NAME);
        bean.setCronExp(CRON_EXPRESSION_FOR_JOB);
        bean.setNonXAcrmDataSource(this.nonXAcrmDataSource);
        return bean;
    }

    @Bean
    public Job bookStorePolicyJob(@Autowired BookStorePoliciesJobTasklet tasklet,
                                   @Autowired JobRepository batchJobRepository) {
        SimpleJob job = new SimpleJob(JOB_NAME);
        job.setSteps(Collections.singletonList(bookStorePolicyJobStep(tasklet, batchJobRepository)));
        job.setJobRepository(batchJobRepository);
        return job;
    }

    public Step bookStorePolicyJobStep(BookStoreJobTasklet tasklet, JobRepository batchJobRepository) {
        TaskletStep step = new TaskletStep(JOB_STEP_NAME);
        step.setTasklet(tasklet);
        step.setJobRepository(batchJobRepository);
        transactionManager.setAllowCustomIsolationLevels(true);
        step.setTransactionManager(transactionManager);
        return step;
    }
}

This is the job:

@Component
public class BookStoreJobTasklet extends CanerTasklet {

       @Override
        public RepeatStatus doExecute(StepContribution contribution, ChunkContext chunkContext) {

    ///
    //

    sendFileBySftp(fileName, file);//if this throws, should not go down line, should exit but it goes



            updateParameters();//if it is successfull only!
           return RepeatStatus.FINISHED;
        }

And the method:

  private void sendFileBySftp(String fileName, File file) {

//
//

try{

//
 } catch (JSchException | SftpException | IOException e) {
            logger.error("error in sendFileFTP {}", e.getMessage());//it comes here and continues. it should exit?
        } 

           logger.info("Session connection closed....");
        }

What should i do to stop batch?

i look at here Make a spring-batch job exit with non-zero code if an exception is thrown

Should i return RepeatStatus for each method so i can check if failed?

Maybe a boolean value and for each catch block, putting it to false?

msadasjwd
  • 105
  • 10
  • Does this answer your question? [Exit Spring Batch Job within tasklet](https://stackoverflow.com/questions/52484874/exit-spring-batch-job-within-tasklet) – Mahmoud Ben Hassine May 06 '20 at 07:39
  • @MahmoudBenHassine it says ** just throw an exception from it.** but i already do it, still continues. – msadasjwd May 06 '20 at 08:03
  • Well, your code does not show that. Please provide a minimal example that reproduces the issue: https://stackoverflow.com/help/minimal-reproducible-example – Mahmoud Ben Hassine May 06 '20 at 08:05
  • @MahmoudBenHassine here , you can just throw an exception **private void sendFileBySftp(String fileName, File file) {** to make it go to catch. – msadasjwd May 06 '20 at 11:10

0 Answers0