001/* 002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 * 017 * $Id$ 018 */ 019package org.nuxeo.runtime.management.counters; 020 021import java.util.Map; 022import java.util.concurrent.ConcurrentHashMap; 023 024import org.javasimon.CallbackSkeleton; 025import org.javasimon.Counter; 026 027/** 028 * Listen to Simon events to store past values of the counters History is kept in memory using 029 * {@link CounterHistoryStack} 030 * 031 * @author Tiry (tdelprat@nuxeo.com) 032 */ 033public class CounterHistoryRecorder extends CallbackSkeleton { 034 035 protected Map<String, CounterHistoryStack> counterHistory = new ConcurrentHashMap<String, CounterHistoryStack>(); 036 037 protected int historyLength = 100; 038 039 public CounterHistoryRecorder(int size) { 040 historyLength = size; 041 } 042 043 protected CounterHistoryStack getCounterHistoryStack(Counter counter) { 044 CounterHistoryStack stack = counterHistory.get(counter.getName()); 045 if (stack == null) { 046 stack = new CounterHistoryStack(historyLength); 047 counterHistory.put(counter.getName(), stack); 048 } 049 return stack; 050 } 051 052 protected void storeCounter(Counter counter) { 053 getCounterHistoryStack(counter).push(new long[] { System.currentTimeMillis(), counter.getCounter() }); 054 } 055 056 @Override 057 public void counterDecrease(Counter counter, long dec) { 058 storeCounter(counter); 059 } 060 061 @Override 062 public void counterSet(Counter counter, long val) { 063 storeCounter(counter); 064 } 065 066 @Override 067 public void counterIncrease(Counter counter, long inc) { 068 storeCounter(counter); 069 } 070 071 public CounterHistoryStack getCounterHistory(String counterName) { 072 return counterHistory.get(counterName); 073 } 074}