2011-05-29 13:25:25 +02:00
|
|
|
package com.Acrobot.ChestShop.DB;
|
|
|
|
|
|
|
|
import com.Acrobot.ChestShop.ChestShop;
|
2011-06-09 22:54:01 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Config;
|
2011-06-11 17:36:55 +02:00
|
|
|
import com.Acrobot.ChestShop.Config.Property;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2011-09-25 16:17:44 +02:00
|
|
|
import javax.persistence.OptimisticLockException;
|
2011-08-13 12:08:34 +02:00
|
|
|
import java.util.ArrayList;
|
2012-06-08 15:28:36 +02:00
|
|
|
import java.util.Collections;
|
2012-01-09 22:39:38 +01:00
|
|
|
import java.util.List;
|
2011-05-29 13:25:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Acrobot
|
|
|
|
*/
|
|
|
|
public class Queue implements Runnable {
|
2012-06-08 15:28:36 +02:00
|
|
|
private static final List<Transaction> queue = Collections.synchronizedList(new ArrayList<Transaction>());
|
2011-05-29 13:25:25 +02:00
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public static void addToQueue(Transaction t) {
|
2011-05-29 13:25:25 +02:00
|
|
|
queue.add(t);
|
|
|
|
}
|
|
|
|
|
2012-04-07 12:41:20 +02:00
|
|
|
public synchronized void run() {
|
2012-03-01 22:03:59 +01:00
|
|
|
if (Config.getInteger(Property.RECORD_TIME_TO_LIVE) != -1)
|
|
|
|
deleteOld();
|
2011-08-13 12:08:34 +02:00
|
|
|
|
2012-01-09 22:39:38 +01:00
|
|
|
ChestShop.getDB().save(queue);
|
2011-05-29 13:25:25 +02:00
|
|
|
queue.clear();
|
|
|
|
}
|
2012-01-09 22:39:38 +01:00
|
|
|
|
2012-04-19 15:50:05 +02:00
|
|
|
public synchronized static boolean deleteOld() {
|
2012-01-09 22:39:38 +01:00
|
|
|
try {
|
|
|
|
ChestShop.getDB().delete(getOld());
|
|
|
|
return true;
|
|
|
|
} catch (OptimisticLockException ex) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 15:28:36 +02:00
|
|
|
public static List getOld() throws OptimisticLockException {
|
2012-01-09 22:39:38 +01:00
|
|
|
return ChestShop
|
|
|
|
.getDB()
|
|
|
|
.find(Transaction.class)
|
|
|
|
.where()
|
2012-02-16 19:09:37 +01:00
|
|
|
.lt("sec", (System.currentTimeMillis() / 1000L) - Config.getInteger(Property.RECORD_TIME_TO_LIVE))
|
2012-01-09 22:39:38 +01:00
|
|
|
.findList();
|
|
|
|
}
|
2011-05-29 13:25:25 +02:00
|
|
|
}
|