简体   繁体   English

如何设计实时警报系统?

[英]How to design a Real Time Alerting System?

I have an requirement where I have to send the alerts when the record in db is not updated/changed for specified intervals. 当数据库中的记录未按指定间隔更新/更改时,我必须发送警报。 For example, if the received purchase order doesn't processed within one hour, the reminder should be sent to the delivery manager. 例如,如果一小时内未处理收到的采购订单,则应将提醒发送给交货经理。

The reminder/alert should sent exactly at the interval (including seconds). 提醒/警报应准确发送间隔(包括秒)。 If the last modified time is 13:55:45 means, the alert should be triggered 14:55:45. 如果最后修改时间是13:55:45,则警报应在14:55:45触发。 There could be million rows needs to be tracked. 可能需要跟踪一百万行。

The simple approach could be implementing a custom scheduler and all the records will registered with it. 简单的方法可能是实现自定义调度程序,并且所有记录都将在其中注册。 But should poll the database to look for the change every second and it will lead to performance problem. 但是应该每秒查询一次数据库以查找更改,这将导致性能问题。

UPDATE: 更新:

Another basic approach would be a creating a thread for each record and put it on sleep for 1 hour (or) Use some queuing concept which has timeout. 另一种基本方法是为每个记录创建一个线程,并将其置于睡眠状态1小时(或)使用具有超时功能的排队概念。 But still it has performance problems 但仍然存在性能问题

Any thoughts on better approach to implement the same? 有更好的方法来实现相同的想法吗?

probably using internal JMS queue would be better solution - for example you may want to use scheduled message feature http://docs.jboss.org/hornetq/2.2.2.Final/user-manual/en/html/examples.html#examples.scheduled-message with hornetq. 可能使用内部JMS队列将是更好的解决方案-例如,您可能要使用计划的消息功能http://docs.jboss.org/hornetq/2.2.2.Final/user-manual/en/html/examples.html# hornetq的examples.scheduled消息

You can ask broker to publish alert message after exactly 1h. 您可以要求经纪人在正好1小时后发布警报消息。 From the other hand during processing of some trading activity you can manually delete this message meaning that the trade activity has been processed without errors. 另一方面,在处理某些交易活动时,您可以手动删除此消息,表示该交易活动已被正确处理。

为每个提醒使用计时器。即,如果最后修改的时间是17:49:45,则应该在18:49:45触发警报,只需为每个将在一个小时后调用的任务创建一个动态计时器时间表。

It is not possible in Java, if you really insist on the "Real-timeness". 如果您真的坚持“实时性”,那么在Java中是不可能的。 In Java you may encouter Garbage collector's stop-the-world phase and you can never guarantee the exact time. 在Java中,您可能会使垃圾回收器的进入世界的阶段陷入困境,并且您永远无法保证确切的时间。

If the approximate time is also permissible, than use some kind of scheduled queue as proposed in other answers, if not, than use real-time Java or some native call. 如果大约时间也是允许​​的,则使用其他答案中建议的某种计划的队列,如果不允许,则使用实时Java或某些本机调用。

If we can assume that the orders are entered with increasing time then: 如果我们可以假设输入的订单时间增加,那么:

You can use a Queue with elements that have the properties time-of-order and order-id . 您可以对具有属性time-of-orderorder-id元素使用Queue

Each new entry that is added to the DB is also enqueued to this Queue . 添加到数据库的每个新条目也都排队到此Queue

You can check the element at the start of the Queue each minute. 您可以每分钟在Queue开始处检查一次元素。

When checking the element at the start of the Queue , if an hour has passed from the time-of-order , then search for the entry with order-id in the DB. Queue的开头检查元素时,如果距离time-of-order过去了一个小时,则在数据库中搜索具有order-id的条目。

If found and was not updated then send a notification, else dequeue it from the Queue . 如果找到并且未更新,则发送通知,否则从Queue中将其出队。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM