数据库轮询和调度在Springboot中

huangapple go评论59阅读模式
英文:

Database Polling and Scheduling in Springboot

问题

我需要在一个数据库表(称为T1)上执行轮询操作,并在另一个数据库表(称为T2)上更新状态。每当在表T2中插入数据时,必须安排执行此作业。

我知道Quartz调度程序,但它提供基于时间的调度和轮询。是否有其他适用于这种情况的库?

英文:

I need to perform Polling operation on one of the DB table(say T1) and update status on another DB table(say T2). This Job has to be scheduled whenever there is an insertion in the Table T2.

I am aware of Quartz scheduler but that gives time based scheduling and polling.
Are there any other libraries which would be handy for such cases?

答案1

得分: 2

  1. 获取在变化发生时立即通知
    这意味着要么数据库告诉您有变化发生(通过数据库触发器),要么进行变化的应用程序告诉您有变化发生。
    您提到触发器不是一个选项,因为数据库不在您的控制之下。您还提到变化是由我们的应用程序发起的。因此,一种可能的解决方案是让进行变化的代码通知其他模块有关变化的信息。一种可能性是允许注册回调/处理程序方法。这种模式非常常见,例如,当您在UI按钮上注册一个onClick处理程序时。您在说:“每当发生这种情况时,通知我”(例如,请参阅https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setAction(javax.swing.Action))。

  2. 轮询变化
    这种方法效率较低/资源消耗较大,但可以在选项1)不可行时应用(无法使用DB触发器,变化是通过您无法控制的应用程序的另一部分发生的)。您只需定期查找变化。您可以使用Quartz进行此操作,也可以使用内置类,如java.util.TimerTask / java.util.concurrent.ScheduledExecutorService。

英文:

In principle there are two approaches to your problem:

  1. Get an immediate notification when a change occurs:
    This means that either the database tells you about the change (via a DB trigger) or the application that makes the change tells you about it.
    You said that triggers are not an option, since the DB is not under your control. You also said that the change originates in our application. So a possible solution could be to have the code that makes the change notify other modules about the change. One possibility could be to allow registering a callback / handler method. This pattern is very common, for example when you register an onClick handler on a UI button. You are saying "whenever this happens, call me" (see for example https://docs.oracle.com/javase/7/docs/api/javax/swing/AbstractButton.html#setAction(javax.swing.Action)).

  2. Poll for the change:
    This method is less effective / more resource intensive but can be applied when option 1) is not possible (DB trigger not possible, changes happen through another (part of the) application you do not control). You simply keep looking for the change in regular intervals. You can use Quartz for this or also the built in classes like java.util.TimerTask / java.util.concurrent.ScheduledExecutorService.

huangapple
  • 本文由 发表于 2020年8月8日 23:03:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316848.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定