View Javadoc
1   /*
2    * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway
3    * Copyright (C) 2010  Mickael Guessant
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU General Public License
7    * as published by the Free Software Foundation; either version 2
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18   */
19  
20  package davmail.http;
21  
22  import org.apache.http.conn.HttpClientConnectionManager;
23  import org.apache.log4j.Logger;
24  
25  import java.util.HashSet;
26  import java.util.concurrent.Executors;
27  import java.util.concurrent.ScheduledExecutorService;
28  import java.util.concurrent.ThreadFactory;
29  import java.util.concurrent.TimeUnit;
30  
31  /**
32   * Single thread for all connection managers.
33   * close idle connections
34   */
35  public class DavMailIdleConnectionEvictor {
36      static final Logger LOGGER = Logger.getLogger(DavMailIdleConnectionEvictor.class);
37  
38      // connection manager set
39      private static final HashSet<HttpClientConnectionManager> connectionManagers = new HashSet<>();
40  
41      private static final long sleepTimeMs = 1000L * 60;
42      private static final long maxIdleTimeMs = 1000L * 60 * 5;
43  
44      private static ScheduledExecutorService scheduler = null;
45  
46      private static void initEvictorThread() {
47          synchronized (connectionManagers) {
48              if (scheduler == null) {
49                  scheduler = Executors.newScheduledThreadPool(1, new ThreadFactory() {
50                      int count = 0;
51                      @Override
52                      public Thread newThread(Runnable r) {
53                          Thread thread = new Thread(r, "PoolEvictor-" + count++);
54                          thread.setDaemon(true);
55                          thread.setUncaughtExceptionHandler((t, e) -> LOGGER.error(e.getMessage(), e));
56                          return thread;
57                      }
58                  });
59                  scheduler.scheduleAtFixedRate(() -> {
60                      synchronized (connectionManagers) {
61                          // iterate over connection managers
62                          for (HttpClientConnectionManager connectionManager : connectionManagers) {
63                              connectionManager.closeExpiredConnections();
64                              if (maxIdleTimeMs > 0) {
65                                  connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
66                              }
67                          }
68                      }
69                  }, sleepTimeMs, sleepTimeMs, TimeUnit.MILLISECONDS);
70              }
71          }
72      }
73  
74      public static void shutdown() throws InterruptedException {
75          synchronized (connectionManagers) {
76              scheduler.shutdown();
77              if (!scheduler.awaitTermination(sleepTimeMs, TimeUnit.MILLISECONDS)) {
78                  LOGGER.warn("Timed out waiting for tasks to complete");
79              }
80              scheduler = null;
81          }
82      }
83  
84      /**
85       * Add connection manager to evictor thread.
86       *
87       * @param connectionManager connection manager
88       */
89      public static void addConnectionManager(HttpClientConnectionManager connectionManager) {
90          synchronized (connectionManagers) {
91              initEvictorThread();
92              connectionManagers.add(connectionManager);
93          }
94      }
95  
96      /**
97       * Remove connection manager from evictor thread.
98       *
99       * @param connectionManager connection manager
100      */
101     public static void removeConnectionManager(HttpClientConnectionManager connectionManager) {
102         synchronized (connectionManagers) {
103             connectionManagers.remove(connectionManager);
104         }
105     }
106 }