Example usage for java.lang Process destroy

List of usage examples for java.lang Process destroy

Introduction

In this page you can find the example usage for java.lang Process destroy.

Prototype

public abstract void destroy();

Source Link

Document

Kills the process.

Usage

From source file:urls.CurlStore.java

public Pair<String, InputStream> get() {
    final Triple<String, Process, Long> triple = QUEUE.poll();
    if (triple == null)
        return null;

    final Process process = triple.getMiddle();
    final Long startTime = triple.getRight();
    final String url = triple.getLeft();

    if (process.isAlive()) {
        if (System.currentTimeMillis() - startTime < timeout)
            QUEUE.offer(triple);//from   ww  w . j a v  a  2  s. co  m
        else {
            process.destroy();
            EXIT_VALUES.append(". ");
        }
        return null;
    }

    if (process.exitValue() != 0) {
        EXIT_VALUES.append(process.exitValue() + " ");
        return null;
    }

    return Pair.of(url, process.getInputStream());
}

From source file:io.apiman.common.es.util.ApimanEmbeddedElastic.java

private void checkForDanglingProcesses() throws IOException {
    if (Files.exists(pidPath)) {
        for (String pid : Files.readAllLines(pidPath)) {
            System.err.println(//from   w  w  w  . ja  v a  2  s  .co m
                    "Attempting to kill Elasticsearch process left over from previous execution: " + pid);
            Process result = Runtime.getRuntime().exec("kill " + pid);
            IOUtils.copy(result.getInputStream(), System.out);
            IOUtils.copy(result.getErrorStream(), System.err);
            result.destroy();
        }
        Files.deleteIfExists(pidPath);
    }
}

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

/**
 * Method used to terminate the node health monitoring service.
 * //from  ww w .  j ava2 s. co  m
 */
@Override
protected void serviceStop() {
    if (!shouldRun(conf)) {
        return;
    }
    if (nodeHealthScriptScheduler != null) {
        nodeHealthScriptScheduler.cancel();
    }
    if (shexec != null) {
        Process p = shexec.getProcess();
        if (p != null) {
            p.destroy();
        }
    }
}

From source file:com.ms.commons.standalone.pojo.StandaloneJob.java

@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
    JobDataMap dataMap = context.getMergedJobDataMap();
    String baseStandalonePath = dataMap.getString("baseStandalonePath");
    CronJob cronJob = (CronJob) dataMap.get("cronJob");
    try {// ww w  .j av  a 2 s  .  c om
        modifyDataSourceProperties(cronJob.getFullClassName(), cronJob.getIdentity(), baseStandalonePath);
    } catch (Exception e1) {
        logger.warn("?job?msun.datasource.properties??"
                + cronJob.getIdentity() + "", e1);
    }

    String cmd = String.format("/bin/bash %s/bin/jobRunner.sh start %s", baseStandalonePath,
            cronJob.getFullClassName());
    Runtime runtime = Runtime.getRuntime();
    Process process = null;
    try {
        process = runtime.exec(cmd);
        process.waitFor();
    } catch (Exception e) {
        logger.error("runtime.exec cmd: " + cmd + " failed");
        throw new JobExecutionException(e);
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}

From source file:io.specto.hoverfly.junit.core.Hoverfly.java

private void cleanUp() {
    LOGGER.info("Destroying hoverfly process");

    if (startedProcess != null) {
        Process process = startedProcess.getProcess();
        process.destroy();//from   w  ww  .  ja v a2s .c  o m

        // Some platforms terminate process asynchronously, eg. Windows, and cannot guarantee that synchronous file deletion
        // can acquire file lock
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        Future<Integer> future = executorService.submit((Callable<Integer>) process::waitFor);
        try {
            future.get(5, TimeUnit.SECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            LOGGER.warn("Timeout when waiting for hoverfly process to terminate.");
        }
        executorService.shutdownNow();
    }

    proxyConfigurer.restoreProxySystemProperties();
    // TODO: reset default SslContext?
    tempFileManager.purge();
}

From source file:org.sonar.runner.api.CommandExecutor.java

private void monitorProcess(final ProcessMonitor processMonitor, final ExecutorService executor,
        final Process process) {
    new Thread() {
        @Override/*from w  ww .j  av  a  2  s.c o m*/
        public void run() {
            while (!executor.isTerminated()) {
                if (processMonitor.stop()) {
                    process.destroy();
                }
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    // ignore
                }
            }
        }
    }.start();
}

From source file:io.fabric8.kit.common.ExternalCommand.java

private void checkProcessExit(Process process) {
    try {//  w ww .  j  av a 2  s  .  co  m
        statusCode = process.waitFor();
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
    } catch (IllegalThreadStateException | InterruptedException e) {
        process.destroy();
        statusCode = -1;
    }
}

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

/**
 * Method used to terminate the node health monitoring service.
 * //w  w w. j a  va  2 s  .  c  om
 */
void stop() {
    if (!shouldRun(conf)) {
        return;
    }
    nodeHealthScriptScheduler.cancel();
    if (shexec != null) {
        Process p = shexec.getProcess();
        if (p != null) {
            p.destroy();
        }
    }
}

From source file:org.artofsolving.jodconverter.office.PooledOfficeManagerTest.java

public void restartAfterCrash() throws Exception {

    final PooledOfficeManagerSettings settings = new PooledOfficeManagerSettings(CONNECTION_MODE);
    settings.setProcessManager(OfficeUtils.findBestProcessManager());
    final PooledOfficeManager officeManager = new PooledOfficeManager(settings);
    ManagedOfficeProcess managedOfficeProcess = (ManagedOfficeProcess) FieldUtils
            .readDeclaredField(officeManager, "managedOfficeProcess", true);
    OfficeProcess process = (OfficeProcess) FieldUtils.readDeclaredField(managedOfficeProcess, "process", true);
    OfficeConnection connection = (OfficeConnection) FieldUtils.readDeclaredField(managedOfficeProcess,
            "connection", true);
    assertNotNull(connection);/*from  w ww .  j  a  va2s  . c  o  m*/

    try {
        officeManager.start();
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        new Thread() {
            public void run() {
                MockOfficeTask badTask = new MockOfficeTask(10 * 1000);
                try {
                    officeManager.execute(badTask);
                    fail("task should be cancelled");
                    //FIXME being in a separate thread the test won't actually fail
                } catch (OfficeException officeEx) {
                    assertTrue(officeEx.getCause() instanceof CancellationException);
                }
            }
        }.start();
        Thread.sleep(500);
        Process underlyingProcess = (Process) FieldUtils.readDeclaredField(process, "process", true);
        assertNotNull(underlyingProcess);
        logger.debug("Simulating the crash");
        underlyingProcess.destroy(); // simulate crash

        Thread.sleep(RESTART_WAIT_TIME);
        assertTrue(process.isRunning());
        assertTrue(connection.isConnected());

        MockOfficeTask goodTask = new MockOfficeTask();
        officeManager.execute(goodTask);
        assertTrue(goodTask.isCompleted());

    } finally {
        officeManager.stop();
        assertFalse(connection.isConnected());
        assertFalse(process.isRunning());
        assertEquals(process.getExitCode(0, 0), 0);
    }
}

From source file:io.jmnarloch.cd.go.plugin.sbt.SbtTaskExecutor.java

private int execute(ProcessBuilder builder, JobConsoleLogger console) throws IOException, InterruptedException {

    Process process = null;
    try {//w w w.  j a  va2 s  . co m
        process = builder.start();

        console.readOutputOf(process.getInputStream());
        console.readErrorOf(process.getErrorStream());
        return process.waitFor();
    } finally {
        if (process != null) {
            process.destroy();
        }
    }
}