本文介绍如何安装Mule,并且开发一个简单的Mule例子。
1.下载Mule到Mule官方网站下载Mule的社区版本,注意企业版本需要收费,而社区版本已经满足开发需要,并且开发源代码。下载地址是:[url]http://www.mulesource.org/display/MULE/Download[/url]。笔者下载的Mule版本是mule2.1.1。
2.安装需要的软件
(1)安装JDK1.6,这里就不详细描述。
(2)到[url]http://maven.apache.org/[/url] 下载Maven,下载Maven2.0.9版本。如果你只想使用mule,而不想编译mule自带的例子和mule源码,就不需要下载Maven。
(3)设置Maven的repository directory为c:\.m2\repository。如果是windows系统,在dos命令下创建.m2命令:mkdir .m2。
(4)设置环境变量,也可以在界面中设置:
Linux环境:
export JAVA_HOME=/opt/java/jdk
export MAVEN_HOME=/opt/apache/maven-2.0.9
export MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256
export MULE_HOME=/opt/mule
export PATH=$PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin:$MULE_HOME/bin
Windows环境:
set JAVA_HOME=C:\Program Files\Java\jdk
set MAVEN_HOME=C:\Apache\maven-2.0.9
set MAVEN_OPTS=-Xmx512m -XX:MaxPermSize=256
set MULE_HOME=C:\Mule
set PATH=%PATH%;%JAVA_HOME%/bin;%MAVEN_HOME%/bin;MULE_HOME/bin
3.测试安装是否成功
在命令行下输入maven -version查看maven是否安装成功,输入mule查看mule是否安装成功。如果成功,就可以开始第一个Mule应用开发了。
4.开发第一个mule例子——Hello
(1)新建一个eclipse java工程,工程名称是:MyHello,创建包org.foo,在这个包下新建接口(interface) MyTestInterface,代码如下:
package org.foo;
public interface MyTestInterface {
public String hello(String aname);
}
public interface MyTestInterface {
public String hello(String aname);
}
package org.foo;
public class MyTest implements MyTestInterface
{
public String hello(String aname)
{
return "you are: " + aname;
}
}
public class MyTest implements MyTestInterface
{
public String hello(String aname)
{
return "you are: " + aname;
}
}
<?
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="
[url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]
[url]http://www.mulesource.org/schema/mule/core/2.1[/url] [url]http://www.mulesource.org/schema/mule/core/2.1/mule.xsd[/url]
[url]http://www.mulesource.org/schema/mule/stdio/2.1[/url] [url]http://www.mulesource.org/schema/mule/stdio/2.1/mule-stdio.xsd[/url]
[url]http://www.mulesource.org/schema/mule/vm/2.1[/url] [url]http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd[/url]" >
< de.ion >
This is a simple component example that demostrates how to expose
a component over multiple transports.
</ de.ion >
<!--
The stdio connector is used to send and receive information via System.in and System.out.
Note this connector is .ly 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 . the connector.
-->
< stdio:connector name ="SystemStreamConnector"
promptMessage ="Please enter yout name: "
messageDelayTime ="1000" />
<!--
The Mule model initialises and manages your UMO components
-->
< model name ="MyHelloSample" >
<!--
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 de.ion of all the parameters.
-->
< service name ="MyHelloUMO" >
<!-- any number of endpoints can be added to an inbound router -->
< inbound >
< stdio:inbound-endpoint system ="IN" />
<!-- vm:inbound-endpoint path="echo"/-->
</ inbound >
< component class ="org.foo.MyTest" />
<!--
An outbound router can have .e or more router configurations that can be
invoked depending . business rules, message contents, headers or any other
criteria. The pass-through-router is a router that automatically passes
on every message it receives
-->
< outbound >
< pass-through-router >
< stdio:outbound-endpoint system ="OUT" />
</ pass-through-router >
</ outbound >
</ service >
</ model >
</ mule >
< 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="
[url]http://www.springframework.org/schema/beans[/url] [url]http://www.springframework.org/schema/beans/spring-beans-2.5.xsd[/url]
[url]http://www.mulesource.org/schema/mule/core/2.1[/url] [url]http://www.mulesource.org/schema/mule/core/2.1/mule.xsd[/url]
[url]http://www.mulesource.org/schema/mule/stdio/2.1[/url] [url]http://www.mulesource.org/schema/mule/stdio/2.1/mule-stdio.xsd[/url]
[url]http://www.mulesource.org/schema/mule/vm/2.1[/url] [url]http://www.mulesource.org/schema/mule/vm/2.1/mule-vm.xsd[/url]" >
< de.ion >
This is a simple component example that demostrates how to expose
a component over multiple transports.
</ de.ion >
<!--
The stdio connector is used to send and receive information via System.in and System.out.
Note this connector is .ly 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 . the connector.
-->
< stdio:connector name ="SystemStreamConnector"
promptMessage ="Please enter yout name: "
messageDelayTime ="1000" />
<!--
The Mule model initialises and manages your UMO components
-->
< model name ="MyHelloSample" >
<!--
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 de.ion of all the parameters.
-->
< service name ="MyHelloUMO" >
<!-- any number of endpoints can be added to an inbound router -->
< inbound >
< stdio:inbound-endpoint system ="IN" />
<!-- vm:inbound-endpoint path="echo"/-->
</ inbound >
< component class ="org.foo.MyTest" />
<!--
An outbound router can have .e or more router configurations that can be
invoked depending . business rules, message contents, headers or any other
criteria. The pass-through-router is a router that automatically passes
on every message it receives
-->
< outbound >
< pass-through-router >
< stdio:outbound-endpoint system ="OUT" />
</ pass-through-router >
</ outbound >
</ service >
</ model >
</ mule >
(5)运行mule服务,有两种方式。第一种比较直接和简单,在命令行中输入:mule -config conf/myhello-config.xml即可。第二种,在MyHello工程文件目录下新建一个文件myHello.bat,内容如下:
@echo off
setlocal
REM There is no need to call this if you set the MULE_HOME in your environment properties
REM but you must also define MULE_LIB for the example (see below)
REM or specify the config as a file: URI (see README.txt)
if "%MULE_HOME%" == "" SET MULE_HOME=%~dp0..\..
if "%MULE_BASE%" == "" SET MULE_BASE=%MULE_HOME%
REM This extends the classpath to include the configuration directory
REM Any changes to the files in .\conf will take precedence over those deployed to %MULE_HOME%\lib\user
SET MULE_LIB=.\conf
call "%MULE_BASE%\bin\mule.bat" -config myhello-config.xml
setlocal
REM There is no need to call this if you set the MULE_HOME in your environment properties
REM but you must also define MULE_LIB for the example (see below)
REM or specify the config as a file: URI (see README.txt)
if "%MULE_HOME%" == "" SET MULE_HOME=%~dp0..\..
if "%MULE_BASE%" == "" SET MULE_BASE=%MULE_HOME%
REM This extends the classpath to include the configuration directory
REM Any changes to the files in .\conf will take precedence over those deployed to %MULE_HOME%\lib\user
SET MULE_LIB=.\conf
call "%MULE_BASE%\bin\mule.bat" -config myhello-config.xml
至此,一个简单的Mule服务就开发完成了,是不是很简单啊?接下来一篇文章讲解如何编译和导入Mule自带的例子到elicpse工程下。
本文转自panpan3210 51CTO博客,原文链接:http://blog.51cto.com/panpan/132270,如需转载请自行联系原作者