数据库触发java,根据数据库更新和时间间隔触发Java程序

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

N new inserts in a MySQL table

Every 5 minutes interval.

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

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;

(or whatever)

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

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.

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".

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

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.

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).

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).

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值