mysql - How to listen new db records through java -
currently use while(true)
, thread.sleep()
checking new records in db , execute java code.
here example:
public class startcommands implements runnable{ private active_job activejob; runnable execute_command; public startcommands(){ activejobs = new active_job(); } @override public void run(){ int jobid = 0; while(true){ //access db , 1 row table status jobid = activejobs.get(status.new); if (jobid > 0){ activejob.updatestatus(status.init); execute_command = activejob.getcommand(); new thread(execute_command).start(); activejob = new active_job(); jobid = 0; } thread.sleep(10*1000); } } }
i've few places in code use method. dont endless loop , check every 10 seconds new row.
so i'm looking kind of listener: once new record has been entered - execute java code. of insert
s executed application , not.
there no builtin "update listener" in mysql (or sql database i'm aware of), have build own.
notice in implementation, if 2 new rows added handle one, wait 10 seconds, handle next one. code cannot handle more 1 event every 10 seconds.
what want separate polling of database dispatching of worker threads. have polling loop wake every n seconds, read new records database, , add them work queue. have consumer thread waiting on queue , launches processors messages appear on queue. using thread pool implementation.
Comments
Post a Comment