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
Newsletter
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

  • Giving AI Agents Safe Access to Legacy Systems: Introducing legacy2mcp
  • Evals in CI: How to Catch Agent Regressions Before Production Does
  • How to Build Your Own AI SRE From Scratch
  • Why Kotlin or Java Is the Wrong Question For Enterprises

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  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
·
Aug. 18, 17 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
11.4K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

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

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • 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