Developers, Developers, Developers! Maksim Sorokin IT Blog

29Jul/11Off

Mule ESB: Building a Self-Contained Standalone Web App with Maven

This example demonstrates how to build standalone self-contained Mule ESB web application, which can be deployed to a Java web container, e.g. Tomcat. The sources used are taken from Mule ESB "hello" example, which is distributed together with Mule.

You can obtain the sources from this post on my github:
https://github.com/mah01/examples/tree/master/muleHelloStandalone

I will just show the keypoints of the application and changes, that were different from "hello" application in Mule distributable.

First of all, the pom.xml file. We download all required Mule dependencies in order to run the example. Note, that we use Mule Maven repository, because for some reason Mule artifacts has transitive dependencies on OSGified versions of certain libraries, which are not contained in Maven Central:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>dk.sorokin.maksim</groupId>
  <artifactId>muleHelloStandalone</artifactId>
  <version>1.0.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>Self-Contained Standalone Mule Hello World</name>
  <description>The sources for this application were copied from Mule distributable</description>

  <properties>
    <mule.version>3.1.2</mule.version>
  </properties>

  <build>
    <resources>
      <resource>
        <directory>src/main/app</directory>
      </resource>
      <resource>
        <directory>src/main/resources</directory>
      </resource>
    </resources>
  </build>

  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>1.8.1</version>
    </dependency>

    <dependency>
      <groupId>org.mule</groupId>
      <artifactId>mule-core</artifactId>
      <version>${mule.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mule</groupId>
      <artifactId>mule-core</artifactId>
      <version>${mule.version}</version>
      <scope>test</scope>
      <type>test-jar</type>
    </dependency>

    <dependency>
      <groupId>org.mule.modules</groupId>
      <artifactId>mule-module-builders</artifactId>
      <version>${mule.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mule.modules</groupId>
      <artifactId>mule-module-scripting</artifactId>
      <version>${mule.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mule.transports</groupId>
      <artifactId>mule-transport-vm</artifactId>
      <version>${mule.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mule.transports</groupId>
      <artifactId>mule-transport-servlet</artifactId>
      <version>${mule.version}</version>
    </dependency>

    <dependency>
      <groupId>org.mule.transports</groupId>
      <artifactId>mule-transport-stdio</artifactId>
      <version>${mule.version}</version>
    </dependency>
  </dependencies>

  <repositories>
    <repository>
      <id>mule-deps</id>
      <name>Mule Dependencies</name>
      <url>http://dist.codehaus.org/mule/dependencies/maven2</url>
    </repository>
  </repositories>
</project>

And then we also add log4j.properties to src/main/java:

log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %-5p %x - %m\n

And our web.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0">
  <display-name>mule.esb</display-name>

  <context-param>
    <param-name>org.mule.config</param-name>
    <param-value>mule-config.xml</param-value>
  </context-param>

  <listener>
    <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
  </listener>
</web-app>

All other sources are copied directly from Mule "hello" example (the whole src folder).

Build the application with maven with mvn clean install and deploy the war to Tomcat (or other container).
Then go to http://localhost:8888/?name=Max. You should see the following message:

Hello Max, how are you?
Comments (0) Trackbacks (0)

Sorry, the comment form is closed at this time.

Trackbacks are disabled.