根据数据库更新和时间间隔触发Java程序 [英] Triggering a Java program based on database updates and time interval

查看:276
本文介绍了根据数据库更新和时间间隔触发Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一种机制来启动Java程序(相当大的一个程序),具体取决于2个条件:

I want a mechanism that will start a java program ( quite a big one ) depending on 2 conditions:

  1. MySQL表中有N个新插入
  2. 每5分钟间隔一次.

我知道我可以通过crontab或使用Timer或使用存储过程等来做到这一点.

I know that I can do this through crontab or using Timer or using Stored Procedure etc.

我的计划是编写一个Java类(我最熟悉),侦听器具有两个并行的线程-数据库侦听器和时间侦听器线程中的每一个都监视这些条件之一.如果回答是,则父类将启动一个新线程来运行本程序.

My plan is to write a Java class ( I am most familiar with ), Listener having two threads in parallel - Database Listener and Time listener threads each of them monitoring one of these conditions. If one says, yes, the parent class will start a new thread to run the Program.

我觉得这将是一个沉重的负担.我还有其他选择吗?

I feel that it will be a heavy weight program. Is there some other option that I am overlooking at?

推荐答案

写一个作业.让它定期执行.

Write a single job. Have it execute regularly.

有效地,您将执行以下操作:

Effectively, you'll be doing some something of the nature of:

SELECT count(*) FROM table WHERE new = 1;

(或其他)

每5秒,10秒运行一次,这取决于您的活动.

Run that every second, 5 seconds, 10 seconds, whatever seems reasonable based on your activity.

当count == N时,运行您的进程.如果自上次运行以来的时间" == 5分钟,请运行您的流程.

When count == N, run your process. When "time since last run" == 5 minutes, run your process.

过程是相同的,只是使用两个条件更频繁地检查它.

The process is the same, you just check it more often with the two criteria.

这提供了一个优点,即您不会在该作业被解雇两次的情况下出现流氓竞争状况(因为作业A发现恰好距离上一个作业运行的时间只有5分钟).很少,是的,但是种族条件似乎总是在积极地寻求从未发生"的罕见"事件.

This offers an advantage that you won't get rogue race condition where the job fires TWICE (because Job A found the insert count that just-so-happens to have been 5 minutes from when the last job ran). Rare, yes, but race conditions always seem to actively seek "rare" events that "never happen".

对于调度,crontab很容易,因为您不必维护进程,保持活动状态,守护进程等.

As for scheduling, a crontab is easy because you don't have to maintain your process, keep it alive, daemonize, etc. etc.

如果您已经在运行时间较长的容器(应用服务器,tomcat等)中运行,则该问题已经解决,您可以利用它.

If you're already running in a long running container (app server, tomcat, etc.) then that problem is already solved and you can just leverage that.

cron的缺点是它的粒度,它最多每分钟运行一次.如果时间太长,它将无法为您服务.但是,如果可以的话,那么一个简单的过程就可以发光,检查并退出,这才是真正的价值.当然,它必须以某种方式保持其状态(例如,它可以在作业日志中查看以查看上一个作业的运行时间).

Downside of cron is it's granularity, it only runs at most every minute. If that too long, it won't work for you. But if it's ok, then there's real value in having a simple process that just lights up, does it's check, and quits. Of course, it will have to persist it's state somehow (it could look in a job log to see when the last job ran, for example).

在Java中,有很多选项:原始线程,睡眠,计时器,ScheduledExecutorService,诸如Quartz,EJB计时器bean(如果您正在运行Java EE容器)之类.

Within java, there are lots of options: raw threads, sleeping, Timers, ScheduledExecutorService, something like Quartz, EJB Timer beans (if you're running a Java EE container).

但是,我是KISS的粉丝.如果cron工作可以做到,就让它做一次.

But, I'm a KISS fan. If a cron job can do it, let it, and do it once.

这篇关于根据数据库更新和时间间隔触发Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆