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

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

问题描述

我想要一种机制,可以根据 2 个条件启动一个 java 程序(相当大的一个):

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天全站免登陆