Running Mule From Java

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Have you wondered that your Mule application can be run from a Java class ?

Yes, it’s true .. You can run your entire Mule flow from a small java class.

So, how can we run a Mule application from Java ????

To start you Mule flow or app from Java, you need to refer Mule Context.

To know more about MuleContext, please visit :- https://www.mulesoft.org/docs/site/current3/apidocs/org/mule/api/MuleEventContext.html

Just like Spring Context, Mule will combine all resource files into a single ApplicationContext, whether they are “pure” Spring files or Mule configuration files.

So let us consider, we have our Mule config file named as MuleTest.xml :-

Untitled

Here you can see set payload component is followed after http inbound endpoint which will send the payload and will be displayed in the browser, which is followed by a logger to log message into console.

Now, let’s create a Java class that will run our Mule application as follows :-

Untitled

As you can see that in the Java class, we are using MuleContext for starting our application.
Here using the following line in the code :-

SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(
“MuleTest.xml”);
We are loading our MuleTest.xml file.

Now, if there would have multiple XML file in our application, we would have used a comma-separated list or an array of configuration files the following :-

SpringXmlConfigurationBuilder configBuilder = new SpringXmlConfigurationBuilder(new String[] { “mule-config.xml”, “another-config.xml” });                

That’s it .. Now let us run our Java class to start our Mule application as follows :-

Untitled

Now, we find that the Mule application is getting deployed in Mule server and running :-

Untitled

So we will be testing our flow by hitting the url :- http://localhost:8060/startfromjava in browser and will get the following result :

Untitled

Also in the console we will get the following log by the logger :-

Untitled

 

That’s it … Now you can try this example by your own and can run your single or multiple Mule XML from you Java code..
Happy coding and share your knowledge everywhere :)

 

 

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Anirban Sen Chowdhary

Anirban Sen Chowdhary is an information technology professional currently working on Java/J2ee, Esb and Integration platform. For more information, please visit http://anirbansenchowdhary.com and you can also follow https://twitter.com/Anir37

You may also like...

1 Response

  1. Yuki says:

    Thanks for the tutorial. I follow it and I am able to run it successfully, but the console returns the following warning. Will you by any chance know how to fix it?

    [main] org.mule.module.ognl.expression.OgnlExpressionEvaluator: OGNL module is deprecated and will be removed in Mule 4.0. Use MEL expressions instead.

Leave a Reply to Yuki Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>