001: /*
002: * Copyright 2004,2005 The Apache Software Foundation.
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: package org.wso2.esb.statistics;
017:
018: import org.apache.log4j.Logger;
019: import org.apache.synapse.core.SynapseEnvironment;
020: import org.apache.synapse.statistics.StatisticsCollector;
021: import org.apache.synapse.statistics.StatisticsHolder;
022: import org.wso2.esb.ServiceBusConstants;
023: import org.wso2.esb.persistence.PersistenceManager;
024: import org.wso2.esb.statistics.persistence.StatisticsDBUtils;
025: import org.wso2.esb.util.HibernateConfigCache;
026:
027: import java.net.InetAddress;
028: import java.net.UnknownHostException;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032:
033: /**
034: *
035: *
036: */
037: public class StatisticsReporterThread extends Thread {
038:
039: private static Logger log = Logger
040: .getLogger(StatisticsReporterThread.class);
041:
042: private boolean shutdownRequested = false;
043:
044: private PersistenceManager pm = new PersistenceManager(
045: HibernateConfigCache
046: .getHibernateConfig(
047: org.wso2.esb.ServiceBusConstants.WSO2ESB_HB_CONFIG_KEY,
048: System
049: .getProperty(ServiceBusConstants.ESB_HIBERNATE_CFG_XML)));
050:
051: /* The reference to the synapse environment */
052: private SynapseEnvironment synapseEnvironment;
053:
054: /* IP Address of Server */
055: private String ipAddr;
056:
057: private long delay = 60 * 1000;
058:
059: /**
060: * To construct it is need synapse Env. and PersistenceManager
061: *
062: * @param synapseEnvironment
063: */
064: public StatisticsReporterThread(
065: SynapseEnvironment synapseEnvironment) {
066: this .synapseEnvironment = synapseEnvironment;
067: }
068:
069: public long getDelay() {
070: return delay;
071: }
072:
073: public void setDelay(long delay) {
074: this .delay = delay;
075: }
076:
077: /**
078: *
079: */
080: public void run() {
081:
082: while (!shutdownRequested) {
083:
084: StatisticsCollector statisticsCollector = synapseEnvironment
085: .getStatisticsCollector();
086:
087: // log.info("Statistics Reporter is running");
088: if (statisticsCollector != null) {
089:
090: try {
091: if (ipAddr == null) {
092: InetAddress addr = InetAddress.getLocalHost();
093: if (addr != null) {
094: // Get IP Address
095: ipAddr = addr.getHostAddress();
096:
097: }
098:
099: }
100: } catch (UnknownHostException e) {
101: log.warn("Unable to get hostname or IP address", e);
102: }
103: // report sequence statistics to database
104: Map seqMap = new HashMap();
105: seqMap.putAll(statisticsCollector
106: .getSequenceStatistics());
107: statisticsCollector.resetSequenceStatistics();
108: for (Iterator sequenceStatistics = seqMap.values()
109: .iterator(); sequenceStatistics.hasNext();) {
110:
111: Object statisticsHolderObject = sequenceStatistics
112: .next();
113: if (statisticsHolderObject instanceof StatisticsHolder) {
114: StatisticsHolder statisticsHolder = (StatisticsHolder) statisticsHolderObject;
115: StatisticsDBUtils.updateRecord(pm, ipAddr,
116: statisticsHolder);
117: statisticsHolder.clearStatistics();
118: }
119: }
120: // report proxy service statistics to database
121: Map proxyMap = new HashMap();
122: proxyMap.putAll(statisticsCollector
123: .getProxyServiceStatistics());
124: statisticsCollector.resetProxyServiceStatistics();
125: for (Iterator psStats = proxyMap.values().iterator(); psStats
126: .hasNext();) {
127:
128: Object statisticsHolderObject = psStats.next();
129: if (statisticsHolderObject instanceof StatisticsHolder) {
130: StatisticsHolder statisticsHolder = (StatisticsHolder) statisticsHolderObject;
131: StatisticsDBUtils.updateRecord(pm, ipAddr,
132: statisticsHolder);
133: statisticsHolder.clearStatistics();
134: }
135: }
136: // report end point statistics to database
137: Map epMap = new HashMap();
138: epMap.putAll(statisticsCollector
139: .getEndPointStatistics());
140: statisticsCollector.resetEndPointStatistics();
141: for (Iterator endPointStatistics = epMap.values()
142: .iterator(); endPointStatistics.hasNext();) {
143:
144: Object statisticsHolderObject = endPointStatistics
145: .next();
146: if (statisticsHolderObject instanceof StatisticsHolder) {
147: StatisticsHolder statisticsHolder = (StatisticsHolder) statisticsHolderObject;
148: StatisticsDBUtils.updateRecord(pm, ipAddr,
149: statisticsHolder);
150: statisticsHolder.clearStatistics();
151: }
152: }
153: }
154:
155: try {
156: // sleep for one minute
157: Thread.sleep(delay);
158: } catch (InterruptedException ignore) {
159: }
160: }
161: }
162:
163: /**
164: *
165: */
166: public void shutdown() {
167: log.info("Statistics reporter thread is being stop ");
168: shutdownRequested = true;
169: }
170: }
|