Commit 34646e33 by Tuomas Riihimäki

Actually put entries in to the queue

1 parent 4d268fed
......@@ -16,6 +16,8 @@ import java.util.concurrent.atomic.AtomicLong;
import javax.ejb.Asynchronous;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.Singleton;
import org.slf4j.Logger;
......@@ -45,7 +47,9 @@ public class QueueBean implements QueueBeanLocal {
private final Map<EventMap, MapQueue> mapqueues = new HashMap<>();
private static class MapQueue {
private int reservingSize = 4;
private class MapQueue {
// private final Set<MapReservationQueueEntry> reserving = new HashSet<>();
private final Set<EventUser> reserving = Collections.newSetFromMap(new ConcurrentHashMap<EventUser, Boolean>());
private final LinkedBlockingQueue<EventUser> queue = new LinkedBlockingQueue<>();
......@@ -88,6 +92,18 @@ public class QueueBean implements QueueBeanLocal {
}
public boolean isReserving(EventUser e) {
// Check queue size and add entry to queue
if (reserving.size() < reservingSize) {
synchronized (queue) {
if (reserving.size() < reservingSize) {
EventUser queEntry = queue.poll();
if (queEntry != null) {
reserving.add(queEntry);
}
}
}
}
queEntries.get(e).setSeenTime(new Date());
return reserving.contains(e);
......@@ -129,12 +145,15 @@ public class QueueBean implements QueueBeanLocal {
private EventBeanLocal eventbean;
private AtomicLong nextReservingTimeoutCheck = new AtomicLong();
@Lock(LockType.READ)
public boolean isReserving(EventMap map, EventUser user) {
// If queue is not enabled, user can always reserve
if (!isQueueEnabled())
return true;
boolean ret = getMapque(map).isReserving(user);
// Do some housekeeping, but only on each 120
long now = System.currentTimeMillis();
long nextTime = nextReservingTimeoutCheck.get();
// Update next checktime to 120 seconds in to the future, so we should have plenty of time
......@@ -150,11 +169,15 @@ public class QueueBean implements QueueBeanLocal {
}
@Asynchronous
public void checkReservingTimeouts() {
private void checkReservingTimeouts() {
try {
final long oldTime = nextReservingTimeoutCheck.get();
for (MapQueue m : mapqueues.values()) {
m.timeoutEntries();
}
// DO housekeeping every 10 seconds.
nextReservingTimeoutCheck.compareAndSet(oldTime, System.currentTimeMillis() + 1000*10);
} catch (Throwable t) {
logger.warn("Exception while checking reservingTimeouts");
}
......@@ -170,6 +193,7 @@ public class QueueBean implements QueueBeanLocal {
return ret;
}
@Lock(LockType.READ)
public boolean isQueueEnabled() {
boolean ret = false;
LanEventProperty mapque = eventbean.getProperty(LanEventPropertyKey.MAP_QUEUE);
......@@ -179,12 +203,14 @@ public class QueueBean implements QueueBeanLocal {
}
@Lock(LockType.READ)
public MapReservationQueueEntry remove(EventMap map, EventUser user) {
MapQueue queue = getMapque(map);
return queue.remove(user);
}
@Override
@Lock(LockType.READ)
public MapReservationQueueEntry enterQueue(EventMap map, EventUser user) {
MapQueue queue = getMapque(map);
return queue.enter(user);
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!