ChestShop-3/com/Acrobot/ChestShop/DB/Queue.java

46 lines
1.2 KiB
Java
Raw Normal View History

2011-05-29 13:25:25 +02:00
package com.Acrobot.ChestShop.DB;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Properties;
2011-05-29 13:25:25 +02:00
import javax.persistence.OptimisticLockException;
import java.util.List;
2012-06-25 17:16:57 +02:00
import java.util.concurrent.ConcurrentLinkedQueue;
2011-05-29 13:25:25 +02:00
/**
* @author Acrobot
*/
public class Queue implements Runnable {
2012-06-25 17:16:57 +02:00
private static final ConcurrentLinkedQueue<Transaction> queue = new ConcurrentLinkedQueue<Transaction>();
2011-05-29 13:25:25 +02:00
public static void addToQueue(Transaction t) {
2011-05-29 13:25:25 +02:00
queue.add(t);
}
public synchronized void run() {
if (Properties.RECORD_TIME_TO_LIVE != -1)
deleteOld();
ChestShop.getDB().save(queue);
2011-05-29 13:25:25 +02:00
queue.clear();
}
2012-04-19 15:50:05 +02:00
public synchronized static boolean deleteOld() {
try {
ChestShop.getDB().delete(getOld());
return true;
} catch (OptimisticLockException ex) {
return false;
}
}
public static List getOld() throws OptimisticLockException {
return ChestShop
.getDB()
.find(Transaction.class)
.where()
.lt("sec", (System.currentTimeMillis() / 1000L) - Properties.RECORD_TIME_TO_LIVE)
.findList();
}
2011-05-29 13:25:25 +02:00
}