package org.jboss.mq.server;
import java.util.HashMap;
import java.util.Iterator;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.TemporaryQueue;
import javax.jms.TemporaryTopic;
import javax.jms.Topic;
import org.jboss.logging.Logger;
import org.jboss.mq.AcknowledgementRequest;
import org.jboss.mq.ConnectionToken;
import org.jboss.mq.DurableSubscriptionID;
import org.jboss.mq.SpyDestination;
import org.jboss.mq.SpyMessage;
import org.jboss.mq.Subscription;
import org.jboss.mq.TransactionRequest;
import org.jboss.mq.il.jvm.JVMClientIL;
public class ClientMonitorInterceptor extends JMSServerInterceptorSupport {
static protected Logger log =
Logger.getLogger(ClientMonitorInterceptor.class);
HashMap clients = new HashMap();
private static class ClientStats {
private long lastUsed = System.currentTimeMillis();
boolean disconnectIfInactive=true;
}
public void disconnectInactiveClients(long disconnectTime) {
log.debug("Checking for timedout clients.");
Iterator i = clients.keySet().iterator();
while (i.hasNext()) {
ConnectionToken dc = (ConnectionToken) i.next();
ClientStats cs = (ClientStats) clients.get(dc);
if( cs.disconnectIfInactive && cs.lastUsed < disconnectTime ) {
try {
log.debug("Disconnecting client due to inactivity timeout: "+dc);
connectionClosing(dc);
} catch (Throwable e ) {
}
}
}
}
public ClientStats getClientStats(ConnectionToken dc) throws JMSException {
ClientStats cq = (ClientStats) clients.get(dc);
if (cq == null) {
cq = new ClientStats();
if( dc.clientIL instanceof JVMClientIL )
cq.disconnectIfInactive = false;
synchronized(clients) {
HashMap m = new HashMap(clients);
m.put(dc, cq);
clients=m;
}
}
return cq;
}
public TemporaryTopic getTemporaryTopic(ConnectionToken dc)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().getTemporaryTopic(dc);
}
public TemporaryQueue getTemporaryQueue(ConnectionToken dc)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().getTemporaryQueue(dc);
}
public void connectionClosing(ConnectionToken dc) throws JMSException {
synchronized (clients) {
HashMap m = new HashMap(clients);
m.remove(dc);
clients = m;
}
getNext().connectionClosing(dc);
}
public void addMessage(ConnectionToken dc, SpyMessage message)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().addMessage(dc, message);
}
public Queue createQueue(ConnectionToken dc, String dest)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().createQueue(dc, dest);
}
public Topic createTopic(ConnectionToken dc, String dest)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().createTopic(dc, dest);
}
public void deleteTemporaryDestination(
ConnectionToken dc,
SpyDestination dest)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().deleteTemporaryDestination(dc, dest);
}
public void transact(ConnectionToken dc, TransactionRequest t)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().transact(dc, t);
}
public void acknowledge(ConnectionToken dc, AcknowledgementRequest item)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().acknowledge(dc, item);
}
public SpyMessage[] browse(
ConnectionToken dc,
Destination dest,
String selector)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().browse(dc, dest, selector);
}
public SpyMessage receive(ConnectionToken dc, int subscriberId, long wait)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().receive(dc, subscriberId, wait);
}
public void setEnabled(ConnectionToken dc, boolean enabled)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().setEnabled(dc, enabled);
}
public void unsubscribe(ConnectionToken dc, int subscriptionId)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().unsubscribe(dc, subscriptionId);
}
public void destroySubscription(
ConnectionToken dc,
DurableSubscriptionID id)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().destroySubscription(dc, id);
}
public void subscribe(
org.jboss.mq.ConnectionToken dc,
org.jboss.mq.Subscription s)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().subscribe(dc, s);
}
public void ping(ConnectionToken dc, long clientTime) throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
getNext().ping(dc, clientTime);
}
public Subscription getSubscription(ConnectionToken dc, int subscriberId)
throws JMSException {
getClientStats(dc).lastUsed = System.currentTimeMillis();
return getNext().getSubscription(dc, subscriberId);
}
}