简体   繁体   English

数据库轮询器Java EE / Spring / Hibernate

[英]Database Poller Java EE/Spring/Hibernate

I am using Java EE with the Spring Framework and Hibernate. 我在Spring Framework和Hibernate中使用Java EE。 I want to write a very simple database poller that does the following 我想编写一个执行以下操作的非常简单的数据库轮询器

  1. Poll the DB every second 每秒轮询数据库
  2. If a certain column in a certain row has the value that I want, stop polling and return success. 如果某行中的某列具有我想要的值,请停止轮询并返回成功。
  3. If polling has gone on for 10 or more seconds, return failure 如果轮询已进行10秒钟或更长时间,则返回失败

I have already looked into Java's ScheduledExecutorService and ExecutorService , and I think that I may be able to do what I want with these. 我已经研究过Java的ScheduledExecutorServiceExecutorService ,并且我认为我可以用这些做我想做的事情。 My question is, is there something built into Spring/Hibernate that can do this more elegantly? 我的问题是, Spring / Hibernate中是否内置了某些功能,可以更优雅地完成此操作?

One alternative is to use the TaskScheduler like this: 一种替代方法是像这样使用TaskScheduler:

1) create a bean with TaskScheduler 1)使用TaskScheduler创建一个bean

@Bean
public TaskScheduler taskScheduler() {
    ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
    threadPoolTaskScheduler.setPoolSize(50);
    threadPoolTaskScheduler.setThreadNamePrefix("Scheduler-Pool-");
    return threadPoolTaskScheduler;
}

2) create a job scheduled with CRON notation 2)使用CRON标记创建计划的作业

ScheduledFuture<?> scheduledFuture = taskScheduler.schedule(() -> aRunnable, new CronTrigger("0 0/5 * * * ?")); // per five seconds

3) when a condition is met or db poll timed out, disable the task 3)当满足条件或数据库轮询超时时,请禁用任务

scheduledFuture.cancel(false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM