*/
public void event(final CometEvent event) throws IOException, ServletException {
final HttpServletRequest request = event.getHttpServletRequest();
final QueueSession session = sessionProvider.getSession(request.getSession());
MessageQueue queue;
switch (event.getEventType()) {
case BEGIN:
boolean post = "POST".equals(request.getMethod());
if ((queue = getQueue(session, !post)) != null) {
synchronized (activeEvents) {
Set<CometEvent> events = activeEvents.get(session);
if (post) {
// do not pause incoming messages.
break;
} else if (queue.messagesWaiting()) {
transmitMessages(event.getHttpServletResponse(), queue);
event.close();
break;
}
if (!queueToSession.containsKey(queue)) {
queueToSession.put(queue, session);
}
if (events == null) {
activeEvents.put(session, events = new HashSet<CometEvent>());
}
if (events.contains(event)) {
break;
} else {
event.setTimeout(30000);
events.add(event);
}
}
} else {
sendDisconnectWithReason(event.getHttpServletResponse().getOutputStream(),
"There is no queue associated with this session.");
}
break;
case END:
event.close();
synchronized (activeEvents) {
Set<CometEvent> evt = activeEvents.get(session);
if (evt != null && !evt.remove(event)) {
return;
}
}
if ((queue = getQueue(session, false)) != null) {
queue.heartBeat();
} else {
return;
}
break;
case ERROR:
log.error("An Error Occured: " + (event != null ? event.getEventSubType() : "<null>"));
queue = getQueue(session, false);
if (queue == null) {
return;
}
synchronized (activeEvents) {
Set<CometEvent> evt = activeEvents.get(session);
if (evt != null && !evt.remove(event)) {
return;
}
}
if (event.getEventSubType() == CometEvent.EventSubType.TIMEOUT) {
if (queue != null) queue.heartBeat();
} else {
if (queue != null) {
queueToSession.remove(queue);
service.getBus().closeQueue(session.getSessionId());
activeEvents.remove(session);