diff --git a/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/ScriptExecutor.java b/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/ScriptExecutor.java new file mode 100644 index 0000000..165f691 --- /dev/null +++ b/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/ScriptExecutor.java @@ -0,0 +1,18 @@ +package com.dianping.platform.slb.agent.shell; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * dianping.com @2015 + * slb - soft load balance + *

+ * Created by leon.li(Li Yang) + */ +public interface ScriptExecutor { + + int execute(String command, OutputStream stdOutput, OutputStream errorOutput) throws IOException; + + void kill(); + +} diff --git a/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/impl/DefaultScriptExecutor.java b/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/impl/DefaultScriptExecutor.java new file mode 100644 index 0000000..d6a35f4 --- /dev/null +++ b/camel-agent/src/main/java/com/dianping/platform/slb/agent/shell/impl/DefaultScriptExecutor.java @@ -0,0 +1,46 @@ +package com.dianping.platform.slb.agent.shell.impl; + +import com.dianping.platform.slb.agent.shell.ScriptExecutor; +import org.apache.commons.exec.*; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * dianping.com @2015 + * slb - soft load balance + *

+ * Created by leon.li(Li Yang) + */ +public class DefaultScriptExecutor implements ScriptExecutor { + + private Executor m_executor = new DefaultExecutor(); + + private ExecuteWatchdog m_executeWatchdog; + + public DefaultScriptExecutor() { + this(300000); + } + + public DefaultScriptExecutor(long watchDogMills) { + m_executeWatchdog = new ExecuteWatchdog(watchDogMills); + m_executor.setWatchdog(m_executeWatchdog); + } + + @Override + public int execute(String command, OutputStream stdOutput, OutputStream errorOutput) throws IOException { + PumpStreamHandler streamHandler = new PumpStreamHandler(stdOutput, errorOutput); + + m_executor.setExitValues(null); + m_executor.setStreamHandler(streamHandler); + + CommandLine commandLine = CommandLine.parse(command); + + return m_executor.execute(commandLine); + } + + @Override + public void kill() { + m_executeWatchdog.killedProcess(); + } +}