调度持久实体

发布于 2024-10-31 11:37:30 字数 293 浏览 4 评论 0 原文

假设我有一些带有 CronExpression 字段的数据库实体:

@Entity
@Table(name = "job")
public class Job {
    ...
    private CronExpression cronExpression;

}

将其放入石英计划的最佳方法是什么?我使用 Spring3 和 Hibernate。基本上我可以在我的 DAO 中安排它 - 任何时候创建或更新作业 - 但我还需要在应用程序启动时安排所有现有的作业..

感谢您的建议!

Let's say I have some DB entity with a CronExpression field:

@Entity
@Table(name = "job")
public class Job {
    ...
    private CronExpression cronExpression;

}

What is the best approach to put it onto quartz schedule? I use Spring3 and Hibernate. Basically I could schedule it in my DAO - anytime Job is created or updated - but I would also need to schedule all existed job at application start-up..

Thanks for your advices!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳 2024-11-07 11:37:30

您需要 DAO/Repository 来从存储中获取所有 cronExpression。我在内存中创建 DAO

@Repository
public class JobEntityDao {
    public List<JobEntity> findAll() {
        List<JobEntity> list = new ArrayList<JobEntity>();

        JobEntity job1 = new JobEntity("0 0 12 * * ?");
        JobEntity job2 = new JobEntity("0 15 10 ? * *");
        JobEntity job3 = new JobEntity("0 15 10 * * ?");

        list.add(job1);
        list.add(job2);
        list.add(job3);

        return list;
    }
}

和组件来根据您的 cronExpression 创建quartz调度程序。我称之为 QuartzExecutor,

@Service
public class QuartzExecutor {


    private JobEntityDao jobEntityDao;

    @Autowired 
    public QuartzExecutor(JobEntityDao jobEntityDao) throws ParseException, SchedulerException {
        this.jobEntityDao = jobEntityDao; 
        init();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void init() throws ParseException, SchedulerException {
        List<JobEntity> jobEntities = jobEntityDao.findAll();

        for (JobEntity jobEntity : jobEntities) {
            System.out.println(jobEntity.cronExpression);

            RunMeTask task = new RunMeTask();

            //specify your sceduler task details
            JobDetail job = new JobDetail();
            job.setName("runMeJob");
            job.setJobClass(RunMeJob.class);

            Map dataMap = job.getJobDataMap();
            dataMap.put("runMeTask", task);

            //configure the scheduler time
            CronTrigger trigger = new CronTrigger();
            trigger.setName("runMeJobTesting");
            trigger.setCronExpression(jobEntity.cronExpression);

            //schedule it
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        }
    }

}

您可以从 RunMeJob 和 RunMeTask 代码rel="nofollow">http://www.mkyong.com/java/quartz-scheduler-example/
我知道课程设计不好,但我关心的是尽力解决你的问题。

这是您要找的吗?

You need DAO/Repository to get all cronExpression from your storage. I create in memory DAO

@Repository
public class JobEntityDao {
    public List<JobEntity> findAll() {
        List<JobEntity> list = new ArrayList<JobEntity>();

        JobEntity job1 = new JobEntity("0 0 12 * * ?");
        JobEntity job2 = new JobEntity("0 15 10 ? * *");
        JobEntity job3 = new JobEntity("0 15 10 * * ?");

        list.add(job1);
        list.add(job2);
        list.add(job3);

        return list;
    }
}

And Component to create quartz scheduler based on your cronExpression. I call it QuartzExecutor

@Service
public class QuartzExecutor {


    private JobEntityDao jobEntityDao;

    @Autowired 
    public QuartzExecutor(JobEntityDao jobEntityDao) throws ParseException, SchedulerException {
        this.jobEntityDao = jobEntityDao; 
        init();
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    private void init() throws ParseException, SchedulerException {
        List<JobEntity> jobEntities = jobEntityDao.findAll();

        for (JobEntity jobEntity : jobEntities) {
            System.out.println(jobEntity.cronExpression);

            RunMeTask task = new RunMeTask();

            //specify your sceduler task details
            JobDetail job = new JobDetail();
            job.setName("runMeJob");
            job.setJobClass(RunMeJob.class);

            Map dataMap = job.getJobDataMap();
            dataMap.put("runMeTask", task);

            //configure the scheduler time
            CronTrigger trigger = new CronTrigger();
            trigger.setName("runMeJobTesting");
            trigger.setCronExpression(jobEntity.cronExpression);

            //schedule it
            Scheduler scheduler = new StdSchedulerFactory().getScheduler();
            scheduler.start();
            scheduler.scheduleJob(job, trigger);
        }
    }

}

you can get RunMeJob and RunMeTask code from http://www.mkyong.com/java/quartz-scheduler-example/.
I Know the class design is not good, but my concern is try to solve your problem.

Is this what you are looking for ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文