KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mq > server > ClientMonitorInterceptor


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.mq.server;
23
24 import java.util.Iterator JavaDoc;
25
26 import javax.jms.Destination JavaDoc;
27 import javax.jms.JMSException JavaDoc;
28 import javax.jms.Queue JavaDoc;
29 import javax.jms.TemporaryQueue JavaDoc;
30 import javax.jms.TemporaryTopic JavaDoc;
31 import javax.jms.Topic JavaDoc;
32
33 import org.jboss.mq.AcknowledgementRequest;
34 import org.jboss.mq.ConnectionToken;
35 import org.jboss.mq.DurableSubscriptionID;
36 import org.jboss.mq.SpyDestination;
37 import org.jboss.mq.SpyMessage;
38 import org.jboss.mq.Subscription;
39 import org.jboss.mq.TransactionRequest;
40 import org.jboss.mq.il.jvm.JVMClientIL;
41
42 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
43
44 /**
45  * A pass through Interceptor, which keeps track of when a
46  * client was last active. If a client is inactive for too long,
47  * then it is disconnected from the server.<p>
48  *
49  * This is only necessary for stateless transports like HTTP
50  *
51  * @author <a HREF="mailto:hchirino@jboss.org">Hiram Chirino</a>
52  * @author adrian@jboss.org
53  */

54 public class ClientMonitorInterceptor extends JMSServerInterceptorSupport
55 {
56    //The list of Clients by ConnectionTokens
57
ConcurrentReaderHashMap clients = new ConcurrentReaderHashMap();
58
59    private static class ClientStats
60    {
61       private long lastUsed = System.currentTimeMillis();
62
63       boolean disconnectIfInactive = true;
64    }
65
66    public void disconnectInactiveClients(long disconnectTime)
67    {
68       log.debug("Checking for timedout clients.");
69       Iterator JavaDoc i = clients.keySet().iterator();
70       while (i.hasNext())
71       {
72          ConnectionToken dc = (ConnectionToken) i.next();
73          ClientStats cs = (ClientStats) clients.get(dc);
74          if (cs.disconnectIfInactive && cs.lastUsed < disconnectTime)
75          {
76             try
77             {
78                log.debug("Disconnecting client due to inactivity timeout: " + dc);
79                connectionClosing(dc);
80             }
81             catch (Throwable JavaDoc e)
82             {
83             }
84          }
85       }
86    }
87
88    /**
89     * Peek the stats. For testing.
90     *
91     * @param dc the connection token
92     * @return the stats
93     */

94    public ClientStats peekClientStats(ConnectionToken dc)
95    {
96       return (ClientStats) clients.get(dc);
97    }
98
99    public ClientStats getClientStats(ConnectionToken dc) throws JMSException JavaDoc
100    {
101       ClientStats cq = (ClientStats) clients.get(dc);
102       if (cq != null)
103          return cq;
104
105       // Remove any previous token with a null client id and remove it
106
if (dc.getClientID() != null)
107       {
108          ConnectionToken withoutID = new ConnectionToken(null, dc.clientIL, dc.getSessionId());
109          clients.remove(withoutID);
110       }
111
112       cq = new ClientStats();
113
114       // The JVM clientil does not ping.
115
if (dc.clientIL instanceof JVMClientIL)
116          cq.disconnectIfInactive = false;
117
118       clients.put(dc, cq);
119       return cq;
120    }
121
122    public TemporaryTopic JavaDoc getTemporaryTopic(ConnectionToken dc) throws JMSException JavaDoc
123    {
124       getClientStats(dc).lastUsed = System.currentTimeMillis();
125       return getNext().getTemporaryTopic(dc);
126    }
127
128    public TemporaryQueue JavaDoc getTemporaryQueue(ConnectionToken dc) throws JMSException JavaDoc
129    {
130       getClientStats(dc).lastUsed = System.currentTimeMillis();
131       return getNext().getTemporaryQueue(dc);
132    }
133
134    public void connectionClosing(ConnectionToken dc) throws JMSException JavaDoc
135    {
136       clients.remove(dc);
137       getNext().connectionClosing(dc);
138    }
139
140    public void addMessage(ConnectionToken dc, SpyMessage message) throws JMSException JavaDoc
141    {
142       getClientStats(dc).lastUsed = System.currentTimeMillis();
143       getNext().addMessage(dc, message);
144    }
145
146    public Queue JavaDoc createQueue(ConnectionToken dc, String JavaDoc dest) throws JMSException JavaDoc
147    {
148       getClientStats(dc).lastUsed = System.currentTimeMillis();
149       return getNext().createQueue(dc, dest);
150
151    }
152
153    public Topic JavaDoc createTopic(ConnectionToken dc, String JavaDoc dest) throws JMSException JavaDoc
154    {
155       getClientStats(dc).lastUsed = System.currentTimeMillis();
156       return getNext().createTopic(dc, dest);
157    }
158
159    public void deleteTemporaryDestination(ConnectionToken dc, SpyDestination dest) throws JMSException JavaDoc
160    {
161       getClientStats(dc).lastUsed = System.currentTimeMillis();
162       getNext().deleteTemporaryDestination(dc, dest);
163    }
164
165    public void transact(ConnectionToken dc, TransactionRequest t) throws JMSException JavaDoc
166    {
167       getClientStats(dc).lastUsed = System.currentTimeMillis();
168       getNext().transact(dc, t);
169
170    }
171
172    public void acknowledge(ConnectionToken dc, AcknowledgementRequest item) throws JMSException JavaDoc
173    {
174       getClientStats(dc).lastUsed = System.currentTimeMillis();
175       getNext().acknowledge(dc, item);
176    }
177
178    public SpyMessage[] browse(ConnectionToken dc, Destination JavaDoc dest, String JavaDoc selector) throws JMSException JavaDoc
179    {
180       getClientStats(dc).lastUsed = System.currentTimeMillis();
181       return getNext().browse(dc, dest, selector);
182    }
183
184    public SpyMessage receive(ConnectionToken dc, int subscriberId, long wait) throws JMSException JavaDoc
185    {
186       getClientStats(dc).lastUsed = System.currentTimeMillis();
187       return getNext().receive(dc, subscriberId, wait);
188    }
189
190    public void setEnabled(ConnectionToken dc, boolean enabled) throws JMSException JavaDoc
191    {
192       getClientStats(dc).lastUsed = System.currentTimeMillis();
193       getNext().setEnabled(dc, enabled);
194    }
195
196    public void unsubscribe(ConnectionToken dc, int subscriptionId) throws JMSException JavaDoc
197    {
198       getClientStats(dc).lastUsed = System.currentTimeMillis();
199       getNext().unsubscribe(dc, subscriptionId);
200    }
201
202    public void destroySubscription(ConnectionToken dc, DurableSubscriptionID id) throws JMSException JavaDoc
203    {
204       getClientStats(dc).lastUsed = System.currentTimeMillis();
205       getNext().destroySubscription(dc, id);
206    }
207
208    public void subscribe(org.jboss.mq.ConnectionToken dc, org.jboss.mq.Subscription s) throws JMSException JavaDoc
209    {
210
211       getClientStats(dc).lastUsed = System.currentTimeMillis();
212       getNext().subscribe(dc, s);
213
214    }
215
216    public void ping(ConnectionToken dc, long clientTime) throws JMSException JavaDoc
217    {
218       getClientStats(dc).lastUsed = System.currentTimeMillis();
219       getNext().ping(dc, clientTime);
220    }
221
222    public Subscription getSubscription(ConnectionToken dc, int subscriberId) throws JMSException JavaDoc
223    {
224       getClientStats(dc).lastUsed = System.currentTimeMillis();
225       return getNext().getSubscription(dc, subscriberId);
226
227    }
228 }
229
Popular Tags