Mule 官方例子研究
Mule 官方例子研究
一、編譯導(dǎo)入Mule自帶的例子
1.準(zhǔn)備
安裝Mule。這里就不介紹mule的安裝了,請參考《Mule安裝部署手冊》。
2. 編譯Mule自帶例子中的Hello例子
使用命令行到目錄:D:\app\mule3.1.1\examples\hello下,輸入:mvn 即可。這里需要安裝Maven;
3. 導(dǎo)入Eclipse工程
輸入命令:mvn eclipse:eclipse 成功后,即可導(dǎo)入eclipse。
4.導(dǎo)入這個工程到eclipse
(1)選擇:File > Import
(2)選擇:展開“General” 選擇“Existing Projects into Workspace”
(3)選擇Browse,導(dǎo)入后,選擇Finish。完成。
5.配置Elipse工程(build path)
(1)右擊工程,選擇“Build Path ->Configure Build”:
?
(2)選擇“Libraries->Add Library”
(3)點擊“User Library”,點擊“Next”
(4)點擊“User Libraries”,點擊“New”
(5)輸入:MULE_LIB,點擊“OK”
(6)點擊“Add JARs”,選擇Mule主目錄下的 \lib\mule,選擇所有的jar包,點擊“Open”
(7)點擊“OK”,點擊“Finish”
(8)點擊“Add Variable”,點擊“Configure Variables”,點擊“New”
(9)輸入:M2_REPO,選擇一個目錄。默認(rèn)地,Maven會在剛才使用mvn命令編譯時,
把下載的jar包放到:C:\Documents and Settings\Administrator\.m2\repository 目錄下,Administrator是我的用戶名,這里我設(shè)置的目錄是C:\.m2\repository,你可以把那個目錄下的所有copy到這個目錄下。 點擊“Folder”,選擇C:\.m2\repository。
?
(10)點擊“OK”,點擊“yes”,點擊“OK”。設(shè)置完成。
6.運行這個工程
?(1)右擊工程,選擇“Run->Run Configurations”
(2)雙擊“Java Application”,把名字改為:Hello,選擇main class為:org.mule.MuleServer。
?? 如圖:
????????????? (3)在Arguments選項表中,在Program Arguments框中輸入-config conf\hello-config.xml
(4)點擊“Apply”,“Run”。運行例子,如圖:
?
備注:
運行“Run as Mule Server”報錯“A Mule runtime distribution must be configured”的解決辦法:在eclipse的classpath界面里面添加libray ;右鍵工程---->properties----->Java Build Path----->Libraries------>Add Library------>Mule Classpath
二、ECHO例子分析
2.1概述
這個演示了“如何通過使用一個簡單的web service flow,讓我們了解Mule ESB 組件;在這個例子中,這個組件被了解是通過使用CXF 的JAX-WS web Service.
?
(上圖描述了MULE2.X 的內(nèi)容。System Stream Connector 和Axis Soap Connector已經(jīng)被普通的http Connector替換,服務(wù)通過使用CXF被從新實現(xiàn),用來處理Soap的請求和響應(yīng));
上圖說明:
1、? 通過兩種方式接入(紅色線):一個是System.in, 一個是Soap(http)方式
2、? 每種接入都可以通過接入器(Connector)經(jīng)過NMR(Normalized Message Router)規(guī)范化的消息路由轉(zhuǎn)發(fā)后,進(jìn)行輸出,目前通過兩種方式:System.out 和Soap(http)方式。
2.2運行應(yīng)用
簡單的拷貝預(yù)構(gòu)建的檔案資料(mule-example-echo.zip)到應(yīng)用文件夾($MULE_HOME/apps),并啟動mule。去通過瀏覽器訪問web service 調(diào)用:
http://localhost:65082/services/EchoUMO/echo/text/hello
??????? 通過在控制臺按“CTR-C”停止mule;
2.3編譯例子
依靠你正在使用的編譯工具(Ant or Maven) ,你能夠通過簡單的運行”ant” 或”mvn”去編譯樣例,這將編譯樣例的類文件,產(chǎn)生一個應(yīng)用的zip文件,拷貝這個zip文件到 ?$MULE_HOME/apps.
2.4 The Echo 組件
Echo Service 是基于一個POJO 組件,該組件是使用JAX-WS 注解方式被注解,并作為一個web service 被暴露出來,該Web Service 是在MULE 使用基于流的配置之上的。組建的初始化如下:
public class Echo
{
??? public String echo(String string)
??? {
??????? return string;
??? }
}
通過增加JAX-WS注解的方法,我們能把該類方法發(fā)布成一個web service, 并具體說明輸入?yún)?shù)和響應(yīng)是怎樣被映射的。
@WebService
public class Echo
{
??? @WebResult(name="text")
??? public String echo(@WebParam(name="text") String string)
??? {
??????? return string;
??? }
}
2.5配置流程(Configuring the Flow)
???????? 配置Service ,先添加<flow>元素到Mule XML 配置文件,并提供name 屬性,用<component>元素具體指定服務(wù)組件的類;對于從Spring注冊庫引用的對象,應(yīng)當(dāng)使用內(nèi)嵌的<spring-object>來代替;
??? <flow name="EchoFlow">
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
</flow>
???????? 類的屬性必須是完全的合格的類路徑名,名稱屬性對服務(wù)來說必須是唯一的名稱。
2.6調(diào)用流程(Invoking the Flow)
???????? 當(dāng)前流程是有效的,但是沒有入口點,通過URL執(zhí)行它,并不能得到反饋信息。首先必須為service配置一個端點endpoint, 以使它能被調(diào)用。當(dāng)運行實例時,通過http發(fā)送一個request去調(diào)用EchoFlow,一個相應(yīng)通過Http channel被返回。Mule Http Transport管理這個交互。
???????? 去配置這個流程,我們已經(jīng)創(chuàng)建一個接入點<inbound-endpoint>,來實現(xiàn)請求相應(yīng)request-response.下面是包括endpoint的EchoFlow配置:
??? <flow name="EchoFlow">
??????? <inbound-endpoint address="http://localhost:65082/services/EchoUMO" exchange-pattern="request-response"/>
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
</flow>
???????? 如果現(xiàn)在去觸發(fā)流程,將會得到一個響應(yīng),它將返回一個URL PATH 給你。然而EchoFlow仍不知道怎么去處理Web Service的調(diào)用,首先你需要去采用一個使用CXF的過濾器。
2.7暴露CXF類型Web Service (Exposing as a Web Service with CXF)
???????? CXF已經(jīng)內(nèi)置了對GET requests 約定的支持,使用的是下面的句法:
http://host/service/OPERATION/PARAM_NAME/PARAM_VALUE
???????? 所以,可以通過輸入下面的URL來運行該樣例:
?????????????????? ???? http://localhost:65082/services/EchoUMO/echo/text/hello
???????? 為了使CXF能夠去處理消息, 像下面去更新EchoFlow
<flow name="EchoFlow">
<inbound-endpoint address="http://localhost:65082/services/EchoUMO" exchange-pattern="request-response"/>
???? <cxf:jaxws-service serviceClass="org.mule.example.echo.Echo"/>
<component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
</component>
</flow>
現(xiàn)在如果去觸發(fā)URL,將會得到適當(dāng)?shù)捻憫?yīng)消息。也可以去請求WSDL,它是組件運行需要的全部的配置信息。去看整體內(nèi)的配置文件,打開目錄” examples/echo/src/main/app/ directory under your Mule installation directory”下的mule-config.xml文件.
2.8添加Stdio端點(Adding a STDIO endpoint)
在MULE 之前的版本中,echo實例支持在命令行輸入信息時的提示,當(dāng)你輸入一些信息時,你正通過System.in 提供輸入數(shù)據(jù)調(diào)用服務(wù),在那時你輸入的數(shù)據(jù)經(jīng)由Systme.out被不停的回應(yīng)回來。MULE STDIO Transport管理這個交互。
去配置STDIO 支持該流程,你需要在你的配置文件中增加STDIO的命名空間,然后使用”one-way”交換模式詳細(xì)制定inbound 和 outbound端點。
??? <flow name="EchoFlow">
??????? <stdio:inbound-endpoint system="IN" exchange-pattern="one-way"/>
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
??????? <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way"/>
</flow>
<outbound>元素允許一個或多個outbound路由元素被配置去控制如何、哪里的消息被發(fā)送,一旦他們被組件處理時。在這個例子中,所有的消息被經(jīng)由System.out發(fā)送。
最后,如果你想去覆默認(rèn)被傳輸具體指定在inbound endpoint上配置,你可以去配置一個連接器。在這個例子中,讓我們覆蓋stdio 傳輸器默認(rèn)的鏈接器,于是提醒用戶輸入下面的內(nèi)容:
<stdio:connector name="SystemStreamConnector"
???????????????? promptMessage="Please enter something: "
???????????????? messageDelayTime="1000"/>
2.9增加多個接入點
有幾個方法對流程增加多個接入點,如果他們是跟隨統(tǒng)一個處理器鏈,你可以使用<composite-source>,像下面所示:
??? <flow name="EchoFlow">
??????? <composite-source>
??????????? <stdio:inbound-endpoint system="IN" exchange-pattern="one-way"/>
??????????? <vm:inbound-endpoint path="echo" exchange-pattern="request-response"/>
??????? </composite-source>
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
</flow>
然而,如果你有明確的端點消息處理器去應(yīng)用,像CXF,一個選擇是去使用復(fù)合流程,在復(fù)合流程中,你引用的一個流程是來自另外的一個流程中。這個方法,你可以中止流程通過端點。看下面的例子:
??? <flow name="EchoStdio">
??????? <stdio:inbound-endpoint system="IN" exchange-pattern="one-way"/>
???? <flow-ref name="EchoComponent"/>
??????? <stdio:outbound-endpoint system="OUT" exchange-pattern="one-way"/>
??? </flow>
?
??? <flow name="EchoComponent">
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
</flow>
?
??? <flow name="EchoWebService">
??????? <inbound-endpoint address="http://localhost:65082/services/EchoUMO" exchange-pattern="request-response"/>
??????? <cxf:jaxws-service serviceClass="org.mule.example.echo.Echo"/>
???? <flow-ref name="EchoComponent"/>
</flow>
EchoStdio 和EchoWebService 提供兩種不同的接入點。他們都死通應(yīng)用執(zhí)行的EchoComponent。
?
2.10使用一個服務(wù)來配置(Configuring using a Service)
???????? 作為選擇,流程能夠使用一個服務(wù)來配置,去配置這個服務(wù),你增加一個<service>元素到你的MULE XML 配置文件中,并提供名字屬性。通過使用<component>元素具體制定服務(wù)組件的類。然后和之前的一樣增加http 端點和消息處理器;
??? <model name="echoSample">
??????? <service name="EchoUMO">
??????????? <inbound>
??????????????? <inbound-endpoint address="http://localhost:65082/services/EchoUMO"
????????????????????????????????? exchange-pattern="request-response">
??????????????????? <cxf:jaxws-service />?????????????????
??????????????? </inbound-endpoint>
??????????? </inbound>
??????????? <component>
??????????????? <singleton-object class="org.mule.example.echo.Echo"/>
??????????? </component>
??????? </service>
</model>
在服務(wù)配置中,對CXF來說ServiceClass是不需要的。這是因為在這個配置中,CXF是能夠自動發(fā)現(xiàn)組件的。基于流的配置目前還不支持,所以serviceClass必須被明確的制定。
三、HelloWorld 例子分析
3.1 概述
???????? 該部分主要描述Hello World例子的配置和行為。
???????? 當(dāng)我們在瀏覽器中輸入預(yù)定義的地址 (http://localhost:8888/?name=Ross) 時 Mule解釋輸入值中“?name=”后的部分,進(jìn)行驗證,使greeeter.java 類去添加一些文本到字符串中,然后傳遞到另外一個flow,第二個flow繼續(xù)添加文本,然后通過瀏覽器傳回來;以上做的這些知識簡單的方式,但是這個例子使用了flows和http傳輸去說明消息路由和轉(zhuǎn)換器在Mule中的引用。
?
3.2運行應(yīng)用
???????? 如果正在使用MULE IDE 環(huán)境,可以創(chuàng)建一個基于Hello World例子的工程,然后通過Eclipse來運行;
如果沒有使用MULE IDE環(huán)境,簡單的拷貝預(yù)構(gòu)建的檔案資料(mule-example-hello-3.0.0.zip)到應(yīng)用文件夾($MULE_HOME/apps),并啟動mule。去通過瀏覽器訪問web service 調(diào)用:
http://localhost:8888/?name=Ross
??????? 通過在控制臺按“CTR-C”停止mule;
3.3編譯例子
同上
3.4如何工作的?
???????? Hello World應(yīng)用被配置在mule-config.xml中,這個文件存在于mule 根目錄下的examples/hello/src/main/app. 該部分主要是通過配置和JAVA資源文件的調(diào)用實現(xiàn)的。
3.5資源文件
消息的文本是從hello-example-messages.properties文件中獲得的,該文件存在于
Message text is taken from the hello-example-messages.properties file in the examples\hello\src\main\resources\messages文件夾, 以下是資源文件的內(nèi)容:
1=, how are you?
2=Hello
3=Please enter your name:
4=Please provide a valid name with at least one character!
在同一個文件夾中,該屬性文件有翻譯的版本可以替換,例如你可以德語的字符串替換英語的字符串。
3.6類
???????? Mule 配置文件喚起兩個JAVA類去處理消息,首先是Greeter類,這個類用一個方法從LocalMessage類去推送字符串“hello”,字符串內(nèi)容來自屬性文件。Greeter類的方法 greet() 然后去追加人名。
public class Greeter
{
??? private String greeting = "";
?
??? public Greeter()
??? {
??????? greeting = LocaleMessage.getGreetingPart1();
??? }
?
??? public Object greet(NameString person)
??? {
??????? Object payload = person;
??????? if (person.isValid())
??????? {
??????????? person.setGreeting(greeting);
??????? }
??????? else
??????? {
??????????? payload = new Exception(LocaleMessage.getInvalidUserNameError());
??????? }
??????? return payload;
??? }
}
???????? 第二個類是ChitChatter , 這個類實現(xiàn)附加字符串”,how are you?”.
public class ChitChatter
{
??? private String chitchat = "";
?
??? public ChitChatter()
??? {
??????? chitchat = LocaleMessage.getGreetingPart2();
??? }
?
??? public void chat(ChatString string)
??? {
??????? string.append(chitchat);
??? }
?
}
?
3.7 把類連接起來
在Hello World中,調(diào)用這些類的配置文件,在composite元素中組成了3個接入點,去接受HTTP、Servlet、和VM請求;
???????? Hollo World flow 使用這個composite元素去獲得在瀏覽器中輸入的名字,調(diào)用greeter類,路由意外的錯誤去分離被調(diào)用的systemErorHandler中的handler.
<flow name="Hello World">
??????? <composite-source>
??????????? <!-- Incoming HTTP requests -->
??????????? <inbound-endpoint address="http://localhost:8888" transformer-refs="HttpRequestToNameString" exchange-pattern="request-response">
??????????????? <not-filter>
??????????? ????????<wildcard-filter pattern="/favicon.ico"/>??
??????????????? </not-filter>
??????????? </inbound-endpoint>
?
??????????? <!-- Incoming Servlet requests -->
??????????? <inbound-endpoint address="servlet://name" transformer-refs="HttpRequestToNameString" exchange-pattern="request-response">
??????????????? <not-filter>
??????????????????? <wildcard-filter pattern="/favicon.ico"/>??
??????????????? </not-filter>
??????????? </inbound-endpoint>
?
??????????? <!-- Incoming VM requests -->
??????????? <vm:inbound-endpoint path="greeter" transformer-refs="StringToNameString" exchange-pattern="request-response"/>
??????? </composite-source>
??????? ...???
</flow>???
?
???????? 接入點元素使用http傳輸 接受接入的消息。在問候者受到消息之前,Transformer-refs屬性指定接入轉(zhuǎn)換器被調(diào)用。該轉(zhuǎn)換器早先在配置文件被定義:
<custom-transformer name="HttpRequestToNameString" class="org.mule.example.hello.HttpRequestToNameString"/>
?
該轉(zhuǎn)換器轉(zhuǎn)換從Http Connector 收到的Http Request 為NameString 對象,該對象是類Greeter的Greet()方法預(yù)期的數(shù)據(jù)類型。當(dāng)你指定逐漸的時候,可以不必指出一個具體類中的一個具體的方法—Mule 能決定基于消息數(shù)據(jù)類型的適合的方法。
Greeter類預(yù)決定對輸入的用戶“問候”后,消息被匹配在端點vm://chitchatter. 這是調(diào)ChitChat flow, 同時調(diào)用定制的轉(zhuǎn)換器NameStringToChatString,轉(zhuǎn)換NameString object為ChatString object.
<flow name="ChitChat">
??????? <vm:inbound-endpoint path="chitchatter" transformer-refs="NameStringToChatString"
??????????? responseTransformer-refs="ChatStringToString" exchange-pattern="request-response"/>
??????? <component class="org.mule.example.hello.ChitChatter"/>
</flow>
?
去更多的演示轉(zhuǎn)換器,ChitChatter類期待一個ChatString對象,于是我們有了一個NameStringToChatString轉(zhuǎn)換器,在組件接收到消息之前,去轉(zhuǎn)換消息有效部分從NameString 為ChatString。消息被接收在vm://chitchatter上,這個端點是關(guān)于Greeter類分發(fā)它的消息。
消息被處理后,該消息被作為Http響應(yīng)信息被發(fā)送,這個組件上的響應(yīng)轉(zhuǎn)換會記錄。甚至認(rèn)為沒有接出提供者,再有一個轉(zhuǎn)換器ChatStringToString,設(shè)置 Even though there is no outbound provider, there is one more transformer, ChatStringToString,它轉(zhuǎn)換有效負(fù)載從ChatString 為 平常的string ,所以他能被Http 傳輸處理,在http響應(yīng)中被顯示。
?
注釋:JAVA類沒有任何的路由邏輯,他們是通過Mule的配置文件連接在一起的,能實現(xiàn)在JAVA類,WebService等之間傳遞消息
3.8 配置Servlet 傳輸
在Web容器中部署MULE是很常見的,使用Web Server 代替Mule Http Transport去管理Http connection。通過Servlet的方式調(diào)用Hello 服務(wù),提交一個Web 表單,如下:
<form method="POST" action="/mule-examples/rest?endpoint=greeter">
??? <input type="text" name="payload"/>
??? <input type="submit" value="POST" />
</form>
使用Servlet傳輸,下面的端點能被添加到配置文件中 (注釋: the REST endpoint currently only works on the servlet transport only since the HttpRequestToParameter transformer only supports servlets.):
<inbound-endpoint address="servlet://name" transformer-refs="HttpRequestToNameString" exchange-pattern="request-response">
??? <not-filter>
??????? <wildcard-filter pattern="/favicon.ico"/>??
??? </not-filter>
</inbound-endpoint>
?
<inbound-endpoint address="servlet://rest" transformer-refs="HttpRequestToParameter StringToNameString" responseTransformer-refs="PlainTextResponseTransformer" exchange-pattern="request
?
3.9 配置VM 傳輸
這個實例也演示在Mule 程序中如何連接服務(wù)、如何使用mule Client
<vm:inbound-endpoint path="greeter" transformer-refs="StringToNameString" exchange-pattern="request-response"/>
然后用Mule Client API 去調(diào)用服務(wù):
MuleClient client = new MuleClient(muleContext);
client.send("vm://greeter", "Ross", null);
MuleMessage response = client.send("vm://greeter", "Ross", null);
System.out.println("response = " + response.getPayload());
?
轉(zhuǎn)載于:https://www.cnblogs.com/GeneralXU/archive/2011/03/29/1998437.html
總結(jié)
以上是生活随笔為你收集整理的Mule 官方例子研究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java为窗体添加滚动条
- 下一篇: Android 体系结构和应用程序组成