使用Java(Spring)从数据库中安排任务的最佳方法

3

我正在寻找用于调度数据库中一组任务的框架

JPA实体长这样

@Entity
class Task extends Model {
    @NotNull
    @Min(1L)
    @Column(name = "interval_ms", nullable = false)
    Integer interval

    @NotNull
    String payload  

    @NotNull
    Boolean enabled
}

@Entity
class TaskResult extends Model {
    @ManyToOne(optional = false)
    Task task

    @Column(nullable = false)
    Boolean result

    @Column(nullable = false)
    String message
}

任务应该根据“interval”字段中定义的时间间隔执行,执行结果应该写入TaskResult表。

任务的目的是进行GET或POST请求,因此需要对请求进行池化,以避免大量任务同时开始执行的情况。

我正在使用Spring Boot。

在这里,最佳实践是什么?


为每个任务创建一个单独/独立的线程是否存在问题? - nabeel
Quartz(https://quartz-scheduler.org/)很好地集成了沙箱,并与数据库集成。 你看过它吗? - jny
1个回答

5
如果您正在使用Spring Boot,可以使用TaskExecutor bean并配置池大小(http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-executor-usage),然后使用TaskScheduler来定义任务何时运行。事件参数的值取自数据库(即Object)。
我曾经创建过调度程序,但我使用了Quartz。我创建了一个每隔1分钟触发一次的新工作,并在数据库中搜索EmailQueue,如果找到电子邮件,则尝试发送它,当发送出现错误时,它不会删除队列中的电子邮件,并在日志表中写入有关错误的详细信息。调度程序(1分钟)是从数据库设置的。对于您的情况,您应该使用: newJob(QuartzJob.class).withIntervalInMilliseconds。 http://quartz-scheduler.org/api/2.2.0/org/quartz/SimpleScheduleBuilder.html#withIntervalInMilliseconds(long)
@Service
public class MyQuartz implements InitializingBean, DisposableBean {

    @Autowired
    StdSchedulerFactory stdSchedulerFactory;
    @Autowired
    TableConfiguration tableConfiguration;
    Scheduler sched = null;
    JobDetail job, job2;
    JobDetail[] jobs = new JobDetail[2];

    public void initIt() throws Exception {

        try {
            System.out.println("Shedulling a job...");
            sched = stdSchedulerFactory.getScheduler();
        } catch (SchedulerException e) {
            System.err.println("Could not create scheduler.");
            e.printStackTrace();
        }
        try {
            System.out.println("Starting scheduler");
            sched.start();
        } catch (SchedulerException e) {
            System.err.println("Could not start scheduler.");
            e.printStackTrace();
        }
        // define the job and tie it to our QuartzJob class
        job = newJob(QuartzJob.class).withIdentity("myJob", "group1").build();
        job2 = newJob(QuartzCronJob.class).withIdentity("myCronJob", "group2")
                .build();
        jobs[0] = job;
        // .. here I have more jobs ...
        // Trigger the job to run now, and then every 5 minutes

        Trigger trigger = newTrigger()
                .withIdentity("myTrigger", "group1")
                .startNow()
                .withSchedule(
                        simpleSchedule().withIntervalInMinutes(
                                tableConfiguration.getTimeInterval())
                                .repeatForever()).build();

        // ... more triggers also here
        // Tell quartz to schedule the job using our trigger
        try {
            sched.scheduleJob(job, trigger);
            System.out.println("..job schedulled.");
        } catch (SchedulerException e) {
            System.err.println("Could not schedulle a job.");
            e.printStackTrace();
        }

    }

    @Override
    public void destroy() throws Exception {

        try {
            System.out.println("Stopping scheduler...");
            for (int i = 0; i < jobs.length; i++) { // Destroying all jobs
                sched.deleteJob(jobs[i].getKey());
            }
            sched.shutdown(true); // Waiting for jobs to complete.
            System.out.println("...scheduler Terminated. Good Bye.");
        } catch (SchedulerException e) {
            System.err.println("ERROR, scheduler cannot be stopped.");
            e.printStackTrace();
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {

    }
}

并且

@Service
@Transactional
public class QuartzCronJob implements Job {
    @Override
    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = new GregorianCalendar();
        String schedulled = sdf.format(calendar.getTime());

        System.out.println("LIST now " + schedulled);
        List<EmailQueue> emails = emailQueueDao.findAllForToday(schedulled);

        if (emails != null) {
            for (EmailQueue email : emails) {
                // Send email:
                try {
                    sendmail.sendNotification(email.getFrom(), email.getTo(),
                            email.getSubject(), email.getMessage(), "Sched.");
                    // Delete email from queue:
                    emailQueueDao.delete(email.getId());
                    System.out.println("Email sucessfully sent and deleted.");
                } catch (Exception e) {
                    sendmail.logEmail(LoggerSeverity.ERROR,
                            "Could not send schedulled email", "Sched.");
                    System.err.println("Could not send schedulled email");
                }
            }
        } 
        // ... more code here, this is just a working sample...
    }
}

我的代码没有使用连接池,但是由于每分钟只运行一次,所以我从数据库中检索不到超过3封电子邮件 :)


如何在集群环境中处理它?如何避免碰撞? - navaltiger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接