aioobeは、この種の問題を解決するためのいくつかの優れたリファレンスを提供しました。ただし、JVMをシャットダウンして再起動する必要がある場合にタイマーやその他の一時的なものが消えることを心配せずに問題を解決したい場合は、次のような簡単なアプローチも検討することをお勧めします。
public class PNRExpirationThread extends Thread {
@Override
public void run() {
while (true) { //or while !stop, or while Server.isRunning(), you get the idea
try {
Thread.sleep(30000); //wait 30 seconds; adjust this to your liking
//it's pseudo-SQL, but you get the idea; I'm assuming your data model has the required fields for this to work
Database.executeTxn("DELETE FROM pnrRecords WHERE NOW() - createDate > 12h AND confirmed = 0");
}
catch (Throwable ignored) {
}
}
}
}
次に、サーバーの起動/初期化ルーチンに次のようなコードを追加します。
//keep a reference to this if you want to terminate the thread gracefully at shutdown time
new PNRExpirationThread().start();
次に、プラットフォームは30秒間隔で12時間より古いレコードを自動的に照会して削除します。