jboss esb基於Drools的Content Based Routing

找了好久才找到jboss soa 基於內容的路由,拷貝過來和大家一起分享一下。

基於Drools的Content Based Routing

1.
JBossESB中,基於內容的路由器(CBR)使用JBoss Rules作爲它的規則評估引擎。JBossESB通過以下三步整合Drools

制定一個路由ruleSet,使用JBoss Rules DRL實現的


規則引擎使用的數據是ESB消息內容。ESB的消息內容可以是序列化的xml或者對象。


規則引擎處理後的結果作爲目標服務。

當一個消息發送到CBR,一個特定的ruleSet就會對消息內容進行評估然後返回一系列服務目標。下面將會討論規則集會怎麼被選定,消息內容怎麼評估以及怎麼處理目標結果。
2.

 

JBossESB包裝了三種略有不同的路由動作類。每個動作類都實現了一種企業整合模式。JbossESB Wiki中包含了關於企業整合模式的更多詳細信息。

 

 

   org.jboss.soa.esb.actions.ContentBasedRouter

 

 

    基於內容路由方式的實現。它是根據消息內容以及對應的ruleSet將消息路由到一個或者多個目標服務。CBR在目標都不符合給定規則集的評估結果時會拋出異常。這樣會終止任何管道處理,因此他應當是管道中最後一個動作。

 

 

org.jboss.soa.esb.actions.ContentBasedWireTap

 

 

         WireTap方式的實現。WireTap是一種企業整合方式,在這種方式中消息的副本會被髮送到一個控制通道。CBR-WT和基於內容路由在功能上是相同的,但是它不會終止管道,這將使它十分適合做一種監聽。

 

 

   org.jboss.soa.esb.actions.MessageFilter

 

 

    消息過濾的實現方式。這種方式適合不符合內容要求的規則可以直接扔掉的場景。CBR-MF和基於內容路由在功能上是相同的,當這種方式不會在目標都不符合給定規則集的評估結果時拋出異常。所以,這只是對消息進行了簡單的過濾。

 

 

 

 

 

 

 

 

下面實現一個ContentBasedRouter的例子

 

 

 

 

 

 

 

MyJMSListenerAction.java                ------------------action class

 

 

RouteExpressShipping.java               ------------------action class
RouteNormalShipping.java                ------------------action class

SendJMSMessage.java                                                    -------------------------------測試類,發送不同的消息

SimpleCBRRules-XPath.drl                                             -------------------------------規則集
jbm-queue-service.xml                                                    -------------------------------這裏是定義啓動所需要的Queue
jndi.properties                                                                   -------------------------------jndi配置文件
jboss-esb.xml                                                                      -------------------------------esb配置文件
deployment.xml                                                                -------------------------------在這裏定義對其他包或者服務的依賴,或者配置classloader

 

 

SampleOrder.xml                                                              -------------------------------一個簡單的xml文件,包含訂單信息

 


 

介紹一下整個流程:測試類讀取xml中的文本信息,將其發送到服務(category="MyFirstCBRServicesESB" name="FirstCBRServiceESB" ),該服務使用了基於內容的路由action,這個action指定了規則集(ruleset)SimpleCBRRules-XPath.drl  ,規則集會對xml中包含的訂單總額信息進行是否大於50的判斷,從而動態路由到目標服務。目標服務各自使用RouteNormalShipping.java和RouteExpressShipping.java兩個自定義動作類輸出相關信息

 

 


 

 


 

 

按照上面的步驟,我們先看規則集。這個規則集處理的信息主要是xml,所有使用了xpath與描述語言

 

 


 

 

SimpleCBRRules-XPath.drl   

 

 

  1.  

  2.  

  3. #created on: Nov 8, 2006

  4. package com.jboss.soa.esb.routing.cbr

  5. #list any import classes here.

  6. #需要因對類Message和MessageType

  7. import org.jboss.soa.esb.message.Message;

  8. import org.jboss.soa.esb.message.format.MessageType;

  9. #指定xpath

  10. expander XPathLanguage.dsl

  11. #declare any global variables here.

  12. #這裏是必須要定義的,返回的就是目標服務列表。

  13. global java.util.List destinations;

  14.  

  15. rule "Routing Rule using XPATH"

  16.  

  17.  when

  18.   #查看xml信息中的totalAmount是不是大於50

  19.   xpathGreaterThan "/Order/@totalAmount", "50.0"

  20.  then 

  21.  Log : "EXPRESS";

  22.   Log : "EXPRESS";

  23.   Log : "Really It's EXPRESS Shipping";

  24.   Destination : "express";

  25.  

  26. end

  27. rule "Routing Rule using XPATH less"

  28. when

  29. xpathLessThan "/Order/@totalAmount", "50.0"

  30. then

  31.  Log : "NORMAL";

  32.   Log : "NORMAL";

  33.   Log : "Really It's NORMAL Shipping";

  34.   Destination : "normal";

  35. end

複製代碼

 

 



 

下面看以下jboss-esb.xml中關於service部分的配置

 


 

 

  1.  

  2. <services>

  3.  

  4. <!-- ESB CBR Service -->

  5. <service 

  6. category="MyFirstCBRServicesESB" 

  7. name="FirstCBRServiceESB" 

  8. description="ESB Listener" >

  9. <listeners>

  10. <!-- Gateway -->

  11. <jms-listener name="the-gateway"

  12. busidref="quickstartGwChannel"

  13. is-gateway="true"

  14. /> 

  15. <jms-listener name="XPathContentBasedRouter"

  16. busidref="quickstartEsbChannel"> 

  17. </jms-listener>

  18. </listeners>

  19. <actions mep="OneWay">

  20.  

  21. <action class="org.jboss.soa.esb.actions.ContentBasedRouter" name="ContentBasedRouter">

  22. <!--這一部分就是設定ruleset-->

  23. <property name="ruleSet" value="SimpleCBRRules-XPath.drl"/>

  24. <property name="ruleLanguage" value="XPathLanguage.dsl"/>

  25. <property name="ruleReload" value="true"/>

  26. <!--這一部分就是設定返回的目標,根據xml我們知道規則會返回“express”,所以就會路由到服務ExpressShippingService-->

  27. <property name="destinations">

  28. <route-to destination-name="express" service-category="ExpressShipping" service-name="ExpressShippingService"/>

  29. <route-to destination-name="normal" service-category="NormalShipping" service-name="NormalShippingService"/>

  30. </property> 

  31. </action>

  32. </actions>

  33. </service>

  34.  

  35. <!-- Normal Shipping -->

  36. <service

  37. category="NormalShipping"

  38. name="NormalShippingService"

  39. description="Normal Shipping Service">

  40. <listeners>

  41. <jms-listener

  42. name="CBRNormalShipping"

  43. busidref="CBRNormalShipping"/>

  44. </listeners>

  45. <actions mep="OneWay">

  46. <action name="testStore" class="org.jboss.soa.esb.actions.TestMessageStore"/>

  47. <action name="displayMessageAction" 

  48. class="org.jboss.soa.esb.samples.quickstart.simplecbr.MyJMSListenerAction" 

  49. process="displayMessage" 

  50. /> 

  51. <!-- This can be replaced with notification -->

  52. <action name="route"

  53. class="org.jboss.soa.esb.samples.quickstart.simplecbr.RouteNormalShipping"

  54. process="sendResponse"

  55. /> 

  56. </actions>

  57. </service>

  58.  

  59. <!-- Express Shipping -->

  60. <service

  61. category="ExpressShipping"

  62. name="ExpressShippingService"

  63. description="Express Shipping Service">

  64. <listeners>

  65. <jms-listener

  66. name="CBRExpressFreeShipping"

  67. busidref="CBRExpressFreeShipping"/>

  68. </listeners>

  69. <actions mep="OneWay">

  70. <action name="testStore" class="org.jboss.soa.esb.actions.TestMessageStore"/>

  71. <action name="displayMessageAction" 

  72. class="org.jboss.soa.esb.samples.quickstart.simplecbr.MyJMSListenerAction" 

  73. process="displayMessage" 

  74. /> 

  75. <!-- This can be replaced with notification -->

  76. <action name="route"

  77. class="org.jboss.soa.esb.samples.quickstart.simplecbr.RouteExpressShipping"

  78. process="sendResponse"

  79. /> 

  80. </actions>

  81. </service>

  82. </services>

複製代碼

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章