Example usage for java.util.concurrent ScheduledFuture isDone

List of usage examples for java.util.concurrent ScheduledFuture isDone

Introduction

In this page you can find the example usage for java.util.concurrent ScheduledFuture isDone.

Prototype

boolean isDone();

Source Link

Document

Returns true if this task completed.

Usage

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestX509SecurityHandler.java

@Test
public void testCertificateRenewal() throws Exception {
    conf.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    RMAppSecurityManager rmAppSecurityManager = new RMAppSecurityManager(rmContext);
    MockX509SecurityHandler x509SecurityHandler = new MockX509SecurityHandler(rmContext, rmAppSecurityManager,
            false);/*from w  w  w  .  j  a  v a 2  s .  c o  m*/
    rmAppSecurityManager.registerRMAppSecurityHandler(x509SecurityHandler);
    rmAppSecurityManager.init(conf);
    rmAppSecurityManager.start();

    LocalDateTime now = DateUtils.getNow();
    LocalDateTime expiration = now.plus(10, ChronoUnit.SECONDS);
    ApplicationId appId = ApplicationId.newInstance(DateUtils.localDateTime2UnixEpoch(now), 1);
    x509SecurityHandler.setOldCertificateExpiration(DateUtils.localDateTime2UnixEpoch(expiration));

    X509SecurityHandler.X509MaterialParameter x509Param = new X509SecurityHandler.X509MaterialParameter(appId,
            "Dolores", 1);
    x509Param.setExpiration(DateUtils.localDateTime2UnixEpoch(expiration));
    x509SecurityHandler.registerRenewer(x509Param);
    Map<ApplicationId, ScheduledFuture> tasks = x509SecurityHandler.getRenewalTasks();
    ScheduledFuture renewalTask = tasks.get(appId);
    assertFalse(renewalTask.isCancelled());
    assertFalse(renewalTask.isDone());

    // Wait until the scheduled task is executed
    TimeUnit.SECONDS.sleep(10);
    assertTrue(renewalTask.isDone());
    assertFalse(x509SecurityHandler.getRenewalException());
    assertTrue(tasks.isEmpty());
    rmAppSecurityManager.stop();
}

From source file:org.apache.hadoop.yarn.server.resourcemanager.security.TestX509SecurityHandler.java

@Test(timeout = 12000)
public void testFailedCertificateRenewal() throws Exception {
    conf.set(YarnConfiguration.HOPS_RM_SECURITY_ACTOR_KEY,
            "org.apache.hadoop.yarn.server.resourcemanager.security.TestingRMAppSecurityActions");
    RMAppSecurityManager securityManager = new RMAppSecurityManager(rmContext);
    MockX509SecurityHandler.MockFailingX509SecurityHandler x509Handler = new MockX509SecurityHandler.MockFailingX509SecurityHandler(
            rmContext, securityManager, Integer.MAX_VALUE);
    securityManager.registerRMAppSecurityHandlerWithType(x509Handler, X509SecurityHandler.class);
    securityManager.init(conf);/*from w w  w.j a  v a2s  .  c o  m*/
    securityManager.start();

    LocalDateTime now = DateUtils.getNow();
    LocalDateTime expiration = now.plus(10, ChronoUnit.SECONDS);
    ApplicationId appId = ApplicationId.newInstance(DateUtils.localDateTime2UnixEpoch(now), 1);
    X509SecurityHandler.X509MaterialParameter x509Param = new X509SecurityHandler.X509MaterialParameter(appId,
            "Dolores", 1);
    x509Param.setExpiration(DateUtils.localDateTime2UnixEpoch(expiration));
    x509Handler.registerRenewer(x509Param);

    Map<ApplicationId, ScheduledFuture> tasks = x509Handler.getRenewalTasks();
    // There should be a scheduled task
    ScheduledFuture task = tasks.get(appId);
    assertFalse(task.isCancelled());
    assertFalse(task.isDone());
    assertFalse(x509Handler.hasRenewalFailed());
    assertEquals(0, x509Handler.getNumberOfRenewalFailures());

    TimeUnit.SECONDS.sleep(10);
    assertTrue(tasks.isEmpty());
    assertEquals(4, x509Handler.getNumberOfRenewalFailures());
    assertTrue(x509Handler.hasRenewalFailed());
    securityManager.stop();
}

From source file:org.eclipse.smarthome.binding.astro.handler.AstroThingHandler.java

/**
 * Stops all jobs for this thing.//from w w  w. j  a  v a  2  s  .c o  m
 */
private void stopJobs() {
    logger.debug("Stopping scheduled jobs for thing {}", getThing().getUID());
    monitor.lock();
    try {
        if (scheduledExecutor != null) {
            if (dailyJob != null) {
                scheduledExecutor.remove(dailyJob);
            }
            dailyJob = null;
        }
        for (ScheduledFuture<?> future : scheduledFutures) {
            if (!future.isDone()) {
                future.cancel(true);
            }
        }
        scheduledFutures.clear();
    } catch (Exception ex) {
        logger.error("{}", ex.getMessage(), ex);
    } finally {
        monitor.unlock();
    }
}

From source file:org.eclipse.smarthome.binding.astro.handler.AstroThingHandler.java

private void tidyScheduledFutures() {
    for (Iterator<ScheduledFuture<?>> iterator = scheduledFutures.iterator(); iterator.hasNext();) {
        ScheduledFuture<?> future = iterator.next();
        if (future.isDone()) {
            logger.trace("Tidying up done future {}", future);
            iterator.remove();/* w  ww.  java  2  s .  c  o  m*/
        }
    }
}

From source file:org.janusgraph.TestBed.java

/**
 * @param args//from w w  w. j  a  v  a  2  s .  com
 * @throws java.io.IOException
 */
public static void main(String[] args) throws Exception {
    Method method = TestBed.class.getMethod("getInt", int.class, int.class);
    AnnotatedType rt = method.getAnnotatedReturnType();
    System.out.println(rt.getType());
    System.out.println(rt.getAnnotations().length);
    System.out.println(method.getAnnotations().length);
    for (int i = 0; i < method.getAnnotations().length; i++) {
        System.out.println(method.getAnnotations()[i]);
    }

    //        String[] s = {"a","b","c","d","e","f","g","h","i","x","u"};
    //        int len = s.length;
    //        Random random = new Random();
    //
    //        Context c = new Context(new ObserverManager(),Observer.NO_OP);
    //        //Warmup
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long before = System.nanoTime();
    //        for (int i = 0; i < 1000000000; i++) {
    //            c.observe(s[1],s[2]);
    //        }
    //        long total = System.nanoTime()-before;
    //        System.out.println("Total time: " + total/1000000);

    System.exit(0);

    final ScheduledExecutorService exe = new ScheduledThreadPoolExecutor(1, new RejectedExecutionHandler() {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            r.run();
        }
    });
    ScheduledFuture future = exe.scheduleWithFixedDelay(new Runnable() {
        AtomicInteger atomicInt = new AtomicInteger(0);

        @Override
        public void run() {
            try {
                for (int i = 0; i < 10; i++) {
                    exe.submit(new Runnable() {

                        private final int number = atomicInt.incrementAndGet();

                        @Override
                        public void run() {
                            try {
                                Thread.sleep(150);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            System.out.println(number);
                        }
                    });
                    System.out.println("Submitted: " + i);
                    //                    doSomethingExpensive(20);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }, 0, 1, TimeUnit.SECONDS);
    Thread.sleep(10000);
    //        future.get(1,TimeUnit.SECONDS);
    System.out.println("Cancel: " + future.cancel(false));
    System.out.println("Done: " + future.isDone());
    exe.shutdown();
    //        Thread.sleep(2000);
    System.out.println("Terminate: " + exe.awaitTermination(5, TimeUnit.SECONDS));
    System.out.println("DONE");
    NonBlockingHashMapLong<String> id1 = new NonBlockingHashMapLong<String>(128);
    ConcurrentHashMap<Long, String> id2 = new ConcurrentHashMap<Long, String>(128, 0.75f, 2);

}

From source file:org.openhab.binding.astro.internal.handler.AstroThingHandler.java

/**
 * Stops all jobs for this thing./*from www.  ja  v  a2s  . c o m*/
 */
private void stopJobs() {
    logger.debug("Stopping scheduled jobs for thing {}", getThing().getUID());
    monitor.lock();
    try {
        if (cronScheduler != null) {
            if (dailyJob != null) {
                dailyJob.cancel(true);
            }
            dailyJob = null;
        }
        for (ScheduledFuture<?> future : scheduledFutures) {
            if (!future.isDone()) {
                future.cancel(true);
            }
        }
        scheduledFutures.clear();
    } catch (Exception ex) {
        logger.error("{}", ex.getMessage(), ex);
    } finally {
        monitor.unlock();
    }
}

From source file:org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler.java

private void disconnectFromHub() {
    ScheduledFuture<?> localHeartBeatJob = heartBeatJob;
    if (localHeartBeatJob != null && !localHeartBeatJob.isDone()) {
        localHeartBeatJob.cancel(false);
    }// w  w w.  j  a v a 2  s  .  c  o  m
    client.disconnect();
}

From source file:org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler.java

private void cancelRetry() {
    ScheduledFuture<?> localRetryJob = retryJob;
    if (localRetryJob != null && !localRetryJob.isDone()) {
        localRetryJob.cancel(false);/*from www. j  av a  2  s.  co  m*/
    }
}

From source file:org.openhab.binding.nest.internal.handler.NestBridgeHandler.java

private void scheduleTransmitJobForPendingRequests() {
    ScheduledFuture<?> localTransmitJob = transmitJob;
    if (!nestUpdateRequests.isEmpty() && (localTransmitJob == null || localTransmitJob.isDone())) {
        transmitJob = scheduler.schedule(this::transmitQueue, 0, SECONDS);
    }//  w  ww  .j a  va2  s .co m
}