All Downloads are FREE. Search and download functionalities are using the official Maven repository.

software.amazon.nio.spi.s3.util.TimeOutUtils Maven / Gradle / Ivy

Go to download

A Java NIO.2 service provider for S3, allowing Java NIO operations to be performed on paths using the `s3` scheme. This package implements the service provider interface (SPI) defined for Java NIO.2 in JDK 1.7 providing "plug-in" non-blocking access to S3 objects for Java applications using Java NIO.2 for file access.

There is a newer version: 2.2.0
Show newest version
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package software.amazon.nio.spi.s3.util;

import org.slf4j.Logger;

import java.util.Locale;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

import static java.lang.String.format;

public class TimeOutUtils {

    public static long TIMEOUT_TIME_LENGTH_1 = 1L;
    public static long TIMEOUT_TIME_LENGTH_3 = 3L;
    public static long TIMEOUT_TIME_LENGTH_5 = 5L;

    /**
     * Generate a time-out message string.
     * @param operationName the name of the operation that timed out
     * @param length the length of time
     * @param unit the unit of time
     * @return the message
     */
    public static String createTimeOutMessage(String operationName, long length, TimeUnit unit) {
        return format("the %s operation timed out after %d %s, check your network connectivity and status of S3 service",
                  operationName, length, unit.toString().toLowerCase(Locale.ROOT));
    }

    /**
     * Creates a time out message and logs the same to the logger
     * @param logger to which the message is logged
     * @param operationName the name of the operation that timed out
     * @param length the length of time
     * @param unit the unit of time
     * @return the message logged
     */
    public static String createAndLogTimeOutMessage(Logger logger, String operationName, long length, TimeUnit unit) {
        Objects.requireNonNull(logger);
        String msg = createTimeOutMessage(operationName, length, unit);
        logger.error(msg);
        return msg;
    }

    /**
     * Generate a RuntimeException representing the time-out and log the message containerd in the exception
     * @param logger to which the message is logged
     * @param operationName the name of the operation that timed out
     * @param length the length of time
     * @param unit the unit of time
     * @return the exception generated, ready to throw.
     */
    public static RuntimeException logAndGenerateExceptionOnTimeOut(Logger logger, String operationName, long length, TimeUnit unit){
        Objects.requireNonNull(logger);
        String msg = createAndLogTimeOutMessage(logger, operationName, length, unit);
        return new RuntimeException(msg);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy