Executing Shell Scripts From Mule
Learn how to execute shell scripts from Mule with a custom Java component so you can perform tasks directly, rather than using wrapper components.
Join the DZone community and get the full member experience.
Join For FreeSometimes it is better to perform a task directly with the help of the OS rather than using wrapper components. For example, if we just want to move a file from one directory to another without any transformation, we can avoid loading the file into the memory and can move the file directly using a shell script.
Objective: Executing shell scripts from a Mule custom Java component.
In this tutorial, I'll pass the filename from HTTP Post to the flow, and later, my custom Java component will execute the shell script command to move the file.
Steps
1. Create a flow by adding an HTTP Listener and do the basic configuration.
<flow name="fileMoverFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/moveFile" allowedMethods="POST" doc:name="HTTP"/>
</flow>
2. Now create a .sh (or any extension) file and put your commands into it. You can dynamically pass values into this script file using a template component. For example: you can pass the file name to this script by putting #[payload] in the script, then parsing it through a template component.
I created a file, the content of which is
mv /input/#[payload] /output/
As you can see, this script has an MEL expression in it. To populate values through MEL expressions, we ll pass this script through the template.
Now the flow will become
<flow name="fileMoverFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/moveFile" allowedMethods="POST" doc:name="HTTP"/>
<parse-template location="scripts/fileMover.sh" doc:name="Parse Template"/>
</flow>
3. Now, I create a Java Class which will get shell script commands as a string input and will execute them.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
public class ScriptExecuter extends AbstractMessageTransformer {
@Override
public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
// this is your script in a string
String script = (String) message.getPayload();
List<String> commandList = new ArrayList<>();
commandList.add("/bin/sh");
ProcessBuilder builder = new ProcessBuilder(commandList);
builder.redirectErrorStream(true);
Process shell;
try {
shell = builder.start();
try (OutputStream commands = shell.getOutputStream()) {
commands.write(script.getBytes());
}
// read the outcome
try (BufferedReader reader = new BufferedReader(new InputStreamReader(shell.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
// check the exit code
int exitCode = shell.waitFor();
System.out.println("EXIT CODE: " + exitCode);
} catch (IOException | InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "Success";
}
}
4. Now, place a Groovy component and call the script executer class to run your script.
Th final flow is as follows:
<flow name="fileMoverFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/moveFile" allowedMethods="POST" doc:name="HTTP"/>
<parse-template location="scripts/fileMover.sh" doc:name="Parse Template"/>
<set-payload value="#[payload:java.lang.String]" doc:name="Set Payload"/>
<custom-transformer class="com.test.ScriptExecuter" doc:name="Execute Script"/>
</flow>
Opinions expressed by DZone contributors are their own.
Comments