Back to the Mule #1

Three years ago I used Mule as Enterprise Service Bus in a Java project; its goal was to  enable interoperability among different IT worlds. A month ago I had to do in a new project a thing very close to that I did 3 years before. Panic for me! I have just 2k of RAM in my mind 🙂 I forgot how Mule works, but I forgot how easy is to use it. I had to search as many samples as I can because I’m the architect and I had to show to a customer that his WEB Portal (built on Servlet + JSP and Hibernate) can send and receive data to/from  his AS400 ERP. The acknowledge was to publish Web Service through a BUS and avoid to use JDBC query directly within the portal. So the goal was to publish Web Services through Mule and add to Mule several components able to read and write  to a DB2 server with standard JDBC statements. Tools I used for this Technical POC (proof of concepts) are Eclipse (Helios), Mule 2.0.2 and a Postgres 8.3 DB on a Ubuntu 10.10 distro; I know I talked before about DB2, but I did miss a detail: I can’t use customer’s enviroment due to security policies.

Installing Mule

After I downloaded the 2.0.2 Mule version I unzipped its content and I added

export MULE_HOME=/home/roby/software/mule-2.0.2
 

at the end of my .bashrc file.

Server side programming

After that I need to implement a Web service in order to publish a service callable from the Portal.

public class ADUserDTO implements Serializable{
	private static final long serialVersionUID = 1L;
	protected String userId=null;
	protected String clientId=null;
	protected String orgId=null;
	protected Date birthDay=null;
	protected String firstname=null;
	protected String lastname=null;

	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	};
 //... other getters and setters
}
 

This DTO will be managed by a simple interface:

. . .
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface ADUserService {
	@WebMethod
	public @WebResult List<ADUserDTO> listAll(@WebParam String clientId);
	@WebMethod
	public @WebResult ADUserDTO read(@WebParam String userId);
	@WebMethod
	public @WebResult String  create(@WebParam ADUserDTO user);
}

This interface has to be “annotated” with standard Java JWS annotation. I think that there is nothing to be explained in this code. But it needs a concrete implementation class; so let’s see how it can be.

public class ADUserServiceImpl implements ADUserService {
	protected ADUserDAO dao=null;
	@Override
	public List<ADUserDTO> listAll(String clientId) {
		List<ADUserDTO> res=null;
		res=this.getDao().listAll(clientId);
		return res;
	}
	@Override
	public ADUserDTO read(String userId) {
		ADUserDTO dto=null;
		dto=this.getDao().read(userId);
		return dto;
	}
	@Override
	public String create(ADUserDTO user) {
		String id=null;
                id=this.getDao().create(user);
		return id;
	}
	public ADUserDAO getDao() {
		return dao;
	}
	public void setDao(ADUserDAO dao) {
		this.dao = dao;
	}
}

Leaving details about the DAO class we can assume that the Web Service is almost ready to be published. Thus, next step is to pack all those classes in a Jar. With Eclipse I use to right-click on my project and select Export -> Export … and to choose the Jar file under Java.
Imagine that it works fine, then we have a MyWSMuleProject.jar file in our home directory.

Publishing the Web Service

In order to publish the web service we miss three steps:
1) edit an xml config file under MULE_HOME/conf folder
2) move the MyWSMuleProject.jar file under the MULE_HOME/lib/usr folder
3) run the Mule Server.

Assuming that 2) doesn’t need particular explanations, let’s put our attention on the first step;
we have to define a Mule configuration file. This one below is what I wrote in the wsconfig.xml.

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesource.org/schema/mule/core/2.0"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xmlns:cxf="http://www.mulesource.org/schema/mule/cxf/2.0"
      xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
       http://www.mulesource.org/schema/mule/core/2.0 http://www.mulesource.org/schema/mule/core/2.0/mule.xsd
       http://www.mulesource.org/schema/mule/cxf/2.0 http://www.mulesource.org/schema/mule/cxf/2.0/mule-cxf.xsd">

<spring:beans>
    <spring:bean id="userService" scope="prototype" class="it.newopenlab.pocspringcompletews.biz.ADUserServiceImpl">
        <spring:property name="dao">
          <spring:ref local="userDAO"/>
        </spring:property>
    </spring:bean>
    <spring:bean id="userDAO" class="it.newopenlab.pocspringcompletews.dao.ADUserDAO">
    	<spring:property name="username" value="tad"/>
	<spring:property name="password" value="tad"/>
	<spring:property name="dbUrl" value="jdbc:postgresql://localhost:5432/ob"/>
	<spring:property name="driverClass" value="org.postgresql.Driver"/>
    </spring:bean>
</spring:beans>
	

    <!-- define an cxf endpoint -->
    <cxf:endpoint name="cxf" address="http://localhost:9080/mule/pocspringcompletews"
                  frontend="jaxws" synchronous="true"/>
    <model name="cfx-simple">
        <service name="world">
            <inbound>
               <cxf:inbound-endpoint ref="cxf"/>
            </inbound>
            <component>
		<spring-object bean="userService" />
            </component>
        </service>
    </model>

</mule> 

There are three distinct sections, the first one is that about the xml schemas we are going to use.
The second section defines Spring DI about the classes we implemented.
The last one is where we publish a CXF web service that exposes the methods annotated in the “userService”
Spring bean.
Ok, we are ready to publish it, at this point it is enough to run:

$MULE_HOME>./mule -config ../conf/wsconfig.xml

In order to check if all is working fine use http://localhost:9080/mule/pocspringcompletews?wsdl
url in a browser. If you get the wsdl of our web service it is OK!

Advertisements

About amicidiroberto

Curioso, onesto, polemico ed innamorato della programmazione, del mondo IT, ma soprattutto di mia moglie e delle mie figlie
This entry was posted in Java. Bookmark the permalink.

2 Responses to Back to the Mule #1

  1. rocky says:

    Easy and simple, what about client side of the project?

  2. mattew says:

    Yes, good article but I miss the client WS stubs. How did you do?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s