001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.transport.http.server;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024:
025: /**
026: * Default hander for failures in connection listener IOProcessors.
027: * Supports configuration of number retries, delay per retry, and uptime interval considered a success (resets number retries to zero).
028: */
029: public class DefaultConnectionListenerFailureHandler implements
030: ConnectionListenerFailureHandler {
031:
032: private static final Log LOG = LogFactory
033: .getLog(DefaultConnectionListenerFailureHandler.class);
034:
035: protected int retryDelay;
036: protected int successInterval;
037: protected int maxRetries;
038:
039: private long lastFailure;
040: private long lastFirstFailure;
041: private int numRetries;
042:
043: /**
044: * Create a new DefaultConnectionListenerFailureHandler with default settings.
045: * retryDelay is 1 second, successInterval is 60 seconds, maxRetries is 10
046: */
047: public DefaultConnectionListenerFailureHandler() {
048: this (1000, 60000, 10);
049: }
050:
051: /**
052: * Create a new DefaultConnectionListenerFailureHandler
053: *
054: * @param retryDelay millis to wait before retrying
055: * @param successInterval millis after which an initial or retry attempt will be deemed a success, resetting retry count to 0
056: * @param maxRetries maximum number of retries allowed without a success, after which the listener will terminate
057: */
058: public DefaultConnectionListenerFailureHandler(int retryDelay,
059: int successInterval, int maxRetries) {
060: this .retryDelay = retryDelay;
061: this .successInterval = successInterval;
062: this .maxRetries = maxRetries;
063: this .lastFailure = this .lastFirstFailure = Long.MIN_VALUE;
064: this .numRetries = 0;
065: }
066:
067: /**
068: * Default behavior is to log a warning and attempt retry per constructor config, eventually failing with a logged error and notification.
069: * May subclass and override this method to change the behavior.
070: */
071: public boolean failed(IOProcessor connectionListener,
072: Throwable cause) {
073: long now = System.currentTimeMillis();
074: if (now > lastFailure + successInterval) {
075: numRetries = 0;
076: lastFirstFailure = now;
077: }
078: lastFailure = now;
079: if (numRetries >= maxRetries) {
080: notifyAbnormalTermination(connectionListener,
081: "Terminating connection listener "
082: + connectionListener + " after "
083: + numRetries + "retries in "
084: + (now - lastFirstFailure) / 1000
085: + " seconds.", cause);
086: return false;
087: } else {
088: numRetries++;
089: if (LOG.isWarnEnabled()) {
090: LOG.warn("Attempt number " + numRetries + " of "
091: + maxRetries
092: + " to reestalish connection listener "
093: + connectionListener + " due to failure ",
094: cause);
095: }
096: return true;
097: }
098: }
099:
100: /**
101: * Default bevarior is to log the error.
102: * May subclass and override this method to change behavior.
103: */
104: public void notifyAbnormalTermination(
105: IOProcessor connectionListener, String message,
106: Throwable cause) {
107: LOG.error(message, cause);
108: }
109:
110: }
|