Mule 官方例子研究
Mule 官方例子研究
一、編譯導入Mule自帶的例子
1.準備
安裝Mule。這里就不介紹mule的安裝了,請參考《Mule安裝部署手冊》。
2. 編譯Mule自帶例子中的Hello例子
使用命令行到目錄:D:\app\mule3.1.1\examples\hello下,輸入:mvn 即可。這里需要安裝Maven;
3. 導入Eclipse工程
輸入命令:mvn eclipse:eclipse 成功后,即可導入eclipse。
4.導入這個工程到eclipse
(1)選擇:File > Import
(2)選擇:展開“General” 選擇“Existing Projects into Workspace”
(3)選擇Browse,導入后,選擇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,選擇一個目錄。默認地,Maven會在剛才使用mvn命令編譯時,
把下載的jar包放到:C:\Documents and Settings\Administrator\.m2\repository 目錄下,Administrator是我的用戶名,這里我設置的目錄是C:\.m2\repository,你可以把那個目錄下的所有copy到這個目錄下。 點擊“Folder”,選擇C:\.m2\repository。
?
(10)點擊“OK”,點擊“yes”,點擊“OK”。設置完成。
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 的內容。System Stream Connector 和Axis Soap Connector已經被普通的http Connector替換,服務通過使用CXF被從新實現,用來處理Soap的請求和響應);
上圖說明:
1、? 通過兩種方式接入(紅色線):一個是System.in, 一個是Soap(http)方式
2、? 每種接入都可以通過接入器(Connector)經過NMR(Normalized Message Router)規范化的消息路由轉發后,進行輸出,目前通過兩種方式:System.out 和Soap(http)方式。
2.2運行應用
簡單的拷貝預構建的檔案資料(mule-example-echo.zip)到應用文件夾($MULE_HOME/apps),并啟動mule。去通過瀏覽器訪問web service 調用:
http://localhost:65082/services/EchoUMO/echo/text/hello
??????? 通過在控制臺按“CTR-C”停止mule;
2.3編譯例子
依靠你正在使用的編譯工具(Ant or Maven) ,你能夠通過簡單的運行”ant” 或”mvn”去編譯樣例,這將編譯樣例的類文件,產生一個應用的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注解的方法,我們能把該類方法發布成一個web service, 并具體說明輸入參數和響應是怎樣被映射的。
@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>元素具體指定服務組件的類;對于從Spring注冊庫引用的對象,應當使用內嵌的<spring-object>來代替;
??? <flow name="EchoFlow">
??????? <component>
??????????? <singleton-object class="org.mule.example.echo.Echo" />
??????? </component>
</flow>
???????? 類的屬性必須是完全的合格的類路徑名,名稱屬性對服務來說必須是唯一的名稱。
2.6調用流程(Invoking the Flow)
???????? 當前流程是有效的,但是沒有入口點,通過URL執行它,并不能得到反饋信息。首先必須為service配置一個端點endpoint, 以使它能被調用。當運行實例時,通過http發送一個request去調用EchoFlow,一個相應通過Http channel被返回。Mule Http Transport管理這個交互。
???????? 去配置這個流程,我們已經創建一個接入點<inbound-endpoint>,來實現請求相應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>
???????? 如果現在去觸發流程,將會得到一個響應,它將返回一個URL PATH 給你。然而EchoFlow仍不知道怎么去處理Web Service的調用,首先你需要去采用一個使用CXF的過濾器。
2.7暴露CXF類型Web Service (Exposing as a Web Service with CXF)
???????? CXF已經內置了對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>
現在如果去觸發URL,將會得到適當的響應消息。也可以去請求WSDL,它是組件運行需要的全部的配置信息。去看整體內的配置文件,打開目錄” examples/echo/src/main/app/ directory under your Mule installation directory”下的mule-config.xml文件.
2.8添加Stdio端點(Adding a STDIO endpoint)
在MULE 之前的版本中,echo實例支持在命令行輸入信息時的提示,當你輸入一些信息時,你正通過System.in 提供輸入數據調用服務,在那時你輸入的數據經由Systme.out被不停的回應回來。MULE STDIO Transport管理這個交互。
去配置STDIO 支持該流程,你需要在你的配置文件中增加STDIO的命名空間,然后使用”one-way”交換模式詳細制定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路由元素被配置去控制如何、哪里的消息被發送,一旦他們被組件處理時。在這個例子中,所有的消息被經由System.out發送。
最后,如果你想去覆默認被傳輸具體指定在inbound endpoint上配置,你可以去配置一個連接器。在這個例子中,讓我們覆蓋stdio 傳輸器默認的鏈接器,于是提醒用戶輸入下面的內容:
<stdio:connector name="SystemStreamConnector"
???????????????? promptMessage="Please enter something: "
???????????????? messageDelayTime="1000"/>
2.9增加多個接入點
有幾個方法對流程增加多個接入點,如果他們是跟隨統一個處理器鏈,你可以使用<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>
然而,如果你有明確的端點消息處理器去應用,像CXF,一個選擇是去使用復合流程,在復合流程中,你引用的一個流程是來自另外的一個流程中。這個方法,你可以中止流程通過端點。看下面的例子:
??? <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 提供兩種不同的接入點。他們都死通應用執行的EchoComponent。
?
2.10使用一個服務來配置(Configuring using a Service)
???????? 作為選擇,流程能夠使用一個服務來配置,去配置這個服務,你增加一個<service>元素到你的MULE XML 配置文件中,并提供名字屬性。通過使用<component>元素具體制定服務組件的類。然后和之前的一樣增加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>
在服務配置中,對CXF來說ServiceClass是不需要的。這是因為在這個配置中,CXF是能夠自動發現組件的。基于流的配置目前還不支持,所以serviceClass必須被明確的制定。
三、HelloWorld 例子分析
3.1 概述
???????? 該部分主要描述Hello World例子的配置和行為。
???????? 當我們在瀏覽器中輸入預定義的地址 (http://localhost:8888/?name=Ross) 時 Mule解釋輸入值中“?name=”后的部分,進行驗證,使greeeter.java 類去添加一些文本到字符串中,然后傳遞到另外一個flow,第二個flow繼續添加文本,然后通過瀏覽器傳回來;以上做的這些知識簡單的方式,但是這個例子使用了flows和http傳輸去說明消息路由和轉換器在Mule中的引用。
?
3.2運行應用
???????? 如果正在使用MULE IDE 環境,可以創建一個基于Hello World例子的工程,然后通過Eclipse來運行;
如果沒有使用MULE IDE環境,簡單的拷貝預構建的檔案資料(mule-example-hello-3.0.0.zip)到應用文件夾($MULE_HOME/apps),并啟動mule。去通過瀏覽器訪問web service 調用:
http://localhost:8888/?name=Ross
??????? 通過在控制臺按“CTR-C”停止mule;
3.3編譯例子
同上
3.4如何工作的?
???????? Hello World應用被配置在mule-config.xml中,這個文件存在于mule 根目錄下的examples/hello/src/main/app. 該部分主要是通過配置和JAVA資源文件的調用實現的。
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文件夾, 以下是資源文件的內容:
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”,字符串內容來自屬性文件。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 , 這個類實現附加字符串”,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中,調用這些類的配置文件,在composite元素中組成了3個接入點,去接受HTTP、Servlet、和VM請求;
???????? Hollo World flow 使用這個composite元素去獲得在瀏覽器中輸入的名字,調用greeter類,路由意外的錯誤去分離被調用的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屬性指定接入轉換器被調用。該轉換器早先在配置文件被定義:
<custom-transformer name="HttpRequestToNameString" class="org.mule.example.hello.HttpRequestToNameString"/>
?
該轉換器轉換從Http Connector 收到的Http Request 為NameString 對象,該對象是類Greeter的Greet()方法預期的數據類型。當你指定逐漸的時候,可以不必指出一個具體類中的一個具體的方法—Mule 能決定基于消息數據類型的適合的方法。
Greeter類預決定對輸入的用戶“問候”后,消息被匹配在端點vm://chitchatter. 這是調ChitChat flow, 同時調用定制的轉換器NameStringToChatString,轉換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>
?
去更多的演示轉換器,ChitChatter類期待一個ChatString對象,于是我們有了一個NameStringToChatString轉換器,在組件接收到消息之前,去轉換消息有效部分從NameString 為ChatString。消息被接收在vm://chitchatter上,這個端點是關于Greeter類分發它的消息。
消息被處理后,該消息被作為Http響應信息被發送,這個組件上的響應轉換會記錄。甚至認為沒有接出提供者,再有一個轉換器ChatStringToString,設置 Even though there is no outbound provider, there is one more transformer, ChatStringToString,它轉換有效負載從ChatString 為 平常的string ,所以他能被Http 傳輸處理,在http響應中被顯示。
?
注釋:JAVA類沒有任何的路由邏輯,他們是通過Mule的配置文件連接在一起的,能實現在JAVA類,WebService等之間傳遞消息
3.8 配置Servlet 傳輸
在Web容器中部署MULE是很常見的,使用Web Server 代替Mule Http Transport去管理Http connection。通過Servlet的方式調用Hello 服務,提交一個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 程序中如何連接服務、如何使用mule Client
<vm:inbound-endpoint path="greeter" transformer-refs="StringToNameString" exchange-pattern="request-response"/>
然后用Mule Client API 去調用服務:
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());
?
轉載于:https://www.cnblogs.com/GeneralXU/archive/2011/03/29/1998437.html
總結
以上是生活随笔為你收集整理的Mule 官方例子研究的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java为窗体添加滚动条
- 下一篇: Android 体系结构和应用程序组成