All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coos.messaging.routing.TimedConcurrentHashMap Maven / Gradle / Ivy

The newest version!
/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.routing;

import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentHashMap;


public class TimedConcurrentHashMap extends ConcurrentHashMap {

    /** Use serialVersionUID for interoperability. */
    private final static long serialVersionUID = -3568451421580405608L;

    private Timer timer = new Timer("TimedConcurrentHashMap", true);
    private final ConcurrentHashMap timers =
        new ConcurrentHashMap();


    private final Log logger = LogFactory.getLog(this.getClass(), true);

    public TimedConcurrentHashMap() {
        super();
    }


    /**
     * This method cancels any current timers for the key
     */
    @Override public synchronized V put(K key, V value) {
        CallbackTask tt = timers.remove(key);

        if (tt != null) {
            tt.cancel();
        }

        return super.put(key, value);
    }


    /**
     *  This method cancels any current timers for the key
     */
    @Override public synchronized V remove(Object key) {
        logger.trace("Removing :" + key);

        CallbackTask tt = timers.remove(key);

        if (tt != null) {
            tt.cancel();
        }

        return super.remove(key);
    }

    /**
     * This method puts a key, value pair into a hashtable and schedules a callback to be called after a timeout
     * @param key
     * @param value
     * @param timeout
     * @param callback
     * @return
     */
    public synchronized V put(final K key, V value, long timeout, final HashMapCallback callback) {
        logger.trace("putting timeout, Key, value: " + timeout + "," + key + ", value:" + value);

        CallbackTask tt = timers.remove(key);

        if (tt != null) {
            tt.cancel();
        }

        try {
            tt = new CallbackTask(key, callback);
            timer.schedule(tt, timeout);
            timers.put(key, tt);
        } catch (IllegalStateException e) {
            logger.error("IllegalStateException ignored. key:" + key + ", value: " + value, e);
        }

        return super.put(key, value);
    }

    public synchronized void stop() {
        timer.cancel();
        timers.clear();

    }

    public void start() {
        timer = new Timer("TimedConcurrentHashMap", true);
    }

    class CallbackTask extends TimerTask {
        HashMapCallback callback;
        K key;


        public CallbackTask(K key, HashMapCallback callback) {
            super();
            this.callback = callback;
            this.key = key;
        }


        @Override public void run() {

            if ((callback != null) && callback.remove(key, TimedConcurrentHashMap.this)) {
                timers.remove(key);

                if (TimedConcurrentHashMap.super.remove(key) != null) {
                    logger.debug("Removing Key:" + key);
                }
            }
        }

    }


}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy