001/* 002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021package org.nuxeo.runtime.management.counters; 022 023import java.util.Map; 024import java.util.concurrent.ConcurrentHashMap; 025 026import org.javasimon.CallbackSkeleton; 027import org.javasimon.Counter; 028 029/** 030 * Listen to Simon events to store past values of the counters History is kept in memory using 031 * {@link CounterHistoryStack} 032 * 033 * @author Tiry ([email protected]) 034 */ 035public class CounterHistoryRecorder extends CallbackSkeleton { 036 037 protected Map<String, CounterHistoryStack> counterHistory = new ConcurrentHashMap<String, CounterHistoryStack>(); 038 039 protected int historyLength = 100; 040 041 public CounterHistoryRecorder(int size) { 042 historyLength = size; 043 } 044 045 protected CounterHistoryStack getCounterHistoryStack(Counter counter) { 046 CounterHistoryStack stack = counterHistory.get(counter.getName()); 047 if (stack == null) { 048 stack = new CounterHistoryStack(historyLength); 049 counterHistory.put(counter.getName(), stack); 050 } 051 return stack; 052 } 053 054 protected void storeCounter(Counter counter) { 055 getCounterHistoryStack(counter).push(new long[] { System.currentTimeMillis(), counter.getCounter() }); 056 } 057 058 @Override 059 public void counterDecrease(Counter counter, long dec) { 060 storeCounter(counter); 061 } 062 063 @Override 064 public void counterSet(Counter counter, long val) { 065 storeCounter(counter); 066 } 067 068 @Override 069 public void counterIncrease(Counter counter, long inc) { 070 storeCounter(counter); 071 } 072 073 public CounterHistoryStack getCounterHistory(String counterName) { 074 return counterHistory.get(counterName); 075 } 076}