0
votes

I have the Spring Batch application that uses the task scheduler and currently set to run every 3 minutes.

As of now, the application schedule (cron job) is hard coded as highlighted below

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task.xsd">

    <bean id="transactionManager"
          class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <bean id="jobLauncher"  class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository" />
        <property name="taskExecutor" ref="simpleAsyncTaskExecutor" />
    </bean>

    <bean id="simpleAsyncTaskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor" />

    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean" />

    <bean id="springSchedulingLauncher" class="com.javacodegeeks.example.scheduler.JobScheduler"></bean>

    <!-- -->
    <task:scheduler id="scheduler" />

    <task:scheduled-tasks scheduler="scheduler">

        <task:scheduled ref="springSchedulingLauncher"
                        method="launch"
                        cron="0 0/3 * ? * *"
        />
    </task:scheduled-tasks>
    <!-- -->

</beans>

below is the java code

package com.javacodegeeks.example.scheduler;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;

public class JobScheduler {
    @Scheduled(cron = "0 0/3 * ? * *")
    public void launch() throws Exception {
        System.out.println("Starting Launch");
        String[] str = {"META-INF/spring/context-config.xml","META-INF/spring/job-config.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(str);
        Job job = (Job) ctx.getBean("myJob");
        JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
        try{
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Execution Status: "+ execution.getStatus());
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("Completed Launch");
    }
}

and the App.java (main method) is given below

package com.javacodegeeks.example.app;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.scheduling.annotation.Scheduled;
import java.util.Date;

public class App {
    public static void main(String[] args) {
        System.out.println("Starting Main");
        String[] str = {"META-INF/spring/context-config.xml","META-INF/spring/job-config.xml"};
        ApplicationContext ctx = new ClassPathXmlApplicationContext(str);
        Job job = (Job) ctx.getBean("myJob");
        JobLauncher jobLauncher = (JobLauncher) ctx.getBean("jobLauncher");
        try{
            JobExecution execution = jobLauncher.run(job, new JobParameters());
            System.out.println("Job Execution Status: "+ execution.getStatus());
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("Completed Main");
    }
}

It looks like it is running starting multiple jobs and not sure why? How do I fix this?

enter image description here

1
are you sure that is the cron job that is invoking that? - Alberto Sinigaglia
Yes, Starting Launch & Completed Launch statements are coming from the JobScheduler class. - Mugil Karthikeyan
try with * * * * * - Alberto Sinigaglia

1 Answers

0
votes

Your XML elements <task:scheduler> and <task:scheduled-tasks> are not properly commented with <!-- -->. You think they are commented but it is not the case. Hence, the task is scheduled twice: once in the XML config and once in Java config inside the launch method.

Here is how to properly comment the XML section:

<!--
    <task:scheduler id="scheduler" />

    <task:scheduled-tasks scheduler="scheduler">

        <task:scheduled ref="springSchedulingLauncher"
                        method="launch"
                        cron="0 0/3 * ? * *"
        />
    </task:scheduled-tasks>
-->