001 /*
002 * This file is part of McIDAS-V
003 *
004 * Copyright 2007-2013
005 * Space Science and Engineering Center (SSEC)
006 * University of Wisconsin - Madison
007 * 1225 W. Dayton Street, Madison, WI 53706, USA
008 * https://www.ssec.wisc.edu/mcidas
009 *
010 * All Rights Reserved
011 *
012 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
013 * some McIDAS-V source code is based on IDV and VisAD source code.
014 *
015 * McIDAS-V is free software; you can redistribute it and/or modify
016 * it under the terms of the GNU Lesser Public License as published by
017 * the Free Software Foundation; either version 3 of the License, or
018 * (at your option) any later version.
019 *
020 * McIDAS-V is distributed in the hope that it will be useful,
021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023 * GNU Lesser Public License for more details.
024 *
025 * You should have received a copy of the GNU Lesser Public License
026 * along with this program. If not, see http://www.gnu.org/licenses.
027 */
028 package edu.wisc.ssec.mcidasv.monitors;
029
030 import java.util.Map;
031 import java.util.concurrent.ConcurrentHashMap;
032 import java.util.concurrent.Executors;
033 import java.util.concurrent.ScheduledExecutorService;
034 import java.util.concurrent.ScheduledFuture;
035 import java.util.concurrent.TimeUnit;
036
037 import edu.wisc.ssec.mcidasv.monitors.memory.MemoryMonitor;
038 import edu.wisc.ssec.mcidasv.monitors.time.TimeMonitor;
039
040 import ucar.unidata.util.CacheManager;
041
042 public class MonitorManager {
043
044 public enum MonitorType { MEMORY, TIME };
045
046 private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(3);
047
048 private final Map<MonitorType, Monitorable> monitors = new ConcurrentHashMap<MonitorType, Monitorable>();
049
050 private final Map<Monitorable, ScheduledFuture<?>> woot = new ConcurrentHashMap<Monitorable, ScheduledFuture<?>>();
051
052 public MonitorManager() {
053 monitors.put(MonitorType.MEMORY, new MemoryMonitor(this, 75, 95));
054 monitors.put(MonitorType.TIME, new TimeMonitor());
055 }
056
057 public void addListener(final MonitorType type, final Monitoring listener) {
058 Monitorable m = monitors.get(type);
059 if (!m.hasMonitors())
060 woot.put(m, scheduler.scheduleWithFixedDelay(m, 0, 2, TimeUnit.SECONDS));
061 m.addMonitor(listener);
062 }
063
064 public void removeListener(final MonitorType type, final Monitoring listener) {
065 Monitorable m = monitors.get(type);
066 m.removeMonitor(listener);
067 if (!m.hasMonitors()) {
068 ScheduledFuture<?> handle = woot.remove(m);
069 if (handle != null) {
070 handle.cancel(false);
071 }
072 }
073 }
074
075 public void scheduleClearCache() {
076 Runnable r = new Runnable() {
077 public void run() {
078 CacheManager.clearCache();
079 }
080 };
081 scheduler.schedule(r, 1, TimeUnit.SECONDS);
082 }
083 }