001package org.apache.commons.jcs.utils.discovery;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import java.util.HashSet;
026import java.util.Set;
027
028/**
029 * This class periodically check the lastHeardFrom time on the services.
030 * <p>
031 * If they exceed the configurable limit, it removes them from the set.
032 * <p>
033 * @author Aaron Smuts
034 */
035public class UDPCleanupRunner
036    implements Runnable
037{
038    /** log instance */
039    private static final Log log = LogFactory.getLog( UDPCleanupRunner.class );
040
041    /** UDP discovery service */
042    private final UDPDiscoveryService discoveryService;
043
044    /** default for max idle time, in seconds */
045    private static final long DEFAULT_MAX_IDLE_TIME_SECONDS = 180;
046
047    /** The configured max idle time, in seconds */
048    private final long maxIdleTimeSeconds = DEFAULT_MAX_IDLE_TIME_SECONDS;
049
050    /**
051     * @param service UDPDiscoveryService
052     */
053    public UDPCleanupRunner( UDPDiscoveryService service )
054    {
055        this.discoveryService = service;
056    }
057
058    /**
059     * This goes through the list of services and removes those that we haven't heard from in longer
060     * than the max idle time.
061     * <p>
062     * @see java.lang.Runnable#run()
063     */
064    @Override
065    public void run()
066    {
067        long now = System.currentTimeMillis();
068
069        // iterate through the set
070        // it is thread safe
071        // http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/CopyOnWriteArraySet.
072        // html
073        // TODO this should get a copy.  you can't simply remove from this.
074        // the listeners need to be notified.
075        Set<DiscoveredService> toRemove = new HashSet<DiscoveredService>();
076        // can't remove via the iterator. must remove directly
077        for (DiscoveredService service : discoveryService.getDiscoveredServices())
078        {
079            if ( ( now - service.getLastHearFromTime() ) > ( maxIdleTimeSeconds * 1000 ) )
080            {
081                if ( log.isInfoEnabled() )
082                {
083                    log.info( "Removing service, since we haven't heard from it in " + maxIdleTimeSeconds
084                        + " seconds.  service = " + service );
085                }
086                toRemove.add( service );
087            }
088        }
089
090        // remove the bad ones
091        for (DiscoveredService service : toRemove)
092        {
093            // call this so the listeners get notified
094            discoveryService.removeDiscoveredService( service );
095        }
096    }
097}