Now I will configure spring 4.2 with quartz 2.2.2 using Java configuration. I am using quartz in cluster.
Java configuration class
@Configuration
@ComponentScan({ "hr.samara.job" })
public class QuartzConfig
{
@Autowired
private DataSource dataSource;
@Autowired
private JpaTransactionManager transactionManager;
@Autowired
private Environment env;
@Autowired
private SimOperation simOperation;
@Bean
public SchedulerFactoryBean scheduler()
{
SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
scheduler.setConfigLocation(new ClassPathResource("quartz.properties"));
scheduler.setDataSource(dataSource);
scheduler.setTransactionManager(transactionManager);
// inject simOperation - spring bean in job
Map<String, Object> map = new HashMap<>();
map.put("simOperation", simOperation);
scheduler.setSchedulerContextAsMap(map);
scheduler.setTriggers(blockSimTrigger().getObject());
return scheduler;
}
@Bean
public CronTriggerFactoryBean blockSimTrigger()
{
CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
cronTriggerFactoryBean.setJobDetail(blockSimJob().getObject());
cronTriggerFactoryBean.setCronExpression(env.getProperty("job.blockSim.cron"));
cronTriggerFactoryBean.setMisfireInstructionName("MISFIRE_INSTRUCTION_FIRE_ONCE_NOW");
return cronTriggerFactoryBean;
}
@Bean
public JobDetailFactoryBean blockSimJob()
{
JobDetailFactoryBean jobDetailFactory = new JobDetailFactoryBean();
jobDetailFactory.setJobClass(BlockSimJob.class);
jobDetailFactory.setDescription("call sim block");
return jobDetailFactory;
}
}
Job class
@DisallowConcurrentExecution
public class BlockSimJob extends QuartzJobBean
{
private transient SimOperation simOperation;
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException
{
simOperation.blockSim();
}
public void setSimOperation(SimOperation simOperation)
{
this.simOperation = simOperation;
}
}
Ref:
http://www.baeldung.com/spring-quartz-schedule
No comments:
Post a Comment