View Javadoc
1   package org.apache.commons.jcs.utils.discovery;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  import java.util.HashSet;
26  import java.util.Set;
27  
28  /**
29   * This class periodically check the lastHeardFrom time on the services.
30   * <p>
31   * If they exceed the configurable limit, it removes them from the set.
32   * <p>
33   * @author Aaron Smuts
34   */
35  public class UDPCleanupRunner
36      implements Runnable
37  {
38      /** log instance */
39      private static final Log log = LogFactory.getLog( UDPCleanupRunner.class );
40  
41      /** UDP discovery service */
42      private final UDPDiscoveryService discoveryService;
43  
44      /** default for max idle time, in seconds */
45      private static final long DEFAULT_MAX_IDLE_TIME_SECONDS = 180;
46  
47      /** The configured max idle time, in seconds */
48      private final long maxIdleTimeSeconds = DEFAULT_MAX_IDLE_TIME_SECONDS;
49  
50      /**
51       * @param service UDPDiscoveryService
52       */
53      public UDPCleanupRunner( UDPDiscoveryService service )
54      {
55          this.discoveryService = service;
56      }
57  
58      /**
59       * This goes through the list of services and removes those that we haven't heard from in longer
60       * than the max idle time.
61       * <p>
62       * @see java.lang.Runnable#run()
63       */
64      @Override
65      public void run()
66      {
67          long now = System.currentTimeMillis();
68  
69          // iterate through the set
70          // it is thread safe
71          // http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/CopyOnWriteArraySet.
72          // html
73          // TODO this should get a copy.  you can't simply remove from this.
74          // the listeners need to be notified.
75          Set<DiscoveredService> toRemove = new HashSet<DiscoveredService>();
76          // can't remove via the iterator. must remove directly
77          for (DiscoveredService service : discoveryService.getDiscoveredServices())
78          {
79              if ( ( now - service.getLastHearFromTime() ) > ( maxIdleTimeSeconds * 1000 ) )
80              {
81                  if ( log.isInfoEnabled() )
82                  {
83                      log.info( "Removing service, since we haven't heard from it in " + maxIdleTimeSeconds
84                          + " seconds.  service = " + service );
85                  }
86                  toRemove.add( service );
87              }
88          }
89  
90          // remove the bad ones
91          for (DiscoveredService service : toRemove)
92          {
93              // call this so the listeners get notified
94              discoveryService.removeDiscoveredService( service );
95          }
96      }
97  }