2016-08-23 87 views
2

我正在按計劃運行批處理。我使用了計劃註釋& cron表達式。批量只運行一次。沒有錯誤顯示。我已經添加了石英的maven依賴項。我沒有添加任何XML文件。無法根據計劃運行作業

@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); 
    } 
} 

}

我有計劃與cron表達式@scheduled註釋作業類。

@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(); 
} 

} 

請幫我解決這個問題

回答