Question

I have a simple java program with main method to execute. While executing I need to pass some arguments as shown below:

class Main{
    public static void main(String[] args) {
        initializeBaseClass();
}

Running above program as:

java -classpath C;/test/sample.jar -mainClass com.test.SampleTest

It executes and runs successfully.

Now I need to run same from Mule esb. Which is the best way to invoke this and execute from mule esb.

I have tried with adding java component and adding property key/value, it doesn't work. How do I pass arguments from mule? Is there any better way of doing it by using Spring or scripting languages, or MEL?

Was it helpful?

Solution

    <spring:beans>
        <spring:bean id="transmission" class="com.test.InvokeMain"/>
    </spring:beans>
    <flow name="test" ...
       <set-payload value="-mainclass com.test.Abcd -driver org.hsqldb.jdbc.JDBCDriver/>
       <invoke object-ref="transmission" method="invoke" methodArgumentTypes="java.lang.String" methodArguments="#[payload]" name="transmissionAPI"/>
    </flow>

and main class is:

import com.test.Main;
public class InvokeTransmission {

    public void invoke(String params){
        String[] res = params.split("\\s+");
        Main.main(res);
    }
}

OTHER TIPS

Why don't you use Mule stdio IN component to take input in mule studio... for example :- in Mule flow :-

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:mulexml="http://www.mulesoft.org/schema/mule/xml" xmlns:jdbc-ee="http://www.mulesoft.org/schema/mule/ee/jdbc" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:stdio="http://www.mulesoft.org/schema/mule/stdio" xmlns:jdbc="http://www.mulesoft.org/schema/mule/jdbc" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:spring="http://www.springframework.org/schema/beans" xmlns:core="http://www.mulesoft.org/schema/mule/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="EE-3.4.2" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
http://www.mulesoft.org/schema/mule/stdio http://www.mulesoft.org/schema/mule/stdio/current/mule-stdio.xsd
http://www.mulesoft.org/schema/mule/ee/jdbc http://www.mulesoft.org/schema/mule/ee/jdbc/current/mule-jdbc-ee.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
    <stdio:connector name="stdio" messageDelayTime="1000" promptMessage="Enter a parameter :" doc:name="STDIO" validateConnections="true"></stdio:connector>


    <flow name="InputFlow" doc:name="InputFlow"> 
        <stdio:inbound-endpoint system="IN" responseTimeout="10000" connector-ref="stdio" doc:name="STDIO"></stdio:inbound-endpoint>  
       <custom-transformer class="InputProccesser" doc:name="Java"/> 
       <logger message="Payload is :- #[message.payload]" level="INFO" doc:name="Logger"/>  

    </flow>

</mule>

Now we have used <custom-transformer class="InputProccesser" doc:name="Java"/> in the flow where InputProccesser is the java class which will process the input message ...

Now, you create a java class named InputProccesser :-

import java.util.List;
import java.util.Map;

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class InputProccesser extends AbstractMessageTransformer {

   @Override
   public Object transformMessage(MuleMessage message, String outputEncoding)
               throws TransformerException {

       String mes=(String) message.getPayload();
       int input=0;  
       try
       {       
        input =  Integer.parseInt(mes);

      System.out.println(" Your input variable is "+input);

       return input;
       }

       catch(NumberFormatException nFE) 
          { 

           System.out.println("Please enter a number");
     return input;
         }


   }

}

Now you can modify the java class as per your requirement to process the input message and can use Mule stdio component for taking input after running the mule application

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top