在Spring3+Hibernate4环境中配置Quartz定时器,注入sessionFactory
来源:互联网 发布:sql 注入漏洞 关键字 编辑:程序博客网 时间:2024/06/04 17:52
最近在做javaweb 开发,碰到一个需求,需要在项目中设定一个定时器将数据库中符合条件的数据删除,那么怎么做呢?Google了很久,找到很多办法,但是这些方法都没有提到如何在定时器中注入sessionFactory。找到的也是在hibernate3中配置,方法并不适用于hibernate4。现将方法记录,方便自己日后需要。引用的jar包为quartz-2.2.1.jar,quartz-jobs-2.2.1.jar,spring-context-support-3.2.3.RELEASE.jar。如果我们不注入sessionFactory,仅仅注入service,那么当我们利用service访问dao层时,我们是没有办法访问数据库的。
不多说,上代码,在Spring的配置文件applicationContext.xml中添加
<!-- 调度器 --><bean id="doJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="durability" value="true"></property><property name="jobClass" value="com.crowd.action.task.TaskJob" /><property name="jobDataAsMap"> <map> <entry key="contributorJobService" value-ref="contributorJobService"></entry> <entry key="sessionFactory" value-ref="sessionFactory"></entry> </map></property></bean> <!-- 触发器 --><bean id="cronTriggerPunch" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="doJob" /> <!-- 1 hour --><property name="cronExpression" value="0 0 0/1 * * ?" /> </bean> <!-- 配置调度工厂 --><bean id= "startQuertz" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="cronTriggerPunch" /> </list> </property> </bean>添加具体的实现定时器方法:
public class TaskJob extends QuartzJobBean{ private ContributorJobService contributorJobService; private SessionFactory sessionFactory; private static final Logger log = Logger.getLogger(TaskJob.class); @Overrideprotected void executeInternal(JobExecutionContext ctx)throws JobExecutionException {// TODO Auto-generated method stubSessionHolder sessionHolder =(SessionHolder) TransactionSynchronizationManager.getResource(getSessionFactory()); boolean existingTransaction = sessionHolder != null; Session session; if (existingTransaction) { log.error("Found thread-bound Session for HibernateInterceptor");session = sessionHolder.getSession();} else {session = openSession();TransactionSynchronizationManager.bindResource(getSessionFactory(), new SessionHolder(session));} try { executeTransactional(ctx); } catch (HibernateException ex) { ex.printStackTrace(); throw ex; } finally { if (existingTransaction) { log.debug("Not closing pre-bound Hibernate Session after TransactionalQuartzTask"); } else { SessionFactoryUtils.closeSession(session); TransactionSynchronizationManager .unbindResource(getSessionFactory()); } } }public void executeTransactional(JobExecutionContext ctx) throws JobExecutionException { //每隔一个小时扫描一下数据库相关表log.debug("成功删除 "+contributorJobService.DeleteAnswerWithoutContent()+"条无答案内容的工作数据"); } protected Session openSession() throws DataAccessResourceFailureException {try {Session session = getSessionFactory().openSession();session.setFlushMode(FlushMode.MANUAL);return session;}catch (HibernateException ex) {throw new DataAccessResourceFailureException("Could not open Hibernate Session", ex);}}public SessionFactory getSessionFactory() {return sessionFactory;}public void setSessionFactory(SessionFactory sessionFactory) {this.sessionFactory = sessionFactory;}public void setContributorJobService(ContributorJobService contributorJobService) {this.contributorJobService = contributorJobService;}}这样子,我们就可以相关的service方法操作数据库啦。
1 0
- 在Spring3+Hibernate4环境中配置Quartz定时器,注入sessionFactory
- hibernate4 spring3 sessionFactory
- Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
- Spring3中定时器的配置使用之Quartz
- Spring3中定时器的配置使用之Quartz
- Spring3中定时器的配置使用之Quartz
- Spring3中定时器的配置使用之Quartz
- Hibernate4中获取SessionFactory
- Hibernate4与spring3 sessionFactory().getCurrentSession() 空指针
- Hibernate4+Spring3 配置数据源
- Spring3+hibernate4整合配置
- 配置Spring3+SpringMVC+Hibernate4
- Spring3 集成 Hibernate4 配置
- spring中配置quartz定时器
- spring中配置quartz定时器
- Hibernate4中SessionFactory已过时
- Spring3与Hibernate4整合配置
- Struts2+Spring3+Hibernate4+JavaEE6 配置
- POJ 3279 + UVA 11464 (二维翻转水题)
- project euler 14
- pat 1068 Find More Coins (30)
- 【以太网数据包】DNS数据包
- project euler 15
- 在Spring3+Hibernate4环境中配置Quartz定时器,注入sessionFactory
- PAT甲级-1004
- OpenJudge_P1760 菲波那契数列(2)(递推)
- project euler 16
- 矩阵的转置
- 8086汇编笔记(基础知识与寄存器)
- 追女孩的心得
- 图像细化、骨架提取
- LeetCode 之 Add Binary