Mule on Tomcat

In this post I will show you how it is possible to deploy Mule on a web container such as Tomcat because running Mule as a standalone service is often the more popular and recommended approach. Both standalone and web based mule applications have their benefits and drawbacks but which one to use is mainly dependent on the project requirements and use-cases.

This post aims to guide you through using Mule API 3.5.1 onto Tomcat server in web based project in Eclipse.

If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.
Prerequisites

The following configurations are required in order to run the application
Eclipse
JDK 1.7 (if you use JDK 8 then use Tomcat 8)
Tomcat 7
Have maven installed and configured
Mule dependencies in pom.xml
Now we will see the below steps how to create a maven based spring project in Eclipse
First we will create service project
Step 1. Create a maven based web project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

maven-arctype-webapp
Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : mule
Step 2. Modify the pom.xml file as shown below.

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.roytuts</groupId>
    <artifactId>mule</artifactId>
    <packaging>war</packaging>
    <version>0.0.1-SNAPSHOT</version>
    <name>mule Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <jdk.version>1.7</jdk.version>
        <mule.version>3.5.1</mule.version>
        <junit.version>4.11</junit.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-builders</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.modules</groupId>
            <artifactId>mule-module-spring-config</artifactId>
            <version>${mule.version}</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <finalName>mule</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.7 from the next window. Click on Finish then Ok.

Step 4. Create mule-webapp.xml file under src/main/resource folder with below source code

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:servlet="http://www.mulesoft.org/schema/mule/servlet"
    xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
http://www.mulesoft.org/schema/mule/servlet http://www.mulesoft.org/schema/mule/servlet/current/mule-servlet.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">
    <flow name="tomcat-mule-flow">
        <servlet:inbound-endpoint path="hello"
            responseTimeout="10000" doc:name="Servlet" />
        <set-payload value="#['Hello World!']" doc:name="Set Payload" />
    </flow>
</mule>

Step 5. Modify generated web.xml file with below source code

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <context-param>
        <param-name>org.mule.config</param-name>
        <param-value>mule-webapp.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.mule.config.builders.MuleXmlBuilderContextListener</listener-class>
    </listener>
    <servlet>
        <servlet-name>muleServlet</servlet-name>
        <servlet-class>org.mule.transport.servlet.MuleReceiverServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>muleServlet</servlet-name>
        <url-pattern>/app/*</url-pattern>
    </servlet-mapping>
</web-app>

Step 6. Create index.jsp file under src/main/webapp folder

<html>
<body>
    <h2>Mule Webapp Example</h2>
    <p>
        Click <a href="app/hello">here</a> to hit a Mule HTTP Servlet
        inbound endpoint.
    </p>
</body>
</html>

Step 7. Now run the mule webapp on tomcat server, you will get below output.

mule on tomcat server
Step 8. Click on the link “here” you will see the below output.

mule on tomcat server

Thanks for reading.

Leave a Reply

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