001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.broker.jmx; 018 019import com.fasterxml.jackson.databind.ObjectMapper; 020import org.apache.activemq.management.TimeStatisticImpl; 021import org.apache.activemq.store.PersistenceAdapter; 022import org.apache.activemq.store.PersistenceAdapterStatistics; 023 024import java.io.IOException; 025import java.util.HashMap; 026import java.util.Map; 027import java.util.concurrent.Callable; 028 029public class PersistenceAdapterView implements PersistenceAdapterViewMBean { 030 031 private final static ObjectMapper mapper = new ObjectMapper(); 032 private final String name; 033 private final PersistenceAdapter persistenceAdapter; 034 035 private Callable<String> inflightTransactionViewCallable; 036 private Callable<String> dataViewCallable; 037 private PersistenceAdapterStatistics persistenceAdapterStatistics; 038 039 public PersistenceAdapterView(PersistenceAdapter adapter) { 040 this.name = adapter.toString(); 041 this.persistenceAdapter = adapter; 042 } 043 044 @Override 045 public String getName() { 046 return name; 047 } 048 049 @Override 050 public String getTransactions() { 051 return invoke(inflightTransactionViewCallable); 052 } 053 054 @Override 055 public String getData() { 056 return invoke(dataViewCallable); 057 } 058 059 @Override 060 public long getSize() { 061 return persistenceAdapter.size(); 062 } 063 064 @Override 065 public String getStatistics() { 066 return serializePersistenceAdapterStatistics(); 067 } 068 069 @Override 070 public String resetStatistics() { 071 final String result = serializePersistenceAdapterStatistics(); 072 073 if (persistenceAdapterStatistics != null) { 074 persistenceAdapterStatistics.reset(); 075 } 076 077 return result; 078 } 079 080 private String invoke(Callable<String> callable) { 081 String result = null; 082 if (callable != null) { 083 try { 084 result = callable.call(); 085 } catch (Exception e) { 086 result = e.toString(); 087 } 088 } 089 return result; 090 } 091 092 private String serializePersistenceAdapterStatistics() { 093 if (persistenceAdapterStatistics != null) { 094 try { 095 Map<String, Object> result = new HashMap<String, Object>(); 096 result.put("writeTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getWriteTime())); 097 result.put("readTime", getTimeStatisticAsMap(persistenceAdapterStatistics.getReadTime())); 098 return mapper.writeValueAsString(result); 099 } catch (IOException e) { 100 return e.toString(); 101 } 102 } 103 104 return null; 105 } 106 107 private Map<String, Object> getTimeStatisticAsMap(final TimeStatisticImpl timeStatistic) { 108 Map<String, Object> result = new HashMap<String, Object>(); 109 110 result.put("count", timeStatistic.getCount()); 111 result.put("maxTime", timeStatistic.getMaxTime()); 112 result.put("minTime", timeStatistic.getMinTime()); 113 result.put("totalTime", timeStatistic.getTotalTime()); 114 result.put("averageTime", timeStatistic.getAverageTime()); 115 result.put("averageTimeExMinMax", timeStatistic.getAverageTimeExcludingMinMax()); 116 result.put("averagePerSecond", timeStatistic.getAveragePerSecond()); 117 result.put("averagePerSecondExMinMax", timeStatistic.getAveragePerSecondExcludingMinMax()); 118 119 return result; 120 } 121 122 public void setDataViewCallable(Callable<String> dataViewCallable) { 123 this.dataViewCallable = dataViewCallable; 124 } 125 126 public void setInflightTransactionViewCallable(Callable<String> inflightTransactionViewCallable) { 127 this.inflightTransactionViewCallable = inflightTransactionViewCallable; 128 } 129 130 public void setPersistenceAdapterStatistics(PersistenceAdapterStatistics persistenceAdapterStatistics) { 131 this.persistenceAdapterStatistics = persistenceAdapterStatistics; 132 } 133}