Example usage for java.util.concurrent TimeUnit toMillis

List of usage examples for java.util.concurrent TimeUnit toMillis

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit toMillis.

Prototype

public long toMillis(long duration) 

Source Link

Document

Equivalent to #convert(long,TimeUnit) MILLISECONDS.convert(duration, this) .

Usage

From source file:org.alfresco.bm.test.ElapsedTimeCompletionEstimator.java

/**
 * Constructor with required dependencies
 * /*from ww  w  .  j  av  a2  s. co  m*/
 * @param resultService                 used find the first event time
 * @param timeUnitStr                   the unit used for the duration e.g. *SECONDS*, *MINUTES*, etc.
 * @param duration                      the duration of the test in whichever units are supplied
 */
public ElapsedTimeCompletionEstimator(EventService eventService, ResultService resultService,
        String timeUnitStr, long duration) {
    super(eventService, resultService);
    // Get the time unit
    try {
        TimeUnit timeUnit = TimeUnit.valueOf(timeUnitStr.trim().toUpperCase());
        duration = timeUnit.toMillis(duration);
    } catch (Exception e) {
        // Assume milliseconds
    }
    this.duration = duration;
}

From source file:org.apache.camel.component.mock.MockEndpoint.java

public static void assertWait(long timeout, TimeUnit unit, MockEndpoint... endpoints)
        throws InterruptedException {
    long start = System.currentTimeMillis();
    long left = unit.toMillis(timeout);
    long end = start + left;
    for (MockEndpoint endpoint : endpoints) {
        if (!endpoint.await(left, TimeUnit.MILLISECONDS)) {
            throw new AssertionError("Timeout waiting for endpoints to receive enough messages. "
                    + endpoint.getEndpointUri() + " timed out.");
        }/*from   ww  w  .ja  v a 2  s . c  o  m*/
        left = end - System.currentTimeMillis();
        if (left <= 0) {
            left = 0;
        }
    }
}

From source file:org.apache.activemq.leveldb.test.ZooKeeperTestSupport.java

protected void within(int time, TimeUnit unit, Task task) throws InterruptedException {
    long timeMS = unit.toMillis(time);
    long deadline = System.currentTimeMillis() + timeMS;
    while (true) {
        try {/*from   www . ja  v  a2  s. c om*/
            task.run();
            return;
        } catch (Throwable e) {
            long remaining = deadline - System.currentTimeMillis();
            if (remaining <= 0) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                }
                if (e instanceof Error) {
                    throw (Error) e;
                }
                throw new RuntimeException(e);
            }
            Thread.sleep(Math.min(timeMS / 10, remaining));
        }
    }
}

From source file:org.jvnet.hudson.plugins.thinbackup.utils.Utils.java

/**
 * Waits until all executors are idle and switch jenkins to quiet mode. If it takes to long that all executors are
 * idle because in the mean time other jobs are executed the timeout ensure that the quiet mode is forced.
 * //from ww w  .j  a v a 2 s.  com
 * @param timeout
 *          specifies when a quiet mode is forced. 0 = no timeout.
 * @param unit
 *          specifies the time unit for the value of timeout.
 * @throws IOException ?
 */
public static void waitUntilIdleAndSwitchToQuietMode(int timeout, TimeUnit unit) throws IOException {
    Hudson hudson = Hudson.getInstance();
    final Computer computers[] = hudson.getComputers();

    boolean running;
    long starttime = System.currentTimeMillis();
    do {
        running = false;
        for (final Computer computer : computers) {
            if (computer.countBusy() != 0) {
                running = true;
                break;
            }
        }

        try {
            TimeUnit.MILLISECONDS.sleep(QUIETMODE_MONITORING_SLEEP);
        } catch (final InterruptedException e) {
            LOGGER.log(Level.WARNING, e.getMessage(), e);
        }

        if (!hudson.isQuietingDown() && starttime + unit.toMillis(timeout) < System.currentTimeMillis()) {
            LOGGER.info("Force quiet mode for jenkins now and wait unilt all executors are idle.");
            hudson.doQuietDown();
        }
    } while (running);
}

From source file:org.apache.pulsar.client.impl.ClientBuilderImpl.java

@Override
public ClientBuilder operationTimeout(int operationTimeout, TimeUnit unit) {
    conf.setOperationTimeoutMs(unit.toMillis(operationTimeout));
    return this;
}

From source file:org.wso2.andes.thrift.MBThriftServer.java

/**
 * Blocks until server stop serving after a stop request, or the timeout occurs, or the current thread is
 * interrupted, whichever happens first.
 *
 * @param timeout the maximum time to wait
 * @param unit the time unit of the timeout argument
 * @return {@code true} if thrift server stopped and
 *         {@code false} if the timeout elapsed before server stops
 * @throws InterruptedException if interrupted while waiting
 *//*from  w  ww . j  a  v  a2  s. c om*/
private boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
    final long timeoutInMillis = unit.toMillis(timeout);
    final long shutDownStartTime = System.currentTimeMillis();
    long timeElapsed = 0;

    while (server.isServing() && timeElapsed < timeoutInMillis) {
        TimeUnit.MILLISECONDS.sleep(100);
        timeElapsed = System.currentTimeMillis() - shutDownStartTime;
    }

    return server.isServing();
}

From source file:org.apache.pulsar.client.impl.ClientBuilderImpl.java

@Override
public ClientBuilder connectionTimeout(int duration, TimeUnit unit) {
    conf.setConnectionTimeoutMs((int) unit.toMillis(duration));
    return this;
}

From source file:org.diorite.impl.scheduler.DioriteFuture.java

@Override
public synchronized T get(long timeout, final TimeUnit unit)
        throws InterruptedException, ExecutionException, TimeoutException {
    timeout = unit.toMillis(timeout);
    long period = this.getPeriod();
    long timestamp = (timeout > 0) ? System.currentTimeMillis() : 0;
    while (true) {
        if ((period == STATE_SINGLE) || (period == STATE_FUTURE)) {
            this.wait(timeout);
            period = this.getPeriod();
            if ((period == -STATE_SINGLE) || (period == STATE_FUTURE)) {
                if (timeout == 0) {
                    continue;
                }/*  w w w. ja  v a  2s  .c o  m*/
                timeout += timestamp - (timestamp = System.currentTimeMillis());
                if (timeout > 0) {
                    continue;
                }
                throw new TimeoutException();
            }
        }
        if (period == -STATE_CANCEL) {
            throw new CancellationException();
        }
        if (period == STATE_FUTURE_DONE) {
            if (this.exception == null) {
                return this.value;
            }
            throw new ExecutionException(this.exception);
        }
        throw new IllegalStateException("Expected from -1 to -4, but got " + period);
    }
}

From source file:org.apache.camel.component.mock.MockEndpoint.java

/**
 * Asserts that all the expectations on any {@link MockEndpoint} instances registered
 * in the given context are valid//from w w w .j a  v a 2s  . c om
 *
 * @param context the camel context used to find all the available endpoints to be asserted
 * @param timeout timeout
 * @param unit    time unit
 */
public static void assertIsSatisfied(CamelContext context, long timeout, TimeUnit unit)
        throws InterruptedException {
    ObjectHelper.notNull(context, "camelContext");
    ObjectHelper.notNull(unit, "unit");
    Collection<Endpoint> endpoints = context.getEndpoints();
    long millis = unit.toMillis(timeout);
    for (Endpoint endpoint : endpoints) {
        // if the endpoint was intercepted we should get the delegate
        if (endpoint instanceof InterceptSendToEndpoint) {
            endpoint = ((InterceptSendToEndpoint) endpoint).getDelegate();
        }
        if (endpoint instanceof MockEndpoint) {
            MockEndpoint mockEndpoint = (MockEndpoint) endpoint;
            mockEndpoint.setResultWaitTime(millis);
            mockEndpoint.assertIsSatisfied();
        }
    }
}

From source file:org.apache.metron.profiler.client.stellar.WindowLookbackTest.java

long getDurationMs() {
    int duration = ProfilerConfig.PROFILER_PERIOD.getDefault(Integer.class);
    TimeUnit unit = TimeUnit.valueOf(ProfilerConfig.PROFILER_PERIOD_UNITS.getDefault(String.class));
    return unit.toMillis(duration);
}