我希望使用Quartz、Spring和Hibernate创建动态作业。用户通过Web服务与此类作业交互:
public class StartJobSpring extends QuartzJobBean {
private String jobId;
private String jobType;
@Autowired
private NoaJobInstancesDAO njiDAO;
@Transactional
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
JobKey key = context.getJobDetail().getKey();
JobDataMap dataMap = context.getMergedJobDataMap();
// some logic
njiDAO.create(instanceUUID, noaJob.getNoaJob(jobId), jobType);
}
}
NoaJobInstancesDAO 是一个简单的 DAO 类,它使用 Hibernate 的 EntityManager:
@Repository
public class NoaJobInstancesDAOHibImpl implements NoaJobInstancesDAO {
@PersistenceContext
private EntityManager entityManager;
@Override
@Transactional
public NoaJobInstanceJPA create(NoaJobInstanceJPA entity) {
entityManager.persist(entity);
return entity;
}
@Override
public void create(String instance_uuid, NoaJobJPA job, String job_type) {
NoaJobInstanceJPA entity = new NoaJobInstanceJPA(instance_uuid, job,
job_type, "CREATED", null, null, "", "N", "N");
this.create(entity);
}
}
问题在于当此任务触发时,会抛出异常:
javax.persistence.TransactionRequiredException: No transactional EntityManager available
我不明白为什么!我在一个管理器类中按照这种方式安排了工作。
JobDetail job = newJob(StartJobSpring.class).withIdentity(//anId)
.setJobData(//aJobMap).build();
getScheduler().getObject().scheduleJob(job, trigger);
调度程序与管理器相连的位置
@Autowired
private ApplicationContext applicationContext;
@Bean
SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource, JpaTransactionManager transactionManager) {
SchedulerFactoryBean bean = new SchedulerFactoryBean();
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
bean.setJobFactory(jobFactory);
bean.setTransactionManager(transactionManager);
return bean;
}
类AutowiringSpringBeanJobFactory与Autowiring相同。
在我看来,调度程序的布线有些问题。事实上,我不明白如何检索应用程序上下文。
编辑1:应用程序上下文似乎被正确实例化了。问题可能不在那里。
编辑2:我正在使用单个配置bean(不是xml文件)。以下是主要方法:
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("package");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.OracleDialect");
jpaProperties.put("hibernate.show_sql", "false");
jpaProperties.put("hibernate.hbm2ddl.auto", "update");
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
public NoaJobInstancesDAO noaJobInstancesDAO() {
NoaJobInstancesDAOHibImpl noaJobInstancesDAO = new NoaJobInstancesDAOHibImpl();
return noaJobInstancesDAO;
}