gpt4 book ai didi

java - 编写查询来删除 3 分钟内未 ping 服务器的节点

转载 作者:行者123 更新时间:2023-12-01 18:56:13 24 4
gpt4 key购买 nike

每 5 秒就有一个节点对服务器执行 ping 操作。如果 ping 成功,节点的时间戳就会更新到服务器数据库中。每 3 分钟后,服务器检查是否有任何早于 3 分钟的时间戳。如果发现任何节点,则会从数据库中删除该节点。

现在问题来了。我无法实现这个查询。例如,我想要以一种相当简单的方式,例如:

// Get the server time-stamp in milliseconds

select LastPingedAt from JustPinged where LastPingedAt > 3 Minutes"

// If it finds any,delete each of them from the database.
<小时/>
The logic :
Let the server's time stamp at the point of checking be 'serverStamp'
Let the node's time stamp (time in milliseconds when it last pinged) in the
database's table be 'nodeStamp'.

If ( serverStamp - nodeStamp > 3 minutes)
// Delete those nodes
If( severStamp - nodeStamp < 3 minutes)
// retain those nodes

我无法设计进一步实现的查询。

在任何时候,JustPinged 表看起来都是这样的:

--------------------------|----------------------
NodesThatJustPinged | LastPingedAt
--------------------------|-----------------------
xxx.xxx.xxx.xxx | 1355406367402
--------------------------|-----------------------
yyy.yyy.yyy.yyy | 1355406421277
--------------------------|-----------------------

时间以毫秒为单位,从 new GregorianCalendar().getTimeInMillis() 开始。

最佳答案

无需获取所有记录并在程序中处理它们。这可以通过单个 SQL 语句来完成。您需要执行的只是这样的语句:

delete from JustPinged where CurrentTime - LastPingedAt > 180000;

因此,您只需要在 Java 中构建字符串,插入当前时间值,然后运行它:

String query = "delete from JustPinged where " +
[your current time variable here] + " - LastPingedAt > 180000;";

your_statement_object.executeQuery(query);

关于java - 编写查询来删除 3 分钟内未 ping 服务器的节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13861264/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com