DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Trending

  • Testing Strategies for Web Development Code Generated by LLMs
  • Context Rot: Why Your AI Agent Gets Worse the Longer It Works
  • Parallel Kafka Batch Processing With Kotlin Coroutines in Spring Boot
  • Encryption Won't Survive Quantum Computing: What to Do?
  1. DZone
  2. Coding
  3. Languages
  4. Executing Shell Scripts From Mule

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.

By 
Deepak Kushwaha user avatar
Deepak Kushwaha
·
Aug. 18, 17 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
11.3K Views

Join the DZone community and get the full member experience.

Join For Free

Sometimes 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>
shell

Opinions expressed by DZone contributors are their own.

Related

  • Reproducible SadTalker Pipeline in Google Colab for Single-Image, Single-Audio Talking-Head Generation
  • How to Introduce a New API Quickly Using Micronaut
  • Spring Boot GoT: Game of Trace!
  • Data Pipeline Techniques in Action

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook