Using Hibernate session with quartz

安稳与你 提交于 2019-11-30 19:49:27

One approach is to use a HibernateUtil class which builds the SessionFactory in a static initializer and makes it available via a public static getter. Your Quartz job can create a Session as HibernateUtil.getSessionFactory().getCurrentSession() and use it.

I know this is an old question, but I did a quick Google search, and this was the first hit.

In the quartz job, add this line at the start of the method:

public void execute(JobExecutionContext context) throws JobExecutionException
{
    SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this); //<-- this line

     //...your code here...
}

I apologize if this doesn't fix your specific issue, but I suspect it will catch someone in the future.

Searching for "Quartz Hibernate" returned this. Coming to a different solution (and using Tapestry), I thought I'd share it.

when scheduling the Job:

…
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobDataMap myJobDataMap = new JobDataMap();
myJobDataMap.put("HibernateSessionManager", hibernateSessionManager);
        myJobDataMap.put("PerthreadManager", perThreadManager);
JobDetail job = JobBuilder.newJob(SomeJob.class).withIdentity(
            "SomeJob", "someGroup").setJobData(
            myJobDataMap).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity(
            "Some Trigger", "someGroup").startNow().withSchedule(
            SimpleScheduleBuilder.repeatSecondlyForever(30)).build();
scheduler.scheduleJob(job, trigger);
scheduler.start();
…

and in the Job

public void execute(JobExecutionContext context)
                throws JobExecutionException
{
    JobDataMap jdm = context.getMergedJobDataMap();
    HibernateSessionManager hibernateSessionManager = (HibernateSessionManager) jdm.get("HibernateSessionManager");
    PerthreadManager perThreadManager = (PerthreadManager) jdm.get("PerthreadManager");

    Session session = hibernateSessionManager.getSession();
    //do stuff with session …
    //now clean up, otherwise I ended up with <IDLE> in transactions
    perThreadManager.cleanUp();
}

Hope somebody can use this.

You can look at the below link to see if it gives you a direction to follow. Since you are not using Spring, it might be hard to apply this directly

http://forum.springsource.org/showthread.php?t=12117

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!