如何通过java监听新的数据库记录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18260593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to listen new db records through java
提问by Nir
Currently I use while(true)
and Thread.sleep()
for checking for new records in the db and execute java code.
目前我使用while(true)
和Thread.sleep()
检查数据库中的新记录并执行java代码。
Here is an 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 the db and get one row from the table by the 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 the code that I use this method. But I dont like the endless loop and check every 10 seconds for new row.
我在代码中使用这种方法的地方很少。但我不喜欢无限循环,每 10 秒检查一次新行。
So what I'm looking for is some kind of listener: once new record has been entered - execute java code. Some of the insert
s executed from the application and some are not.
所以我正在寻找的是某种侦听器:一旦输入了新记录 - 执行 java 代码。有些insert
从应用程序执行,有些则不是。
回答by tFrisch
The Condition Interface would work nicely for your needs. It will give you the granular control you are looking for, and it will avoid the problem of spinning the thread constantly.
条件接口可以很好地满足您的需求。它将为您提供所需的精细控制,并避免不断旋转线程的问题。
http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.htmlhttp://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html
http://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/locks/Condition.html
回答by KyleM
The technique you are using is called polling. You are checking for new records, waiting a set amount of time, then checking again for new records. One good way to respond to new records might be to create a controller that handles inserting new records into the database and force all clients (who update database records) to use the controller to do so. Then the controller can alert you when there is a new record. To facilitate the controller's alerts, you can set up a web service where the controller can contact you.
您正在使用的技术称为轮询。您正在检查新记录,等待一段时间,然后再次检查新记录。响应新记录的一种好方法可能是创建一个控制器来处理将新记录插入数据库并强制所有客户端(更新数据库记录的)使用控制器来执行此操作。然后控制器可以在有新记录时提醒您。为了便于控制器发出警报,您可以设置一个 Web 服务,以便控制器可以与您联系。
I say that this "might" be a good way to do it because creating a controller and a web service is obviously extra work. However, it would make polling unnecessary. If you want to continue using your polling technique, you could make a service (producer) that does the polling and fills a queue with the new results. Your other program (consumer) can then retrieve items from the queue and do something with them.
我说这“可能”是一个很好的方法,因为创建控制器和 Web 服务显然是额外的工作。但是,这将使轮询变得不必要。如果您想继续使用您的轮询技术,您可以创建一个服务(生产者)来进行轮询并用新结果填充队列。然后,您的其他程序(消费者)可以从队列中检索项目并对其进行处理。
回答by Jim Garrison
There is no builtin "update listener" in MySQL (or any SQL database I'm aware of), so you have to build your own.
MySQL(或我知道的任何 SQL 数据库)中没有内置的“更新侦听器”,因此您必须构建自己的。
Notice that in your implementation, if two new rows are added you will handle one, wait 10 seconds, then handle the next one. Your code cannot handle more than one event every 10 seconds.
请注意,在您的实现中,如果添加了两个新行,您将处理一个,等待 10 秒,然后处理下一个。您的代码不能每 10 秒处理一个以上的事件。
What you want to do is separate the polling of the database from the dispatching of the worker threads. Have the polling loop wake up every n seconds, read ALL new records from the database, and add them to a work queue. Have a consumer thread that is waiting on the queue and launches processors as messages appear on the queue. using a thread pool implementation.
您想要做的是将数据库的轮询与工作线程的调度分开。让轮询循环每 n 秒唤醒一次,从数据库中读取所有新记录,并将它们添加到工作队列中。有一个消费者线程正在等待队列并在消息出现在队列上时启动处理器。使用线程池实现。
回答by Martin James
Use a trigger, call a User Defined Function that uses sys_exec() to run an external app that signals an inter-process semaphore. Your listener thread can wait on that and, when signaled, process the new records.
使用触发器,调用用户定义的函数,该函数使用 sys_exec() 运行一个外部应用程序,该应用程序向进程间信号量发出信号。您的侦听器线程可以等待,并在收到信号时处理新记录。
回答by Akrotera
In oracle exists something called database change notification http://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.htmand I just implement a component like yours is there something like that in mysql or what approach you arrived?
在 oracle 中存在一种叫做数据库更改通知http://docs.oracle.com/cd/E11882_01/java.112/e16548/dbchgnf.htm 的东西,我只是实现了一个像你这样的组件在 mysql 中是否有类似的东西或者你用什么方法到达的?
回答by Dila Gurung
@nir, Since there is no mysql database update listener in java so far, so what you can do is, create a database update trigger against the table, the change of which you want to listen. Within the trigger statement or code construct a function.Now from within that function call java function. Java function should be such that it modify some text, say "a". Now register the listener against the change in "a". And within the class implementing the text change listener of "a",put the code you want to execute.
@nir,由于目前java中没有mysql数据库更新监听器,所以你可以做的是,针对表创建一个数据库更新触发器,你想监听它的变化。在触发器语句或代码中构造一个函数。现在从该函数中调用java 函数。Java 函数应该是这样的,它可以修改一些文本,比如“a”。现在针对“a”中的更改注册侦听器。并在实现“a”的文本更改侦听器的类中,放入要执行的代码。