何らかの理由で、プロジェクトで kestrel キューを使用する予定です。主な問題は、CPU 使用率が低く、効果的にキューからデータをフェッチする方法です。フェッチするために実装した方法は、キューからのデータのフェッチに 5 回以上失敗した場合、CPU 使用率を下げるためにスレッドを 100 ミリ秒スリープさせることです。
while (running) {
try {
LoginLogQueueEntry data = kestrelQueue.fetch();
if (null != data && data.isLegal()) {
entryCacheList.add(data); //add the data to the local caceh
resetStatus();
} else {
failedCount++;
//if there is no data in the kestrel and the local cache is not empty, insert the data into mysql database
if (failedCount == 1 && !entryCacheList.isEmpty()) {
resetStatus();
insertLogList(entryCacheList); // insert current data into database
entryCacheList.clear(); //empty local cache
}
if (failedCount >= 5 && entryCacheList.isEmpty()) {
//fail 5 times. Sleep current thread.
failedCount = 0;
Thread.sleep((sleepTime + MIN_SLEEP_TIME) % MAX_SLEEP_TIME);
}
}
//Insert 1000 rows once
if (entryCacheList.size() >= 1000) {
insertLogList(entryCacheList);
entryCacheList.clear();
}
} catch (Exception e) {
logger.warn(e.getMessage());
}
他に良い方法はありますか?私が考える完璧な方法は、データを取得してフェッチしたことをキューがワーカーに通知できることです。