Ashok Reddy
Ashok Reddy

Reputation: 174

Failed to run job based on schedule

I'm tying to run the batch based on schedule.I have used scheduled annotation & cron expression. Batch is running only once. No error is getting displayed. I have added maven dependency for quartz.I have not added any XML file.

@EnableAutoConfiguration(exclude = {  DataSourceAutoConfiguration.class,SwaggerConfig.class,
    WebMvcAutoConfiguration.class,RepositoryRestMvcAutoConfiguration.class })
@EnableScheduling
@ComponentScan
public class BatchApplication {

public static void main(String[] args) throws Exception {
     SpringApplication app = new SpringApplication(BatchApplication.class);
     app.setWebEnvironment(false);
     ConfigurableApplicationContext ctx = app.run(args);
     System.out.println(ctx.getBean(DataSource.class));
     JobLauncher jobLauncher = ctx.getBean(JobLauncher.class);
     Job addLeaveAllocationJob = ctx.getBean("addLeaveAllocationJob", Job.class); 
     JobParameters jobParameters = new JobParametersBuilder().addDate("date", new Date()) 
     .toJobParameters();   

     JobExecution jobExecution = jobLauncher.run(addLeaveAllocationJob, jobParameters); 
     BatchStatus batchStatus = jobExecution.getStatus();
     while(batchStatus.isRunning()){
         System.out.println("*** Still Running ************");
         Thread.sleep(2000);
     }
}

}

I have job class which is scheduled with @scheduled annotation with cron expression.

@Configuration
@EnableBatchProcessing
@Component
public class LeaveAllocationJobConfiguration {

@Autowired
private JobBuilderFactory jobs;

@Autowired
private StepBuilderFactory stepBuilderFactory;

@Autowired
private EntityManagerFactory entityManagerFactory;

@Bean
public ItemReader<Employee> reader() {
    JpaPagingItemReader<Employee> employeeReader = new JpaPagingItemReader<Employee>();
    employeeReader.setEntityManagerFactory(entityManagerFactory);
    employeeReader.setQueryString("from Employee");
    return employeeReader;
}

@Bean
@Scheduled(cron="0 0/1 * 1/1 * ? *")
public Job addLeaveAllocationJob() {
    System.out.println("Hello");
    return jobs.get("addLeaveAllocationJob").listener(protocolListener()).start(step()).build();
}

@Bean
public Step step() {
    // important to be one in this case to commit after every line read
    return stepBuilderFactory.get("step").<Employee, EmployeeDTO> chunk(1).reader(reader()).processor(processor())
            .writer(writer()).build();
}

/**
 * @return
 */
@Bean
public ItemWriter<? super EmployeeDTO> writer() {
    return new ItemWriter<EmployeeDTO>() {

        @Override
        public void write(List<? extends EmployeeDTO> items) throws Exception {
            System.out.println("Processing " + items);

        }
    };
}

@Bean
public ItemProcessor<Employee, EmployeeDTO> processor() {
    return new ItemProcessor<Employee, EmployeeDTO>() {

        @Override
        public EmployeeDTO process(Employee employee) throws Exception {

            return new EmployeeDTO(employee);
        }
    };
}

@Bean
public ProtocolListener protocolListener() {
    return new ProtocolListener();
}

}

Please help me to solve the issue

Upvotes: 2

Views: 58

Answers (1)

AngularDeveloper
AngularDeveloper

Reputation: 93

Chek this link.enter link description here

Upvotes: 1

Related Questions