Mule安装,配置,以及简单例子的运行

最近开始接触ESB。在开源ESB领域,出现最早,使用广泛的当属Mule了。但是很遗憾,在网上并没有多少实用的关于ESB的资料,所以在这里记下自己的学习,使用和熟悉的过程。如有不正确之处,欢迎指出,也欢迎讨论。

 

1. 下载,安装与配置

1.1 下载Mule - http://www.mulesoft.org/

1.2 下载Mule IDE - http://www.mulesoft.org/documentation/display/MULEIDE/Mule+IDE+2.0+Installation+Guide

其中,Mule IDE是一个Eclipse插件,其安装与配置在官方站点中有详细的说明,这里不再重复。本人使用的是

Eclipse:MyEclipse 6.5

Mule: mule-2.1.2

操作系统:Windows XP

注意:Eclipse中的JDK最好与系统安装的JDK保持一致。

 

2. 运行例子

2.1 直接运行

%Mule_Home%\examples目录下有好几个例子,可直接运行,比如运行echo,直接运行echo目录下的echo.bat即可,这里提供了三种模式,分别是命令行,以及基于Axis和Cxf的服务方式。下面两图分别是命令行运行,和基于Axis服务的运行效果图,采用后者,需在浏览器中输入以下地址: http://localhost:65081/services/EchoUMO?method=echo

 

2.2 在开发环境中运行

2.2.1 创建Mule Project

输入项目名称,选取Add sample project content,同时在下拉列表中选择Echo Example,然后Finish。

 

2.2.2 运行

创建项目后,可以看看项目结构,然后右键点击conf目录下任一个xml配置文件,Run As -> Mule Server,即可运行,效果图略。

 

3. 开发第一个Mule项目——Hello World

3.1 创建工程 - 创建一个Java Project

 

3.2 添加所需要的JAR包

添加一个lib文件夹,同时把以下目录中所有的JAR包复制到lib文件夹中

-- %Mule_Home%\lib\boot

-- %Mule_Home%\lib\mule

-- %Mule_Home%\lib\opt

并将这些JAR包添加到项目的classpath

 

3.3 编写Java文件

3.3.1 创建接口

3.3.2 创建实现类

/***********************************************************************  * <p>Project Name: test</p>  * <p>File Name: com.thu.afa.mule.demo.HelloImpl.java</p>  * <p>Copyright: Copyright (c) 2010</p>  * <p>Company: <a href="http://afa.thu.com">http://afa.thu.com</a></p>  ***********************************************************************/ package com.thu.afa.mule.demo;  /**  * <p>Class Name: HelloImpl</p>  * <p>Description: </p>  * @author Afa  * @date Jun 26, 2010  * @version 1.0  */ public class HelloImpl implements Hello { 	/* (non-Javadoc) 	 * <p>Title: </p> 	 * <p>Method Name: hello</p> 	 * <p>Description: </p> 	 * @author: Afa 	 * @date: Jun 26, 2010 	 * @see com.thu.afa.mule.demo.Hello#hello(java.lang.String) 	 * 	 * @param name 	 * @return 	 */ 	public String hello(String name) 	{ 		return name; 	}  } 

 

3.4 编写配置文件

在项目目录下创建conf文件夹,并创建文件:hello-config.xml。当然,你也可以从mule的例子中复制一个配置文件到此,然后做适当的修改。下面的本例子的配置:

<?xml version="1.0" encoding="UTF-8"?> <mule xmlns="http://www.mulesource.org/schema/mule/core/2.1"        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:spring="http://www.springframework.org/schema/beans"        xmlns:stdio="http://www.mulesource.org/schema/mule/stdio/2.1"        xmlns:vm="http://www.mulesource.org/schema/mule/vm/2.1"        xsi:schemaLocation="                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd                http://www.mulesource.org/schema/mule/core/2.1 http://www.mulesource.org/schema/mule/core/2.1/mule.xsd                http://www.mulesource.org/schema/mule/stdio/2.1 http://www.mulesource.org/schema/mule/stdio/2.1/mule-stdio.xsd                http://www.mulesource.org/schema/mule/vm/2.1 http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd">          <!--         The system stream connector is used to send and receive information via the         System.in and System.out. Note this connector is only really useful for testing         purposes.         promptMessage - is what is written to the console         messageDelayTime - is the time in milliseconds before the user is prompted again.         These properties are set as bean properties on the connector.     -->     <stdio:connector name="SystemStreamConnector"         promptMessage="Please enter yout name: "         messageDelayTime="1000"/>     <!--         The Mule model initialises and manages your UMO components     -->     <model name="HelloSample">         <!--             A Mule service defines all the necessary information about how your components will             interact with the framework, other components in the system and external sources.             Please refer to the Configuration Guide for a full description of all the parameters.         -->         <service name="HelloUMO">             <inbound>                 <stdio:inbound-endpoint system="IN" />             </inbound>                          <component class="com.thu.afa.mule.demo.HelloImpl"/>              <outbound>                 <pass-through-router>                 	<stdio:outbound-endpoint system="OUT" />                 </pass-through-router>             </outbound>         </service>          </model> </mule> 

  

3.5 运行

右键点击hello-config.xml,Run As -> Mule Server,即可运行,运行结果如下:

 

4. 小结

本人最近一直在看Mule的官方文档以及所提供的例子,但由于是初学,所以理论性的东西说的就比较少了,如有不正确之处,欢迎指出,也欢迎一起探讨。

posted @ 2011-03-23 21:26  六不朽  阅读(3251)  评论(0编辑  收藏  举报