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