Spring batch Step after JMS Reader/Processor/Writer step is not getting triggered

2020-05-23 java spring jms spring-batch exit-code

Our spring batch needs to have 2 steps

Step1 : Deque's a message from JMS and process the message and write output data to a file. Step 2 : Partitioned step that contain 2 slave steps .Each salve step applies a different algortihm on step1 output data.

Issue: Step 2 is never invoked . Tried to attach a Step listener to step 1 (and also to writer) and it was never executed. It looks like step1 is always in continued state and hence step2 (Partitioned step) is never getting executed.

Observation : When replaced JMSReader in step 1 with Normal (File/DB) Reader, control goes to Step2

NOTE : Need firstQueueTemplate.setReceiveTimeout(Long.MAX_VALUE); as we need to continuously dequeue message one by one on a continuous basis.

    public TransactionAwareConnectionFactoryProxy activeMQConnectionFactory() {
        ActiveMQConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
        TransactionAwareConnectionFactoryProxy activeMQConnectionFactory = new TransactionAwareConnectionFactoryProxy(amqConnectionFactory);
        return activeMQConnectionFactory;
    }

    @Bean
    public ActiveMQQueue defaultQueue() {
        return new ActiveMQQueue("batch-test");
    }

    @Bean
    @DependsOn(value = { "activeMQConnectionFactory", "defaultQueue" })
    public JmsTemplate firstQueueTemplate(ActiveMQQueue defaultQueue, TransactionAwareConnectionFactoryProxy activeMQConnectionFactory) {
        JmsTemplate firstQueueTemplate = new JmsTemplate(activeMQConnectionFactory);
        firstQueueTemplate.setDefaultDestination(defaultQueue);
        firstQueueTemplate.setSessionTransacted(true);
        firstQueueTemplate.setReceiveTimeout(Long.MAX_VALUE);
        return firstQueueTemplate;
    }

    @Bean(name = "partitionerJob")
    public Job partitionerJob() throws UnexpectedInputException, MalformedURLException, ParseException {
        return jobs.get("partitionerJob")
          .start(ingestionstep())
          .next(partitionStep())
          .build();
    }




    @Bean
    public Step ingestionstep() throws UnexpectedInputException, MalformedURLException, ParseException {
        System.out.println("ingestionstep forming");
        return steps.get("ingsetionstep")
                .<SPDRIngestScanBO, SPDRIngestScanBO>chunk(1)
                  .reader(jmsItemReader())
                  .processor(ingestionProcessor())
                  .writer(ingestionwriter() )
                  .listener(new StepExecutionListener() {

                                @Override
                                public void beforeStep(StepExecution stepExecution) {
                                    // TODO Auto-generated method stub

                                }

                                @Override
                                public ExitStatus afterStep(StepExecution stepExecution) {
                                    System.out.println("step exit status :"+stepExecution.getExitStatus());
                                    return null;
                                }
                })
                 // .listener(promotionListener())

                  .build();
    }




   @Bean
    @StepScope
    public Step partitionStep() throws UnexpectedInputException, MalformedURLException, ParseException {
        System.out.println(" Inside partitionStep method ");
        return steps.get("partitionStep")
                 .partitioner("partitionscans", partitioner(null))
                 .gridSize(2)
                 .step(scanStep())
                 .taskExecutor(taskExecutor())
                 .build();
    }

    @Bean
    public JmsItemReader<SPDRIngestScanBO> jmsItemReader() {
        JmsItemReader<SPDRIngestScanBO> jmsItemReader = new JmsItemReader<>();
        jmsItemReader.setJmsTemplate(jmsTemplate);
        jmsItemReader.setItemType(SPDRIngestScanBO.class);

        return jmsItemReader;
    }

    @Bean
    public  SPDRIngestionStepProcessor  ingestionProcessor() {
        return new SPDRIngestionStepProcessor();
    }

    @Bean
    public  SPDRIngestionStepWriter  ingestionwriter() {
        return new SPDRIngestionStepWriter();
    }


    @Bean
    @StepScope
    public ModelsPartitioner partitioner(@Value("#{jobExecutionContext[models]}")  List<SPDRScanModelBO> models) {

        ModelsPartitioner partitioner = new ModelsPartitioner();
        partitioner.setModels(models);
       System.out.println("----partitioner----");
        return partitioner;
    }


    @Bean
    @StepScope
    public Step scanStep() throws UnexpectedInputException, MalformedURLException, ParseException {
        return steps.get("scanstep")
          .<SPDRScanModelBO, SPDRScanResultBO>chunk(1)
          .reader(scanStepReader(null))
          .processor(scanStepProcessor())
          .writer(scanStepWriter())
          .build();
    }


  @Bean
  @StepScope
  public SPDRScanStepReader scanStepReader(@Value("#{stepExecutionContext[model]}") SPDRScanModelBO scanModelBO){
      System.out.println("----scanStepReader----"); 
      SPDRScanStepReader scanStepReader = new SPDRScanStepReader();
      scanStepReader.setScanModelBO(scanModelBO);
      return scanStepReader;

  }

  @Bean
  @StepScope
  public SPDRScanStepProcessor scanStepProcessor(){

      SPDRScanStepProcessor scanStepProcessor = new SPDRScanStepProcessor();
      return scanStepProcessor;

  }

  @Bean
  @StepScope
  public SPDRScanStepWriter scanStepWriter(){

      SPDRScanStepWriter scanStepWriter = new SPDRScanStepWriter();
      return scanStepWriter;

  }

    @Bean
    @StepScope
    public TaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(2);
        taskExecutor.setCorePoolSize(2);
        taskExecutor.setQueueCapacity(2);
        taskExecutor.afterPropertiesSet();
        return taskExecutor;
    }
    ```

Answers

In Spring Batch, when you run two steps sequentially, say step1 then step2, then step2 will be executed only when step1 is complete.

In your case, step1 reads from a jms queue with a receiveTimeout = Long.MAX_VALUE, so unless this timeout happens, your step1 will not complete and hence step2 will not be started.

Related