Steps to Call a Mule flow from Java class

3.1k Views Asked by At

I am new to Mule and I need to call a flow from Java class. Can anyone give steps please?

2

There are 2 best solutions below

1
RamakrishnaN On

you can do it by following below steps

1) In order to make a call, first your flow should have the VM as inbound endpoint.

2) in your java code, get the Mulecontext from MuleEventContext.

3) get the client() from MuleContext then use send method. below is the sample code

MuleContext mContext = eventContext.getMuleContext();
    mContext.getClient().send(VM URL, payload);

The VM URL in the send() is the name of the VM Queue path (sometimes you may need to prefix it with 'vm://...' - example: 'vm://myVMQueuePath'.

Any issues let me know.

0
Tushar Koley On

I have tried this is it did work. I have created java class inside any-point studio and called the java from a main flow. Java is calling the second flow using VMQ. Here is my Java Class. Hope this helps

package mulewithjava;

import org.mule.api.MuleContext;
import org.mule.api.MuleEventContext;
import org.mule.api.MuleMessage;
import org.mule.api.lifecycle.Callable;

public class CallMuleFlow implements Callable {     
    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {
        // TODO Auto-generated method stub
        
        String  payload = (String) eventContext.getMessage().getPayload();  
        
        MuleContext mContext = eventContext.getMuleContext();
        
        MuleMessage msg= eventContext.getMessage();
        
        mContext.getClient().send("vm://VMQ", msg);
                        
        return "After Flow Calling";
    }
}