Java 从mule中的shell脚本获取返回值
是否有一种方法可以在mule负载中获取shell脚本的返回值。 当我试图获取值时,它会返回“java.lang”。UNIXProcess@d58c32b“ 我不熟悉这一点,我有没有办法从对象中获得价值 我创建了一个示例shell脚本Java 从mule中的shell脚本获取返回值,java,shell,mule,Java,Shell,Mule,是否有一种方法可以在mule负载中获取shell脚本的返回值。 当我试图获取值时,它会返回“java.lang”。UNIXProcess@d58c32b“ 我不熟悉这一点,我有没有办法从对象中获得价值 我创建了一个示例shell脚本 is_admin() { return 1 } test() { if is_admin; then echo 0 else echo 1 fi } test; 下面是我用来调用这个shell脚本的流程: <flow name="pythontestFl
is_admin() {
return 1
}
test()
{
if is_admin;
then echo 0
else
echo 1
fi
}
test;
下面是我用来调用这个shell脚本的流程:
<flow name="pythontestFlow">
<http:listener config-ref="HTTP_Listener_Configuration1" path="/" doc:name="HTTP"/>
<scripting:component doc:name="Script">
<scripting:script engine="Groovy"><![CDATA[def command="/home/integration/scriptest/test.sh"
command.execute()]]></scripting:script>
</scripting:component>
</flow>
感谢要获取执行的输出消息,您必须在脚本中打印此消息(使用文本功能),并将其分配给有效负载: 在Groovy中:
payload = "/home/integration/scriptest/test.sh".execute().text
您的流程:
<flow name="pythontestFlow">
<http:listener config-ref="HTTP_Listener_Configuration1" path="/" doc:name="HTTP"/>
<scripting:component doc:name="Script">
<scripting:script engine="Groovy"><![CDATA[payload = "/home/integration/scriptest/test.sh".execute().text]]></scripting:script>
</scripting:component>
<logger level="INFO" doc:name="Logger" message="#[payload]"/>
</flow>
如果只需要返回代码:
Groovy:
payload = "/home/integration/scriptest/test.sh".execute().exitValue()
爪哇:
你能更明确地告诉我你尝试了什么吗?
test
对于shell函数名来说不是一个好的选择,因为它也是一个内置函数名。我更改了名称,它没有任何区别。这给了我一个空的负载。我更新了帖子,你必须打印你需要的,返回代码或执行消息。还要尝试从脚本返回一个字符串,比如:echo“Hello,world!”我只需要从代码返回,所以我使用下面的代码,但它会给我“process has not exit”errorPost updated,使用这个简单的groovy脚本试试,它正在工作:“/home/integration/scriptest/test.sh”。execute()。textyup我尝试了这个,它工作了“payload=”/home/integration/scriptest/test.sh“.execute().text”需要将其分配给有效负载
public class CommandExec implements Callable{
@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec("/your_path/test.sh");
int returnCode = proc.waitFor();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
StringBuffer finalResult = new StringBuffer();
finalResult.append("results:" + "\n");
String results = "";
while ((results = stdInput.readLine()) != null) {
finalResult.append(results + "\n");
}
finalResult.append(" errors:");
String errors = "";
while ((errors = stdError.readLine()) != null) {
finalResult.append(errors + "\n");
}
return finalResult;
}
}
payload = "/home/integration/scriptest/test.sh".execute().exitValue()
return returnCode;