running mule from java

14
By Anirban Sen Chowdhary

Upload: anirban-sen-chowdhary

Post on 22-Jul-2015

225 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Running mule from java

By Anirban Sen Chowdhary

Page 2: Running mule from java

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

Page 3: Running mule from java

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

Page 4: Running mule from java

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

Page 5: Running mule 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.

Page 6: Running mule from java

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

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

Page 7: Running mule from java

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

Page 8: Running mule from java

.

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" });

Page 9: Running mule from java

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

Page 10: Running mule from java

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

Page 11: Running mule from java

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

Page 12: Running mule from java

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

Page 13: Running mule from java

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..Hope you enjoyed this little trick …I have also posted this on my blog :-http://anirbansenchowdhary.com/blog/?p=201

Happy coding and share your knowledge everywhere

Page 14: Running mule from java