我有几个Spring Batch作业,如果在Spring Batch中触发了作业执行,它们可以正常工作。这些作业将使用JpaItemWriter读取和写入需要事务的数据库。
我的问题是,我将石英调度程序配置为定期运行这些作业,但出现“ javax.persistence.TransactionRequiredException:无事务进行中”错误。我知道当前石英正在实例化作业bean而不是spring本身,这使bean不知道spring所管理的正在进行的事务,如果我错了,请纠正我。
但是,我尝试了很多方法,但是没有一种起作用。以下是我当前的配置:
quartz.properties
org.quartz.scheduler.instanceName=sample_instance
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=5
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreCMT
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.MSSQLDelegate
org.quartz.jobStore.useProperties=true
org.quartz.jobStore.misfireThreshold=60000
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=20000
SchedulerConfig.java
@EnableScheduling
@Configuration
public class SchedulerConfig {
@Inject
private DataSource dataSource;
@Inject
private JobsListenerService jobsListenerService;
@Bean
public JobFactory jobFactory(ApplicationContext applicationContext) {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory)
throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(jobFactory);
factory.setDataSource(dataSource);
factory.setQuartzProperties(quartzProperties());
factory.setGlobalJobListeners(jobsListenerService);
return factory;
}
@Bean
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
}
工作:
@Component
@DisallowConcurrentExecution
public class TestJob implements Job {
private static final String PARAMETERS = "param1=abc";
@Inject
private JobRegistry jobRegistry;
@Inject
private JobLauncher jobLauncher;
@Inject
private JobRepository jobRepository;
@Inject
private JobExplorer jobExplorer;
private JobParametersConverter jobParametersConverter = new DefaultJobParametersConverter();
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
try {
org.springframework.batch.core.Job job = jobRegistry.getJob("Job1");
JobParameters jobParameters = JobParametersUtil.createJobParametersWithIncrementerIfAvailable(PARAMETERS, job);
jobLauncher.run(job, jobParameters).getId();
} catch (NoSuchJobException | JobParametersNotFoundException | JobParametersInvalidException | JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException e) {
e.printStackTrace();
}
}
}
我已经配置了石英以使用JobStoreCMT。我是否必须显式声明一个TransactionManager并将其传递给SchedulerFactoryBean?非常感谢您的帮助。
答案 0 :(得分:2)
如果您遇到与我相似或相同的问题,可以节省其他人在世界各地的时间,这对我有用。
我明确提供了SchedulerFactoryBean的事务管理器。
@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory emf) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(emf);
return transactionManager;
}
@Bean
public SchedulerFactoryBean schedulerFactoryBean(JobFactory jobFactory, PlatformTransactionManager transactionManager)
throws IOException {
SchedulerFactoryBean factory = new SchedulerFactoryBean();
factory.setJobFactory(jobFactory);
factory.setDataSource(dataSource);
factory.setTransactionManager(transactionManager);
factory.setQuartzProperties(quartzProperties());
factory.setGlobalJobListeners(jobsListenerService);
return factory;
}