Example usage for java.util.concurrent TimeUnit sleep

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

Introduction

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

Prototype

public void sleep(long timeout) throws InterruptedException 

Source Link

Document

Performs a Thread#sleep(long,int) Thread.sleep using this time unit.

Usage

From source file:com.alexholmes.hdfsslurper.FileSystemManager.java

public FileStatus pollForInboundFile(TimeUnit unit, long period) throws IOException, InterruptedException {
    FileStatus fs;/*from w  ww.  ja  v a2s. c o m*/
    // we should optimize fo the case where the FileSystem is a local file
    // sytem,
    // in which case we could use Java 7's WatchService
    //
    while ((fs = getInboundFile()) == null) {
        unit.sleep(period);
    }
    return fs;
}

From source file:net.dv8tion.jda.core.requests.RestAction.java

/**
 * Blocks the current Thread for the specified delay and calls {@link #complete()}
 * when delay has been reached.//  www .j  av a  2  s.  c  om
 * <br>If the specified delay is negative this action will execute immediately. (see: {@link TimeUnit#sleep(long)})
 *
 * @param  delay
 *         The delay after which to execute a call to {@link #complete()}
 * @param  unit
 *         The {@link java.util.concurrent.TimeUnit TimeUnit} which should be used
 *         (this will use {@link java.util.concurrent.TimeUnit#sleep(long) unit.sleep(delay)})
 *
 * @throws java.lang.IllegalArgumentException
 *         If the specified {@link java.util.concurrent.TimeUnit TimeUnit} is {@code null}
 * @throws java.lang.RuntimeException
 *         If the sleep operation is interrupted
 *
 * @return The response value
 */
public T completeAfter(long delay, TimeUnit unit) {
    Checks.notNull(unit, "TimeUnit");
    try {
        unit.sleep(delay);
        return complete();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.hadoop.security.ssl.TestReloadingX509KeyManager.java

@Test(timeout = 4000)
public void testReload() throws Exception {
    KeyPair keyPair = KeyStoreTestUtil.generateKeyPair(KEY_PAIR_ALGORITHM);
    X509Certificate cert1 = KeyStoreTestUtil.generateCertificate("CN=cert1", keyPair, 2, CERTIFICATE_ALGORITHM);
    String keyStoreLocation = Paths.get(BASE_DIR, "testKeystore.jks").toString();
    KeyStoreTestUtil.createKeyStore(keyStoreLocation, KEYSTORE_PASSWORD, "cert1", keyPair.getPrivate(), cert1);

    ReloadingX509KeyManager keyManager = new ReloadingX509KeyManager("jks", keyStoreLocation, KEYSTORE_PASSWORD,
            KEYSTORE_PASSWORD, 10, TimeUnit.MILLISECONDS);

    try {//from  ww w .j a  v a2  s .  c o m
        keyManager.init();

        TimeUnit reloadTimeUnit = keyManager.getReloadTimeUnit();
        long reloadInterval = keyManager.getReloadInterval();

        X509Certificate[] certChain = keyManager.getCertificateChain("cert1");
        assertNotNull("Certificate chain should not be null for alias cert1", certChain);
        assertEquals("Certificate chain should be 1", 1, certChain.length);
        assertEquals("DN for cert1 should be CN=cert1", cert1.getSubjectDN().getName(),
                certChain[0].getSubjectDN().getName());

        // Wait a bit for the modification time to be different
        reloadTimeUnit.sleep(reloadInterval);
        TimeUnit.SECONDS.sleep(1);

        // Replace keystore with a new one with a different DN
        X509Certificate cert2 = KeyStoreTestUtil.generateCertificate("CN=cert2", keyPair, 2,
                CERTIFICATE_ALGORITHM);
        KeyStoreTestUtil.createKeyStore(keyStoreLocation, KEYSTORE_PASSWORD, "cert2", keyPair.getPrivate(),
                cert2);

        reloadTimeUnit.sleep(reloadInterval * 2);

        certChain = keyManager.getCertificateChain("cert1");
        assertNull("Certificate chain for alias cert1 should be null", certChain);
        certChain = keyManager.getCertificateChain("cert2");
        assertNotNull("Certificate chain should not be null for alias cert2", certChain);
        assertEquals("Certificate chain should be 1", 1, certChain.length);
        assertEquals("DN for cert2 should be CN=cert2", cert2.getSubjectDN().getName(),
                certChain[0].getSubjectDN().getName());

    } finally {
        keyManager.stop();
    }
}

From source file:org.apache.hadoop.security.ssl.TestReloadingX509KeyManager.java

@Test
public void testReloadWithPasswordfile() throws Exception {
    KeyPair keyPair = KeyStoreTestUtil.generateKeyPair(KEY_PAIR_ALGORITHM);
    X509Certificate cert1 = KeyStoreTestUtil.generateCertificate("CN=cert1", keyPair, 2, CERTIFICATE_ALGORITHM);

    String keyStoreLocation = Paths.get(BASE_DIR, "testKeystore.jks").toString();
    KeyStoreTestUtil.createKeyStore(keyStoreLocation, KEYSTORE_PASSWORD, "cert1", keyPair.getPrivate(), cert1);

    String passwordFileLocation = Paths.get(BASE_DIR, "password_file").toString();
    FileUtils.write(new File(passwordFileLocation), KEYSTORE_PASSWORD);

    ReloadingX509KeyManager keyManager = new ReloadingX509KeyManager("jks", keyStoreLocation, "wrong-password",
            passwordFileLocation, "wrong-password", 10, TimeUnit.MILLISECONDS);

    try {/*from   w  w w  .jav a  2s .c o  m*/
        keyManager.init();

        TimeUnit reloadTimeUnit = keyManager.getReloadTimeUnit();
        long reloadInterval = keyManager.getReloadInterval();

        X509Certificate[] certChain = keyManager.getCertificateChain("cert1");
        assertNotNull("Certificate chain should not be null for alias cert1", certChain);
        assertEquals("Certificate chain should be 1", 1, certChain.length);
        assertEquals("DN for cert1 should be CN=cert1", cert1.getSubjectDN().getName(),
                certChain[0].getSubjectDN().getName());

        // Wait a bit for the modification time to be different
        reloadTimeUnit.sleep(reloadInterval);
        TimeUnit.SECONDS.sleep(1);

        // Replace keystore with a new one with a different DN
        X509Certificate cert2 = KeyStoreTestUtil.generateCertificate("CN=cert2", keyPair, 2,
                CERTIFICATE_ALGORITHM);
        String newKeystorePassword = "password1";
        KeyStoreTestUtil.createKeyStore(keyStoreLocation, newKeystorePassword, "cert2", keyPair.getPrivate(),
                cert2);
        FileUtils.write(new File(passwordFileLocation), newKeystorePassword);

        reloadTimeUnit.sleep(reloadInterval * 2);

        certChain = keyManager.getCertificateChain("cert1");
        assertNull("Certificate chain for alias cert1 should be null", certChain);
        certChain = keyManager.getCertificateChain("cert2");
        assertNotNull("Certificate chain should not be null for alias cert2", certChain);
        assertEquals("Certificate chain should be 1", 1, certChain.length);
        assertEquals("DN for cert2 should be CN=cert2", cert2.getSubjectDN().getName(),
                certChain[0].getSubjectDN().getName());

    } finally {
        keyManager.stop();
    }
}

From source file:org.grails.datastore.mapping.redis.engine.RedisEntityPersister.java

@Override
protected void lockEntry(PersistentEntity persistentEntity, @SuppressWarnings("hiding") String entityFamily,
        Serializable id, int timeout) {
    String redisKey = getRedisKey(entityFamily, id);
    final TimeUnit milliUnit = TimeUnit.MILLISECONDS;
    final long waitTime = TimeUnit.SECONDS.toMillis(timeout);
    final String lockName = lockName(redisKey);
    int sleepTime = 0;
    while (true) {
        if (redisTemplate.setnx(lockName, System.currentTimeMillis())
                && redisTemplate.expire(lockName, timeout)) {
            break;
        }/*  w  w  w.j a v  a 2 s.c o m*/
        if (redisTemplate.ttl(lockName) > 0) {
            try {
                if (sleepTime > waitTime) {
                    throw new CannotAcquireLockException(
                            "Failed to acquire lock on key [" + redisKey + "]. Wait time exceeded timeout.");
                }
                // wait for previous lock to expire
                sleepTime += 500;
                milliUnit.sleep(500);
            } catch (InterruptedException e) {
                throw new CannotAcquireLockException(
                        "Failed to acquire lock on key [" + redisKey + "]: " + e.getMessage(), e);
            }
        } else {
            if (redisTemplate.getset(lockName, System.currentTimeMillis()) != null
                    && redisTemplate.expire(lockName, timeout)) {
                break;
            }
        }
    }
}