Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mysql - Triggering a Java program based on database updates and time interval

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

  1. N new inserts in a MySQL table
  2. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...