Example usage for java.util TimerTask run

List of usage examples for java.util TimerTask run

Introduction

In this page you can find the example usage for java.util TimerTask run.

Prototype

public abstract void run();

Source Link

Document

The action to be performed by this timer task.

Usage

From source file:de.eod.jliki.users.utils.UserDBHelper.java

/**
 * Initializes the database for user management.<br/>
 *//*w  w  w  .j ava2 s .  com*/
public static void initializeDB() {
    final Permission adminsDocroot = new Permission("*", PermissionCategoryMap.CATEGORY_DOCROOT,
            PermissionType.READWRITER);
    final Permission adminsFiles = new Permission("*", PermissionCategoryMap.CATEGORY_FILES,
            PermissionType.READWRITER);
    final Permission adminDocroot = new Permission("*", PermissionCategoryMap.CATEGORY_DOCROOT,
            PermissionType.OWNER);
    final Permission adminFiles = new Permission("*", PermissionCategoryMap.CATEGORY_FILES,
            PermissionType.OWNER);

    final UserGroup admins = new UserGroup("admins");
    admins.addPermission(new Permission("*", PermissionCategoryMap.CATEGORY_CONFIG, PermissionType.READWRITER));
    admins.addPermission(adminsDocroot);
    admins.addPermission(adminsFiles);

    final User admin = new User("admin", "password", "", "", "");

    admin.addPermission(new Permission("*", PermissionCategoryMap.CATEGORY_CONFIG, PermissionType.OWNER));
    admin.addPermission(new Permission("*", PermissionCategoryMap.CATEGORY_DOCROOT, PermissionType.OWNER));
    admin.addPermission(new Permission("*", PermissionCategoryMap.CATEGORY_FILES, PermissionType.OWNER));

    final UserGroup users = new UserGroup("users");
    // duplicates can occurr here ...
    final Permission usersDocroot = ConfigManager.getInstance().getConfig().getBaseConfig()
            .userDocrootPermission();
    final Permission usersFiles = ConfigManager.getInstance().getConfig().getBaseConfig().userFilePermission();

    if (usersDocroot.equals(adminsDocroot) && usersDocroot.getType() == adminsDocroot.getType()) {
        users.addPermission(adminsDocroot);
    } else if (usersDocroot.equals(adminDocroot) && usersDocroot.getType() == adminDocroot.getType()) {
        users.addPermission(adminDocroot);
    } else {
        users.addPermission(usersDocroot);
    }

    if (usersFiles.equals(adminsFiles) && usersFiles.getType() == adminsFiles.getType()) {
        users.addPermission(adminsFiles);
    } else if (usersFiles.equals(adminFiles) && usersFiles.getType() == adminFiles.getType()) {
        users.addPermission(adminFiles);
    } else {
        users.addPermission(usersFiles);
    }

    admin.getGroups().add(admins);
    admin.getGroups().add(users);

    admin.setActive(ActiveState.ACTIVE);
    UserDBHelper.addUserToDB(admin);

    final Timer timer = new Timer();
    final TimerTask task = new UserDBScheduler();
    task.run();
    final int onceADay = 1000 * 60 * 60 * 24;
    timer.scheduleAtFixedRate(new UserDBScheduler(), new Date(), onceADay);
}

From source file:org.apache.hadoop.mapred.TestNodeHealthService.java

public void testNodeHealthScript() throws Exception {
    TaskTrackerHealthStatus healthStatus = new TaskTrackerHealthStatus();
    String errorScript = "echo ERROR\n echo \"Tracker not healthy\"";
    String normalScript = "echo \"I am all fine\"";
    String timeOutScript = "sleep 4\n echo\"I am fine\"";
    Configuration conf = getConfForNodeHealthScript();
    conf.writeXml(new FileOutputStream(nodeHealthConfigFile));

    NodeHealthCheckerService nodeHealthChecker = new NodeHealthCheckerService(conf);
    TimerTask timer = nodeHealthChecker.getTimer();
    writeNodeHealthScriptFile(normalScript, true);
    timer.run();

    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking initial healthy condition");
    // Check proper report conditions.
    assertTrue("Node health status reported unhealthy", healthStatus.isNodeHealthy());
    assertTrue("Node health status reported unhealthy", healthStatus.getHealthReport().isEmpty());

    // write out error file.
    // Healthy to unhealthy transition
    writeNodeHealthScriptFile(errorScript, true);
    // Run timer/*from ww w .  j a  va2  s . c o  m*/
    timer.run();
    // update health status
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking Healthy--->Unhealthy");
    assertFalse("Node health status reported healthy", healthStatus.isNodeHealthy());
    assertFalse("Node health status reported healthy", healthStatus.getHealthReport().isEmpty());

    // Check unhealthy to healthy transitions.
    writeNodeHealthScriptFile(normalScript, true);
    timer.run();
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking UnHealthy--->healthy");
    // Check proper report conditions.
    assertTrue("Node health status reported unhealthy", healthStatus.isNodeHealthy());
    assertTrue("Node health status reported unhealthy", healthStatus.getHealthReport().isEmpty());

    // Healthy to timeout transition.
    writeNodeHealthScriptFile(timeOutScript, true);
    timer.run();
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking Healthy--->timeout");
    assertFalse("Node health status reported healthy even after timeout", healthStatus.isNodeHealthy());
    assertEquals("Node time out message not propogated", healthStatus.getHealthReport(),
            NodeHealthCheckerService.NODE_HEALTH_SCRIPT_TIMED_OUT_MSG);
}

From source file:org.apache.hadoop.TestNodeHealthService.java

@Test
public void testNodeHealthScript() throws Exception {
    RecordFactory factory = RecordFactoryProvider.getRecordFactory(null);
    NodeHealthStatus healthStatus = factory.newRecordInstance(NodeHealthStatus.class);
    String errorScript = "echo ERROR\n echo \"Tracker not healthy\"";
    String normalScript = "echo \"I am all fine\"";
    String timeOutScript = "sleep 4\n echo\"I am fine\"";
    Configuration conf = getConfForNodeHealthScript();
    conf.writeXml(new FileOutputStream(nodeHealthConfigFile));
    conf.addResource(nodeHealthConfigFile.getName());

    NodeHealthCheckerService nodeHealthChecker = new NodeHealthCheckerService(conf);
    TimerTask timer = nodeHealthChecker.getTimer();
    writeNodeHealthScriptFile(normalScript, true);
    timer.run();

    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking initial healthy condition");
    // Check proper report conditions.
    Assert.assertTrue("Node health status reported unhealthy", healthStatus.getIsNodeHealthy());
    Assert.assertTrue("Node health status reported unhealthy", healthStatus.getHealthReport().isEmpty());

    // write out error file.
    // Healthy to unhealthy transition
    writeNodeHealthScriptFile(errorScript, true);
    // Run timer/*  w w  w.j  a  va  2s.  c o m*/
    timer.run();
    // update health status
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking Healthy--->Unhealthy");
    Assert.assertFalse("Node health status reported healthy", healthStatus.getIsNodeHealthy());
    Assert.assertFalse("Node health status reported healthy", healthStatus.getHealthReport().isEmpty());

    // Check unhealthy to healthy transitions.
    writeNodeHealthScriptFile(normalScript, true);
    timer.run();
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking UnHealthy--->healthy");
    // Check proper report conditions.
    Assert.assertTrue("Node health status reported unhealthy", healthStatus.getIsNodeHealthy());
    Assert.assertTrue("Node health status reported unhealthy", healthStatus.getHealthReport().isEmpty());

    // Healthy to timeout transition.
    writeNodeHealthScriptFile(timeOutScript, true);
    timer.run();
    nodeHealthChecker.setHealthStatus(healthStatus);
    LOG.info("Checking Healthy--->timeout");
    Assert.assertFalse("Node health status reported healthy even after timeout",
            healthStatus.getIsNodeHealthy());
    Assert.assertEquals("Node time out message not propogated", healthStatus.getHealthReport(),
            NodeHealthCheckerService.NODE_HEALTH_SCRIPT_TIMED_OUT_MSG);
}

From source file:org.apache.hadoop.yarn.server.nodemanager.nodelabels.TestConfigurationNodeLabelsProvider.java

@Test
public void testNodeLabelsFromConfig() throws IOException, InterruptedException {
    Configuration conf = new Configuration();
    modifyConf("A");
    nodeLabelsProvider.init(conf);/*  w w  w  .j a va2s.  c o m*/
    // test for ensuring labels are set during initialization of the class
    nodeLabelsProvider.start();
    assertNLCollectionEquals(toNodeLabelSet("A"), nodeLabelsProvider.getNodeLabels());

    // test for valid Modification
    TimerTask timerTask = nodeLabelsProvider.getTimerTask();
    modifyConf("X");
    timerTask.run();
    assertNLCollectionEquals(toNodeLabelSet("X"), nodeLabelsProvider.getNodeLabels());
}

From source file:org.opendaylight.vtn.manager.internal.util.concurrent.FutureCancellerTest.java

/**
 * Test method for//from w  w  w  .  j ava 2s .  c o  m
 * {@link FutureCanceller#set(Timer,long,ListenableFuture)} and
 * {@link FutureCanceller#set(Timer,long,ListenableFuture,boolean)}.
 *
 * @throws Exception  An error occurred.
 */
@Test
public void testSet() throws Exception {
    TestTimer timer = new TestTimer();
    List<Triple<SettableVTNFuture<Void>, Long, Boolean>> list = new ArrayList<>();
    IllegalStateException cause = new IllegalStateException();
    ListenableFuture<Void> succeeded = Futures.<Void>immediateFuture(null);
    ListenableFuture<Void> failed = Futures.<Void>immediateFailedFuture(cause);
    ListenableFuture<Void> cancelled = Futures.<Void>immediateCancelledFuture();
    List<ListenableFuture<Void>> ignored = new ArrayList<>();
    Collections.addAll(ignored, succeeded, failed, cancelled);

    long[] timeouts = { 1L, 10L, 333333L };
    boolean[] bools = { true, false };
    for (long timeout : timeouts) {
        SettableVTNFuture<Void> f = new SettableVTNFuture<>();
        f.setThread(Thread.currentThread());
        Triple<SettableVTNFuture<Void>, Long, Boolean> expected = new ImmutableTriple<>(f, timeout, false);
        list.add(expected);
        FutureCanceller.set(timer, timeout, f);

        // Completed future should be ignored.
        for (ListenableFuture<Void> completed : ignored) {
            FutureCanceller.set(timer, timeout, completed);
        }

        for (boolean intr : bools) {
            f = new SettableVTNFuture<>();
            f.setThread(Thread.currentThread());
            expected = new ImmutableTriple<>(f, timeout, intr);
            list.add(expected);
            FutureCanceller.set(timer, timeout, f, intr);

            // Completed future should be ignored.
            for (ListenableFuture<Void> completed : ignored) {
                FutureCanceller.set(timer, timeout, completed, intr);
            }
        }
    }

    Iterator<Triple<SettableVTNFuture<Void>, Long, Boolean>> it = list.iterator();
    for (Pair<Long, TimerTask> pair : timer.getTasks()) {
        assertTrue(it.hasNext());
        Triple<SettableVTNFuture<Void>, Long, Boolean> expected = it.next();

        // Check delay.
        assertEquals(expected.getMiddle(), pair.getLeft());

        // Check the target future.
        TimerTask task = pair.getRight();
        assertTrue(task instanceof FutureCanceller<?>);
        ListenableFuture target = getFieldValue(task, ListenableFuture.class, "targetTask");
        SettableVTNFuture<Void> f = expected.getLeft();
        assertEquals(target, f);

        // Check intr flag.
        Boolean intr = getFieldValue(task, Boolean.class, "needInterrupt");
        assertEquals(intr, expected.getRight());

        // Cancel the target future.
        assertEquals(false, f.isCancelled());
        assertEquals(false, f.isDone());
        task.run();
        assertEquals(intr.booleanValue(), Thread.interrupted());
        assertEquals(true, f.isCancelled());
        assertEquals(true, f.isDone());
    }

    assertFalse(it.hasNext());
}