當前位置: 首頁>>代碼示例>>Java>>正文


Java Executor類代碼示例

本文整理匯總了Java中org.apache.commons.exec.Executor的典型用法代碼示例。如果您正苦於以下問題:Java Executor類的具體用法?Java Executor怎麽用?Java Executor使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


Executor類屬於org.apache.commons.exec包,在下文中一共展示了Executor類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: call

import org.apache.commons.exec.Executor; //導入依賴的package包/類
@Override
public Long call() throws Exception {
    Executor executor = new DefaultExecutor();
    executor.setProcessDestroyer(new ShutdownHookProcessDestroyer());
    ExecuteWatchdog watchDog = new ExecuteWatchdog(watchdogTimeout);
    executor.setWatchdog(watchDog);
    executor.setStreamHandler(new PumpStreamHandler(new MyLogOutputStream(handler, true), new MyLogOutputStream(handler, false)));
    Long exitValue;
    try {
        exitValue = new Long(executor.execute(commandline));
    } catch (ExecuteException e) {
        exitValue = new Long(e.getExitValue());
    }
    if (watchDog.killedProcess()) {
        exitValue = WATCHDOG_EXIST_VALUE;
    }
    return exitValue;
}
 
開發者ID:polygOnetic,項目名稱:guetzliconverter,代碼行數:19,代碼來源:ProcessExecutor.java

示例2: testExecutable

import org.apache.commons.exec.Executor; //導入依賴的package包/類
private static boolean testExecutable() {
    CommandLine commandLine = CommandLine.parse(RCLIProcessor.rExecutable + " " + VERSION_CALL);

    DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
    Executor executor = new DefaultExecutor();

    // put a watchdog with a timeout
    ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(TIMEOUT_SECONDS) * 1000);
    executor.setWatchdog(watchdog);

    try {
        executor.execute(commandLine, resultHandler);
        resultHandler.waitFor();
        int exitVal = resultHandler.getExitValue();
        if (exitVal != 0) {
            return false;
        }
            return true;
    }
    catch (Exception e) {
        return false;
    }
}
 
開發者ID:52North,項目名稱:movingcode,代碼行數:24,代碼來源:RCLIProbe.java

示例3: testExecutable

import org.apache.commons.exec.Executor; //導入依賴的package包/類
public static boolean testExecutable() {
	CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " --version");

	DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
	Executor executor = new DefaultExecutor();

	// put a watchdog with a timeout
	ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000);
	executor.setWatchdog(watchdog);

	try {
		executor.execute(commandLine, resultHandler);
		resultHandler.waitFor();
		int exitVal = resultHandler.getExitValue();
		if (exitVal != 0) {
			return false;
		}
		return true;
	}
	catch (Exception e) {
		return false;
	}
}
 
開發者ID:52North,項目名稱:movingcode,代碼行數:24,代碼來源:PythonCLIProbe.java

示例4: main

import org.apache.commons.exec.Executor; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    String cmd = "/tmp/test.sh";
    CommandLine cmdLine = CommandLine.parse("/bin/bash " + cmd);

    ByteArrayOutputStream stdout = new ByteArrayOutputStream();
    ByteArrayOutputStream stderr = new ByteArrayOutputStream();
    PumpStreamHandler psh = new PumpStreamHandler(stdout, stderr);

    psh.setStopTimeout(TIMEOUT_FIVE_MINUTES);

    ExecuteWatchdog watchdog = new ExecuteWatchdog(TIMEOUT_TEN_MINUTES); // timeout in milliseconds

    Executor executor = new DefaultExecutor();
    executor.setExitValue(0);
    executor.setStreamHandler(psh);
    executor.setWatchdog(watchdog);

    int exitValue = executor.execute(cmdLine, Collections.emptyMap());
    System.out.println(exitValue);
}
 
開發者ID:kinow,項目名稱:commons-sandbox,代碼行數:21,代碼來源:Tests.java

示例5: runH2Spec

import org.apache.commons.exec.Executor; //導入依賴的package包/類
public static List<Failure> runH2Spec(File targetDirectory, int port, final int timeout, final int maxHeaderLength, final Set<String> excludeSpecs) throws IOException {
    File reportsDirectory = new File(targetDirectory, "reports");
    if (!reportsDirectory.exists()) {
        reportsDirectory.mkdir();
    }

    File junitFile = new File(reportsDirectory, "TEST-h2spec.xml");
    File h2spec = getH2SpecFile(targetDirectory);

    Executor exec = new DefaultExecutor();
    PumpStreamHandler psh = new PumpStreamHandler(System.out, System.err, System.in);
    exec.setStreamHandler(psh);
    exec.setExitValues(new int[]{0, 1});

    psh.start();
    if (exec.execute(buildCommandLine(h2spec, port, junitFile, timeout, maxHeaderLength)) != 0) {
        return parseReports(reportsDirectory, excludeSpecs);
    }
    psh.stop();

    return Collections.emptyList();
}
 
開發者ID:madgnome,項目名稱:h2spec-maven-plugin,代碼行數:23,代碼來源:H2SpecTestSuite.java

示例6: dockerAsync

import org.apache.commons.exec.Executor; //導入依賴的package包/類
/**
 * Runs docker command asynchronously.
 *
 * @param arguments command line arguments
 * @param out       stdout will be written in to this file
 *
 * @return a handle used to stop the command process
 *
 * @throws IOException when command has error
 */
public ExecuteWatchdog dockerAsync(final List<Object> arguments, OutputStream out) throws IOException {
    CommandLine cmdLine = new CommandLine(DOCKER);
    arguments.forEach((arg) -> {
        cmdLine.addArgument(arg + "");
    });
    LOG.debug("[{} {}]", cmdLine.getExecutable(), StringUtils.join(cmdLine.getArguments(), " "));
    List<String> output = new ArrayList<>();
    ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
    Executor executor = new DefaultExecutor();
    executor.setWatchdog(watchdog);
    PrintWriter writer = new PrintWriter(out);
    ESH esh = new ESH(writer);
    executor.setStreamHandler(esh);
    executor.execute(cmdLine, new DefaultExecuteResultHandler());
    return watchdog;
}
 
開發者ID:tascape,項目名稱:reactor,代碼行數:27,代碼來源:DockerClient.java

示例7: setupSudoCredentials

import org.apache.commons.exec.Executor; //導入依賴的package包/類
public static ExecResult setupSudoCredentials(String password) {
    ExecCommand execCommand = new ExecCommand("sudo");
    execCommand.addArgument("--validate", false);
    execCommand.addArgument("--stdin", false);
    Executor executor = new DefaultExecutor();
    executor.setWorkingDirectory(execCommand.getWorkingDirectory());
    executor.setExitValues(execCommand.getExitValues());
    ExecOutputStream execOutputStream = new ExecOutputStream();
    executor.setStreamHandler(new PumpStreamHandler(execOutputStream, execOutputStream, new ExecInputStream(password)));
    try {
        executor.execute(execCommand);
    } catch (IOException e) {
        return new ExecResult(execOutputStream.getOutput(), e);
    }
    return new ExecResult(execOutputStream.getOutput());
}
 
開發者ID:ctco,項目名稱:gradle-mobile-plugin,代碼行數:17,代碼來源:ExecUtil.java

示例8: execCommand

import org.apache.commons.exec.Executor; //導入依賴的package包/類
@Deprecated
public static ExecResult execCommand(CommandLine commandLine, File workDir, int[] exitValues, boolean routeToCapture, boolean routeToStdout) {
    Executor executor = new DefaultExecutor();
    if (workDir != null) {
        executor.setWorkingDirectory(workDir);
    }
    if (exitValues != null) {
        executor.setExitValues(exitValues);
    }
    ExecOutputStream execOutputStream = new ExecOutputStream(routeToCapture, routeToStdout);
    PumpStreamHandler streamHandler = new PumpStreamHandler(execOutputStream);
    executor.setStreamHandler(streamHandler);
    try {
        executor.execute(commandLine);
    } catch (IOException e) {
        return new ExecResult(false, execOutputStream.getOutput(), e);
    }
    return new ExecResult(true, execOutputStream.getOutput(), null);
}
 
開發者ID:ctco,項目名稱:gradle-mobile-plugin,代碼行數:20,代碼來源:ExecUtil.java

示例9: waitForEngineStart

import org.apache.commons.exec.Executor; //導入依賴的package包/類
private void waitForEngineStart(Executor executor) throws Exception
{
  int i = 0;
  while (!engineStarted.get())
  {
    Thread.sleep(1_000);
    i++;
    if (!executor.getWatchdog().isWatching())
    {
      throw new RuntimeException("Engine start failed unexpected.");
    }
    if (i > context.timeoutInSeconds)
    {
      throw new TimeoutException("Timeout while starting engine " + context.timeoutInSeconds + " [s].\n"
              + "Check the engine log for details or increase the timeout property '"+StartTestEngineMojo.IVY_ENGINE_START_TIMEOUT_SECONDS+"'");
    }
  }
  context.log.info("Engine started after " + i + " [s]");
}
 
開發者ID:axonivy,項目名稱:project-build-plugin,代碼行數:20,代碼來源:EngineControl.java

示例10: executeSynch

import org.apache.commons.exec.Executor; //導入依賴的package包/類
/**
 * Run a short living engine command where we expect a process failure as the engine invokes <code>System.exit(-1)</code>.
 * @param statusCmd 
 * @return the output of the engine command.
 */
private String executeSynch(CommandLine statusCmd)
{
  String engineOutput = null;
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, System.err);
  Executor executor = createEngineExecutor();
  executor.setStreamHandler(streamHandler);
  executor.setExitValue(-1);
  try
  {
    executor.execute(statusCmd);
  }
  catch (IOException ex)
  { // expected!
  }
  finally
  {
    engineOutput = outputStream.toString();
    IOUtils.closeQuietly(outputStream);
  }
  return engineOutput;
}
 
開發者ID:axonivy,項目名稱:project-build-plugin,代碼行數:28,代碼來源:EngineControl.java

示例11: canStartEngine

import org.apache.commons.exec.Executor; //導入依賴的package包/類
@Test
public void canStartEngine() throws Exception
{
  StartTestEngineMojo mojo = rule.getMojo();
  assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).isNull();
  assertThat(getProperty(EngineControl.Property.TEST_ENGINE_LOG)).isNull();
  
  Executor startedProcess = null;
  try
  {
    startedProcess = mojo.startEngine();
    assertThat(getProperty(EngineControl.Property.TEST_ENGINE_URL)).startsWith("http://");
    assertThat(new File(getProperty(EngineControl.Property.TEST_ENGINE_LOG))).exists();
  }
  finally
  {
    kill(startedProcess);
  }
}
 
開發者ID:axonivy,項目名稱:project-build-plugin,代碼行數:20,代碼來源:TestStartEngine.java

示例12: testKillEngineOnVmExit

import org.apache.commons.exec.Executor; //導入依賴的package包/類
@Test
public void testKillEngineOnVmExit() throws Exception
{
  StartTestEngineMojo mojo = rule.getMojo();
  Executor startedProcess = null;
  try
  {
    startedProcess = mojo.startEngine();
    assertThat(startedProcess.getProcessDestroyer()).isInstanceOf(ShutdownHookProcessDestroyer.class);
    ShutdownHookProcessDestroyer jvmShutdownHoock = (ShutdownHookProcessDestroyer) startedProcess.getProcessDestroyer();
    assertThat(jvmShutdownHoock.size())
      .as("One started engine process must be killed on VM end.")
      .isEqualTo(1);
  }
  finally
  {
    kill(startedProcess);
  }
}
 
開發者ID:axonivy,項目名稱:project-build-plugin,代碼行數:20,代碼來源:TestStartEngine.java

示例13: shouldExecuteCommand

import org.apache.commons.exec.Executor; //導入依賴的package包/類
@Test
public void shouldExecuteCommand() throws IOException {
    // given
    Executor executor = mock(Executor.class);
    when(executor.getWorkingDirectory()).thenReturn(Paths.get("/my/repo").toFile());
    CommandOutput output = new CommandOutput(executor.getWorkingDirectory().getName())
            .addOutputLine(new CommandOutputLine("First line."))
            .addOutputLine(new CommandOutputLine("Second line."));
    CommandExecutor commandExecutor = new CommandExecutor(mock(Command.class), executor, () -> output);

    // when
    CommandOutput expectedOutput = commandExecutor.execute();

    // then
    verify(executor).execute(any(CommandLine.class));
    assertThat(expectedOutput.getRepositoryName()).isEqualTo("repo");
    assertThat(expectedOutput.getOutputLines().stream().map(CommandOutputLine::getLine).collect(toList()))
            .containsExactly("First line.", "Second line.");
}
 
開發者ID:yu55,項目名稱:yagga,代碼行數:20,代碼來源:CommandExecutorTest.java

示例14: getVersion

import org.apache.commons.exec.Executor; //導入依賴的package包/類
public static String getVersion() {

		try {
			URL scriptURL = PythonCLIProbe.class.getResource(versionScriptFile);
			File sf = new File(scriptURL.toURI());
			String scriptPath = sf.getAbsolutePath();

			CommandLine commandLine = CommandLine.parse(PythonCLIProcessor.pythonExecutable + " " + scriptPath);

			DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
			Executor executor = new DefaultExecutor();

			// put a watchdog with a timeout
			ExecuteWatchdog watchdog = new ExecuteWatchdog(new Long(timeout_seconds) * 1000);
			executor.setWatchdog(watchdog);

			ByteArrayOutputStream os = new ByteArrayOutputStream();
			PumpStreamHandler psh = new PumpStreamHandler(os);
			executor.setStreamHandler(psh);

			executor.execute(commandLine, resultHandler);
			resultHandler.waitFor();
			int exitVal = resultHandler.getExitValue();
			if (exitVal != 0) {
				return null;
			}

			return (os.toString());

		}
		catch (Exception e) {
			return null;
		}
	}
 
開發者ID:52North,項目名稱:movingcode,代碼行數:35,代碼來源:PythonCLIProbe.java

示例15: applyFileMode

import org.apache.commons.exec.Executor; //導入依賴的package包/類
private static void applyFileMode(final File file, final FileMode fileMode)
        throws MojoExecutionException {

    if (OS.isFamilyUnix() || OS.isFamilyMac()) {
        final String smode = fileMode.toChmodStringFull();
        final CommandLine cmdLine = new CommandLine("chmod");
        cmdLine.addArgument(smode);
        cmdLine.addArgument(file.getAbsolutePath());
        final Executor executor = new DefaultExecutor();
        try {
            final int result = executor.execute(cmdLine);
            if (result != 0) {
                throw new MojoExecutionException("Error # " + result + " while trying to set mode \""
                        + smode + "\" for file: " + file.getAbsolutePath());
            }
        } catch (final IOException ex) {
            throw new MojoExecutionException("Error while trying to set mode \"" + smode + "\" for file: "
                    + file.getAbsolutePath(), ex);
        }
    } else {
        file.setReadable(fileMode.isUr() || fileMode.isGr() || fileMode.isOr());
        file.setWritable(fileMode.isUw() || fileMode.isGw() || fileMode.isOw());
        file.setExecutable(fileMode.isUx() || fileMode.isGx() || fileMode.isOx());
    }
}
 
開發者ID:fuinorg,項目名稱:event-store-maven-plugin,代碼行數:26,代碼來源:EventStoreDownloadMojo.java


注:本文中的org.apache.commons.exec.Executor類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。