📄 queryhandlermeter.java
字号:
/* * Copyright (c) 2001-2007 Sun Micro//Systems, Inc. All rights reserved. * * The Sun Project JXTA(TM) Software License * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * 3. The end-user documentation included with the redistribution, if any, must * include the following acknowledgment: "This product includes software * developed by Sun Microsystems, Inc. for JXTA(TM) technology." * Alternately, this acknowledgment may appear in the software itself, if * and wherever such third-party acknowledgments normally appear. * * 4. The names "Sun", "Sun Microsystems, Inc.", "JXTA" and "Project JXTA" must * not be used to endorse or promote products derived from this software * without prior written permission. For written permission, please contact * Project JXTA at http://www.jxta.org. * * 5. Products derived from this software may not be called "JXTA", nor may * "JXTA" appear in their name, without prior written permission of Sun. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SUN * MICROSYSTEMS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * JXTA is a registered trademark of Sun Microsystems, Inc. in the United * States and other countries. * * Please see the license information page at : * <http://www.jxta.org/project/www/license.html> for instructions on use of * the license in source files. * * ==================================================================== * * This software consists of voluntary contributions made by many individuals * on behalf of Project JXTA. For more information on Project JXTA, please see * http://www.jxta.org. * * This license is based on the BSD license adopted by the Apache Foundation. */package net.jxta.impl.resolver.resolverMeter;import net.jxta.peer.*;import net.jxta.peergroup.*;import net.jxta.resolver.*;import net.jxta.impl.resolver.*;import net.jxta.impl.meter.*;import net.jxta.protocol.*;import net.jxta.meter.*;import net.jxta.util.*;import net.jxta.endpoint.*;import net.jxta.exception.*;import java.net.*;import java.util.*;public class QueryHandlerMeter { private static final int QUERY_CULLING_INTERVAL = 5 * 60 * 1000; // Fix-Me: Five minutes hardcoded for now... private ResolverServiceMonitor resolverServiceMonitor; private String handlerName; private QueryHandlerMetric cumulativeMetrics; private QueryHandlerMetric deltaMetrics; private Hashtable queryDestinationMeters = new Hashtable(); private Hashtable queryMetricsTable = null; private class QueryMetricsTable extends Hashtable { void deReference() { queryMetricsTable = null; } @Override public String toString() { return handlerName; } } private class QueryMetric { int queryId; long querySentTime = System.currentTimeMillis(); int numResponsesReceived = 0; long lastResponseTime = 0; QueryMetric(int queryId) { this.queryId = queryId; } } private static LinkedList queryMetricsTables = new LinkedList(); static { Thread cullQueries = new Thread(new Runnable() { public void run() { for (;;) { try { Thread.sleep(QUERY_CULLING_INTERVAL); } catch (Exception e) {} long dormantTime = System.currentTimeMillis() - QUERY_CULLING_INTERVAL; synchronized (queryMetricsTables) { LinkedList keysToRemove = new LinkedList(); for (Iterator i = queryMetricsTables.iterator(); i.hasNext();) { QueryMetricsTable queryMetricsTable = (QueryMetricsTable) i.next(); keysToRemove.clear(); synchronized (queryMetricsTable) { for (Enumeration e = queryMetricsTable.keys(); e.hasMoreElements();) { Integer key = (Integer) e.nextElement(); QueryMetric queryMetric = (QueryMetric) queryMetricsTable.get(key); if (queryMetric.lastResponseTime < dormantTime) { keysToRemove.add(key); } } for (Iterator k = keysToRemove.iterator(); k.hasNext();) { Integer key = (Integer) k.next(); queryMetricsTable.deReference(); queryMetricsTable.remove(key); } if (queryMetricsTable.size() == 0) { i.remove(); } } } } } } }, "Resolver Query Metrics Culling Thread"); cullQueries.setDaemon(true); cullQueries.start(); } public QueryHandlerMeter(String handlerName, ResolverServiceMonitor resolverServiceMonitor) { this.handlerName = handlerName; cumulativeMetrics = new QueryHandlerMetric(handlerName); this.resolverServiceMonitor = resolverServiceMonitor; } public QueryHandlerMetric getCumulativeMetrics() { return cumulativeMetrics; } public String getHandlerName() { return handlerName; } public synchronized QueryHandlerMetric collectMetrics() { QueryHandlerMetric prevDelta = deltaMetrics; for (Enumeration e = queryDestinationMeters.elements(); e.hasMoreElements();) { QueryDestinationMeter queryDestinationMeter = (QueryDestinationMeter) e.nextElement(); QueryDestinationMetric queryDestinationMetric = queryDestinationMeter.collectMetrics(); if (queryDestinationMetric != null) { /* Fix-me: while fixing an exception thrown by SrdiHandlerMeter, I noticed that a similar problem might occur here if there can be a case where a destinationMtric is available even though nothing has happened to create the delta. If that is not possible (we need to do a code review!) remove the check for a null delta here. */ if (prevDelta == null) { createDeltaMetric(); prevDelta = deltaMetrics; } prevDelta.addQueryDestinationMetric(queryDestinationMetric); } } deltaMetrics = null; return prevDelta; } public synchronized QueryDestinationMeter getQueryDestinationMeter(EndpointAddress endpointAddress) { PeerID peerID = MetricUtilities.getPeerIdFromEndpointAddress(endpointAddress); return getQueryDestinationMeter(peerID); } public synchronized QueryDestinationMeter getQueryDestinationMeter(String peerIdString) { PeerID peerID = MetricUtilities.getPeerIdFromString(peerIdString); return getQueryDestinationMeter(peerID); } public synchronized QueryDestinationMeter getQueryDestinationMeter(PeerID peerID) { QueryDestinationMeter queryDestinationMeter = (QueryDestinationMeter) queryDestinationMeters.get(peerID); if (queryDestinationMeter == null) { queryDestinationMeter = new QueryDestinationMeter(peerID); queryDestinationMeters.put(peerID, queryDestinationMeter); cumulativeMetrics.addQueryDestinationMetric(queryDestinationMeter.getCumulativeMetrics()); } return queryDestinationMeter; } private void createDeltaMetric() { deltaMetrics = new QueryHandlerMetric(handlerName); } @Override public String toString() { return "ResolverHandlerMeter(" + handlerName + ")"; } public void setRegistered(boolean registered) { if (deltaMetrics == null) { createDeltaMetric(); } deltaMetrics.setRegistered(registered); cumulativeMetrics.setRegistered(registered); } private QueryMetric getQueryMetric(int queryId) { Integer key = new Integer(queryId); synchronized (queryMetricsTables) { if (queryMetricsTable == null) { queryMetricsTable = new QueryMetricsTable(); queryMetricsTables.add(queryMetricsTable); } QueryMetric queryMetric = (QueryMetric) queryMetricsTable.get(key); if (queryMetric == null) { queryMetric = new QueryMetric(queryId);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -