天天看點

Mule安裝與開發部署一個簡單例子

    本文介紹如何安裝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);

}

    (2)在這個包下建立類MyTest,代碼如下:

public class MyTest implements MyTestInterface

{

  public String hello(String aname)

  {

    return "you are: " + aname;

  }

    (3)在工程下建立檔案夾conf用于存放工程配置檔案,建立檔案myhello-config.xml,内容如下:

<?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>

    (4)至此,一個Mule服務就開發完成了,是不是很容易?接下來就部署這個服務。右擊該工程,點選“export”,将工程導出到一個目錄,命名為MyHello.jar,然後将這個jar包放到C:\Mule\mule-2.1.2\lib\user下(我的Mule安裝到C:\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

    然後輕按兩下這個檔案就可以運作MyHello服務了,允許結果截圖如下:

    至此,一個簡單的Mule服務就開發完成了,是不是很簡單啊?接下來一篇文章講解如何編譯和導入Mule自帶的例子到elicpse工程下。

     本文轉自panpan3210 51CTO部落格,原文連結:http://blog.51cto.com/panpan/132270,如需轉載請自行聯系原作者