View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   *
19   */
20  package org.apache.mina.core.session;
21  
22  import java.net.SocketAddress;
23  
24  import org.apache.mina.util.ExpirationListener;
25  import org.apache.mina.util.ExpiringMap;
26  
27  /**
28   * An {@link IoSessionRecycler} with sessions that time out on inactivity.
29   *
30   * @author <a href="http://mina.apache.org">Apache MINA Project</a>
31   * @org.apache.xbean.XBean
32   */
33  public class ExpiringSessionRecycler implements IoSessionRecycler {
34      /** A map used to store the session */
35      private ExpiringMap<SocketAddress, IoSession> sessionMap;
36  
37      /** A map used to keep a track of the expiration */ 
38      private ExpiringMap<SocketAddress, IoSession>.Expirer mapExpirer;
39  
40      /**
41       * Create a new ExpiringSessionRecycler instance
42       */
43      public ExpiringSessionRecycler() {
44          this(ExpiringMap.DEFAULT_TIME_TO_LIVE);
45      }
46  
47      /**
48       * Create a new ExpiringSessionRecycler instance
49       * 
50       * @param timeToLive The delay after which the session is going to be recycled
51       */
52      public ExpiringSessionRecycler(int timeToLive) {
53          this(timeToLive, ExpiringMap.DEFAULT_EXPIRATION_INTERVAL);
54      }
55  
56      /**
57       * Create a new ExpiringSessionRecycler instance
58       * 
59       * @param timeToLive The delay after which the session is going to be recycled
60       * @param expirationInterval The delay after which the expiration occurs
61       */
62      public ExpiringSessionRecycler(int timeToLive, int expirationInterval) {
63          sessionMap = new ExpiringMap<>(timeToLive, expirationInterval);
64          mapExpirer = sessionMap.getExpirer();
65          sessionMap.addExpirationListener(new DefaultExpirationListener());
66      }
67  
68      /**
69       * {@inheritDoc}
70       */
71      @Override
72      public void put(IoSession session) {
73          mapExpirer.startExpiringIfNotStarted();
74  
75          SocketAddress key = session.getRemoteAddress();
76  
77          if (!sessionMap.containsKey(key)) {
78              sessionMap.put(key, session);
79          }
80      }
81  
82      /**
83       * {@inheritDoc}
84       */
85      @Override
86      public IoSession recycle(SocketAddress remoteAddress) {
87          return sessionMap.get(remoteAddress);
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public void remove(IoSession session) {
95          sessionMap.remove(session.getRemoteAddress());
96      }
97  
98      /**
99       * Stop the thread from monitoring the map
100      */
101     public void stopExpiring() {
102         mapExpirer.stopExpiring();
103     }
104 
105     /**
106      * @return The session expiration time in second
107      */
108     public int getExpirationInterval() {
109         return sessionMap.getExpirationInterval();
110     }
111 
112     /**
113      * @return The session time-to-live in second
114      */
115     public int getTimeToLive() {
116         return sessionMap.getTimeToLive();
117     }
118 
119     /**
120      * Set the interval in which a session will live in the map before it is removed.
121      * 
122      * @param expirationInterval The session expiration time in seconds
123      */
124     public void setExpirationInterval(int expirationInterval) {
125         sessionMap.setExpirationInterval(expirationInterval);
126     }
127 
128     /**
129      * Update the value for the time-to-live
130      *
131      * @param timeToLive The time-to-live (seconds)
132      */
133     public void setTimeToLive(int timeToLive) {
134         sessionMap.setTimeToLive(timeToLive);
135     }
136 
137     private class DefaultExpirationListener implements ExpirationListener<IoSession> {
138         @Override
139         public void expired(IoSession expiredSession) {
140             expiredSession.closeNow();
141         }
142     }
143 }