Search in sources :

Example 11 with TimerTask

use of java.util.TimerTask in project hadoop by apache.

the class Dispatcher method prepareToExit.

private void prepareToExit() {
    checkState(devMode, "only in dev mode");
    new Timer("webapp exit", true).schedule(new TimerTask() {

        @Override
        public void run() {
            LOG.info("WebAppp /{} exiting...", webApp.name());
            webApp.stop();
            // FINDBUG: this is intended in dev mode
            System.exit(0);
        }
    }, // enough time for the last local request to complete
    18);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 12 with TimerTask

use of java.util.TimerTask in project hadoop by apache.

the class TestScriptBasedNodeLabelsProvider method testNodeLabelsScript.

@Test
public void testNodeLabelsScript() throws Exception {
    String scriptWithoutLabels = "";
    String normalScript = "echo NODE_PARTITION:Windows";
    String scrptWithMultipleLinesHavingNodeLabels = "echo NODE_PARTITION:RAM\n echo NODE_PARTITION:JDK1_6";
    String timeOutScript = Shell.WINDOWS ? "@echo off\nping -n 4 127.0.0.1 >nul\n" + "echo NODE_PARTITION:ALL" : "sleep 4\necho NODE_PARTITION:ALL";
    writeNodeLabelsScriptFile(scriptWithoutLabels, true);
    nodeLabelsProvider.init(getConfForNodeLabelScript());
    nodeLabelsProvider.start();
    Thread.sleep(500l);
    TimerTask timerTask = nodeLabelsProvider.getTimerTask();
    timerTask.run();
    Assert.assertNull("Node Label Script runner should return null when script doesnt " + "give any Labels output", nodeLabelsProvider.getNodeLabels());
    writeNodeLabelsScriptFile(normalScript, true);
    timerTask.run();
    assertNLCollectionEquals(toNodeLabelSet("Windows"), nodeLabelsProvider.getNodeLabels());
    // multiple lines with partition tag then the last line's partition info
    // needs to be taken.
    writeNodeLabelsScriptFile(scrptWithMultipleLinesHavingNodeLabels, true);
    timerTask.run();
    assertNLCollectionEquals(toNodeLabelSet("JDK1_6"), nodeLabelsProvider.getNodeLabels());
    // timeout script.
    writeNodeLabelsScriptFile(timeOutScript, true);
    timerTask.run();
    Assert.assertNotEquals("Node Labels should not be set after timeout ", toNodeLabelSet("ALL"), nodeLabelsProvider.getNodeLabels());
}
Also used : TimerTask(java.util.TimerTask) Test(org.junit.Test)

Example 13 with TimerTask

use of java.util.TimerTask in project hive by apache.

the class TestTxnCommands method testCompactionBlocking.

/**
   * Writing UTs that need multiple threads is challenging since Derby chokes on concurrent access.
   * This tests that "AND WAIT" actually blocks and responds to interrupt
   * @throws Exception
   */
@Test
public void testCompactionBlocking() throws Exception {
    Timer cancelCompact = new Timer("CancelCompactionTimer", false);
    final Thread threadToInterrupt = Thread.currentThread();
    cancelCompact.schedule(new TimerTask() {

        @Override
        public void run() {
            threadToInterrupt.interrupt();
        }
    }, 5000);
    long start = System.currentTimeMillis();
    runStatementOnDriver("alter table " + TestTxnCommands2.Table.ACIDTBL + " compact 'major' AND WAIT");
    //no Worker so it stays in initiated state
    //w/o AND WAIT the above alter table retunrs almost immediately, so the test here to check that
    //> 2 seconds pass, i.e. that the command in Driver actually blocks before cancel is fired
    Assert.assertTrue(System.currentTimeMillis() > start + 2);
}
Also used : Timer(java.util.Timer) TimerTask(java.util.TimerTask) Test(org.junit.Test)

Example 14 with TimerTask

use of java.util.TimerTask in project hive by apache.

the class ScriptOperator method close.

@Override
public void close(boolean abort) throws HiveException {
    boolean new_abort = abort;
    if (!abort) {
        if (scriptError != null) {
            throw new HiveException(ErrorMsg.SCRIPT_GENERIC_ERROR.getErrorCodedMsg(), scriptError);
        }
        // everything ok. try normal shutdown
        try {
            try {
                if (scriptOutWriter != null) {
                    scriptOutWriter.close();
                }
            } catch (IOException e) {
                if (isBrokenPipeException(e) && allowPartialConsumption()) {
                    LOG.warn("Got broken pipe: ignoring exception");
                } else {
                    if (isBrokenPipeException(e)) {
                        displayBrokenPipeInfo();
                    }
                    throw e;
                }
            }
            int exitVal = 0;
            if (scriptPid != null) {
                exitVal = scriptPid.waitFor();
            }
            if (exitVal != 0) {
                LOG.error("Script failed with code " + exitVal);
                new_abort = true;
            }
        } catch (IOException e) {
            LOG.error("Got ioexception: " + e.getMessage());
            e.printStackTrace();
            new_abort = true;
        } catch (InterruptedException e) {
        }
    } else {
        // error code of the child process if possible.
        try {
            // Interrupt the current thread after 1 second
            final Thread mythread = Thread.currentThread();
            Timer timer = new Timer(true);
            timer.schedule(new TimerTask() {

                @Override
                public void run() {
                    mythread.interrupt();
                }
            }, 1000);
            // Wait for the child process to finish
            int exitVal = 0;
            if (scriptPid != null) {
                scriptPid.waitFor();
            }
            // Cancel the timer
            timer.cancel();
            // Output the exit code
            LOG.error("Script exited with code " + exitVal);
        } catch (InterruptedException e) {
            // Ignore
            LOG.error("Script has not exited yet. It will be killed.");
        }
    }
    // try these best effort
    try {
        if (outThread != null) {
            outThread.join(0);
        }
    } catch (Exception e) {
        LOG.warn("Exception in closing outThread: " + StringUtils.stringifyException(e));
    }
    try {
        if (errThread != null) {
            errThread.join(0);
        }
    } catch (Exception e) {
        LOG.warn("Exception in closing errThread: " + StringUtils.stringifyException(e));
    }
    try {
        if (scriptPid != null) {
            scriptPid.destroy();
        }
    } catch (Exception e) {
        LOG.warn("Exception in destroying scriptPid: " + StringUtils.stringifyException(e));
    }
    super.close(new_abort);
    if (new_abort && !abort) {
        throw new HiveException(ErrorMsg.SCRIPT_CLOSING_ERROR.getErrorCodedMsg());
    }
}
Also used : HiveException(org.apache.hadoop.hive.ql.metadata.HiveException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) IOException(java.io.IOException) IOException(java.io.IOException) SerDeException(org.apache.hadoop.hive.serde2.SerDeException) HiveException(org.apache.hadoop.hive.ql.metadata.HiveException)

Example 15 with TimerTask

use of java.util.TimerTask in project tomcat by apache.

the class PoolUtils method checkMinIdle.

/**
     * Periodically check the idle object count for the pool. At most one idle
     * object will be added per period. If there is an exception when calling
     * {@link ObjectPool#addObject()} then no more checks will be performed.
     *
     * @param pool
     *            the pool to check periodically.
     * @param minIdle
     *            if the {@link ObjectPool#getNumIdle()} is less than this then
     *            add an idle object.
     * @param period
     *            the frequency to check the number of idle objects in a pool,
     *            see {@link Timer#schedule(TimerTask, long, long)}.
     * @param <T> the type of objects in the pool
     * @return the {@link TimerTask} that will periodically check the pools idle
     *         object count.
     * @throws IllegalArgumentException
     *             when <code>pool</code> is <code>null</code> or when
     *             <code>minIdle</code> is negative or when <code>period</code>
     *             isn't valid for {@link Timer#schedule(TimerTask, long, long)}
     */
public static <T> TimerTask checkMinIdle(final ObjectPool<T> pool, final int minIdle, final long period) throws IllegalArgumentException {
    if (pool == null) {
        throw new IllegalArgumentException("keyedPool must not be null.");
    }
    if (minIdle < 0) {
        throw new IllegalArgumentException("minIdle must be non-negative.");
    }
    final TimerTask task = new ObjectPoolMinIdleTimerTask<>(pool, minIdle);
    getMinIdleTimer().schedule(task, 0L, period);
    return task;
}
Also used : TimerTask(java.util.TimerTask)

Aggregations

TimerTask (java.util.TimerTask)359 Timer (java.util.Timer)260 IOException (java.io.IOException)25 Date (java.util.Date)21 Test (org.junit.Test)20 File (java.io.File)13 ArrayList (java.util.ArrayList)11 Intent (android.content.Intent)8 Bundle (android.os.Bundle)8 InputMethodManager (android.view.inputmethod.InputMethodManager)7 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 Location (android.location.Location)6 LocationListener (android.location.LocationListener)6 View (android.view.View)6 ImageView (android.widget.ImageView)6 HashMap (java.util.HashMap)6 TextView (android.widget.TextView)5 URI (java.net.URI)5 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5