2013年8月5日 星期一

JBoss ESB Sample - Math Service by Mutil Service

因應需求會有多個 Service 運作後再傳送至 JMS-Topic 再由多個接收端(Application System)接收訊息,因此規劃如下圖的架構,當 JMS Client 傳入二個數字後,會對這二個數字做 +、X 法的運算,只要有 Application System 須要知道運算結果即可接收 Result-Topic 的訊息來獲得運算式的結果。
  • Math Service:這一層為 Mutil Service ,因應需求可再擴充 Service ,此 Sample 只定義二個 Service 來示範;這裡是採用JBoss SOA Web Service - Math Sample 裡的+、-、X、/ 運算。
  • Input-Topic:配合 Mutil Service 因此採用Topic ,相較於採用 Queue 因考慮到 Mutil Service 擴充時可避免還要新增 Input 這一層。
  • Notify Service:ESB Service 的輸出,這個 Sample 共有三種輸出,Log 類型 Console-Log、File-Log,JMS 類型 Result-Topic 也是主要的輸出方式配合會有多個系統獲取運算結果的需求。

準備執行環境

預設JBoss SOA/ESB 5.0 及 MySQL Server 5.1 已配置這裡不再贅述,如要更換 SOA/ESB 配置的 Database 可參考另一篇:JBoss SOA/ESB DataSource Configuration using MySql
  • JBoss SOA Server 5.0.2.GA+
  • JBoss ESB Server 4.10+
  • MySQL Server 5.1
  • Apache Ant 1.8.2+

%JBOSS_HOME%設定為 JBoss SOA 的路徑jboss-soa-5.0\jboss-as,須跟JBoss EAP做區別。
這個 Mutil Service Sample 會參考 JBoss SOA - Quickstarts 的相關案例,關於 Quickstarts 的佈署可參考 JBoss ESB Sample quickstarts - HelloWorldQuickstarts相關的案例說明如下,需要進一步了解可先參考。
  • jms_topic:主要架構,以 Topic 為對外接口,並有 Mutil Service 接收 Topic 訊息。
  • helloworld_topic_notifier:以 Topic 為輸出。
  • webservice_consumer1:Action 如何呼叫 Web Service。


Math Service by Mutil Service Create & Deployment

  1. 複製資料夾 %JBOSS_HOME%\samples\quickstarts\jms_topic 並將其更名為 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify ,接下來的說明都是針對 esb_math_service_mutil_notify 進行;首先 jboss-esb.xml 修改成如下所示,接著會以 ESB 內部可以做為獨立 Service 為單位進行說明。
    • MathServiceCategory:ServiceMathAdd
    • mathservice-topic-input - Gateway Listener 負責接收對外 Message。(line-32)
    • mathservice-queue-add - 傳遞ESB內部訊息的Listener,負責監聽 mathservice-topic-input 訊息並將訊息傳送至 Action 也就是request-mapper
    • request-mapper - 接收 Listener 的訊息並輸入做為 Add Web Service 的參數,由 AddServiceRequestAction 處理這些動作,後續會說明實作的部份。(line-36、37)
    • soapui-client-action - 呼叫 Add Web Service,並將執行結果傳送至 response-mapper ; property-SOAPAction 則需設定要執行的 Web Service method ,以 Add Web Service 來說只有一個 method - add。(line-39~45)
    • response-mapper - 接收 SOAP Client 的執行結果,再將結果傳遞至 NotifyService ,由 AddServiceResponseAction 處理這些動作,後續會說明實作的部份 ; property-target 是設定給 AddServiceResponseAction 使用的參數,以 ServiceCategory + ServiceName 來識別 Service ,而呼叫 Service 則是 AddServiceResponseAction 須實作的部份。(line-46~49)
    • MathServiceCategory:ServiceMathMultiply - 與 ServiceMathAdd 流程一致因此只列出須對照或說明的部份
    • mathservice-topic-input - 因使用 JMS-Topic 做為 Gateway Listener 因此可提供多個 Service 使用。(line-56)
    • request-mapper - 有設定參數 process - multiplyProcess,表示執行的是 MultiplyServiceRequestAction.java 的 method- multiplyProcess ,如省略沒有設定則預設執行的method為process 。(line-60、61)
    • soapui-client-action - property-SOAPAction 改為設定 Multiply Web Service method - multiply。(line-68)
    • response-mapper - property-target 的設定可重覆呼叫 NotifyService。(line-72)
    • MathServiceCategory:NotifyService
    • mathservice-nofify-gtw - 只要有呼叫 NotifyService 的訊息都會傳送到這個 Gateway Listener 。
    • mathservice-nofify-srv - 傳遞ESB內部訊息的Listener,負責監聽 mathservice-nofify-gtw 訊息並將訊息傳送至 Action 也就是notificationTopicAction
    • notificationTopicAction - 接收 Listener 的訊息並將訊息通知到目的地,這裡的目的地如設定所列共有三種型態:Console、File、JMS-Topic
    • providers - 上述三個 Service 中的 Listener 都為 JMS 型態,因此也都須配置對應的 jms-provider
    • mathservice-topic-input - 做為 ESB Service 對外的接口接收來自 JMS Client Request。
    • 其餘4個皆為 ESB Service 內部訊息傳遞。(line-12~24)
    <?xml version = "1.0" encoding = "UTF-8"?>
    <jbossesb
            xmlns="http://anonsvn.labs.jboss.com/labs/jbossesb/trunk/product/etc/schemas/xml/jbossesb-1.0.1.xsd"
            parameterReloadSecs="5">
    
        <providers>
            <jms-provider name="JMS" connection-factory="ConnectionFactory">
                <jms-bus busid="mathservice-topic-input">
                    <jms-message-filter dest-type="TOPIC" dest-name="topic/math_service_topic_input_num"/>
                </jms-bus>
    
                <jms-bus busid="mathservice-queue-add">
                    <jms-message-filter dest-type="QUEUE" dest-name="queue/math_service_queue_add"/>
                </jms-bus>
                <jms-bus busid="mathservice-queue-multiply">
                    <jms-message-filter dest-type="QUEUE" dest-name="queue/math_service_queue_multiply"/>
                </jms-bus>
    
                <jms-bus busid="mathservice-nofify-gtw">
                    <jms-message-filter dest-type="QUEUE" dest-name="queue/math_service_queue_nofify_gtw"/>
                </jms-bus>
                <jms-bus busid="mathservice-nofify-srv">
                    <jms-message-filter dest-type="QUEUE" dest-name="queue/math_service_queue_nofify_srv"/>
                </jms-bus>
            </jms-provider>
        </providers>
    
        <services>
            <!-- Math - Add Service -->
            <service category="MathServiceCategory" name="ServiceMathAdd" description="Topic receiver call add service to Result Topic">
                <listeners>
                    <jms-listener name="JMS-Gateway" busidref="mathservice-topic-input" is-gateway="true"/>
                    <jms-listener name="JMS-ESBListener" busidref="mathservice-queue-add"/>
                </listeners>
                <actions mep="OneWay">
                    <action name="request-mapper"
                        class="org.jboss.soa.esb.samples.mathservice.AddServiceRequestAction">
                    </action>
                    <action name="soapui-client-action"
                        class="org.jboss.soa.esb.actions.soap.SOAPClient">
                        <!-- call AddService -->
                        <property name="wsdl" value="http://127.0.0.1:8080/mathWS/AddService?wsdl" />
                        <property name="responseAsOgnlMap" value="true" />
                        <property name="SOAPAction" value="add"/> 
                    </action>
                    <action name="response-mapper"
                        class="org.jboss.soa.esb.samples.mathservice.AddServiceResponseAction">
                        <property name="target" value="MathServiceCategory:NotifyService"/>
                    </action>
                </actions>
            </service>
    
            <!-- Math - Multiply Service -->
            <service category="MathServiceCategory" name="ServiceMathMultiply" description="Topic receiver call multiply service to Result Topic">
                <listeners>
                    <jms-listener name="JMS-Gateway" busidref="mathservice-topic-input" is-gateway="true"/>
                    <jms-listener name="JMS-ESBListener" busidref="mathservice-queue-multiply"/>
                </listeners>
                <actions mep="OneWay">
                    <action name="request-mapper"
                        class="org.jboss.soa.esb.samples.mathservice.MultiplyServiceRequestAction" process="multiplyProcess">
                    </action>
                    <action name="soapui-client-action"
                        class="org.jboss.soa.esb.actions.soap.SOAPClient">
                        <!-- call MultiplyService --> <!---->
                        <property name="wsdl" value="http://127.0.0.1:8080/mathWS/MultiplyService?wsdl" />
                        <property name="responseAsOgnlMap" value="true" />
                        <property name="SOAPAction" value="multiply"/> 
                    </action>
                    <action name="response-mapper"
                        class="org.jboss.soa.esb.samples.mathservice.MultiplyServiceResponseAction" process="multiplyProcess">
                        <property name="target" value="MathServiceCategory:NotifyService"/>
                    </action>
                </actions>
            </service>
    
            <!-- Math Notification Service -->
            <service category="MathServiceCategory" name="NotifyService" description="mutil math service send to Notification Service">
                <listeners>
                    <jms-listener name="JMS-Gateway" busidref="mathservice-nofify-gtw" is-gateway="true"/>
                    <jms-listener name="JMS-ESBListener" busidref="mathservice-nofify-srv"/>
                </listeners>
                <actions mep="OneWay">
                    <action name="notificationTopicAction" class="org.jboss.soa.esb.actions.Notifier">
                        <property name="okMethod" value="notifyOK" />
                        <property name="notification-details"> 
                            <NotificationList type="ok">
                                <target class="NotifyConsole"/>
                                <target class="NotifyFiles">
                                    <file append="true" URI="file:///D:\jboss-soa-5.0\jboss-as\server\default\log\math_service_results.log"/>
                                </target>
                                <target class="NotifyTopics">
                                    <topic jndiName="topic/math_service_topic_result"/>
                                </target>    
                            </NotificationList>
                        </property>
                    </action>
                </actions>
            </service>
    
        </services>
    </jbossesb>
    

  2. 新增 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\src\org\jboss\soa\esb\samples\mathservice\AddServiceRequestAction.java file 內容如下。
    • line-36:傳入的 Message 是以逗號(,)區隔的二個數字,將其轉換為 Double ArrayList ,以利後續操作。
    • line-46~47:設定呼叫 Add Web Service 時需傳入的參數,可參考 Add Web Service wsdl file(如圖) 來設定 requestMap 的 key ,method - add,parameter - arg0、arg1requestMap 的 value 則帶入 Double ArrayList 。
    • line-51: 將 requestMap 新增至 Message Body 。
    • line-44: 將算式設定為 Message Property ,可傳遞至 ESB Service Output 做為算式說明,可以知道運算結果的來源;也透過這方式了解 Message 如何傳遞其他訊息。


    package org.jboss.soa.esb.samples.mathservice;
    
    import org.jboss.soa.esb.actions.AbstractActionLifecycle;
    import org.jboss.soa.esb.actions.ActionUtils;
    import org.jboss.soa.esb.helpers.ConfigTree;
    import org.jboss.soa.esb.message.Body;
    import org.jboss.soa.esb.message.Message;
    import java.util.HashMap;
    import java.util.ArrayList;
    
    public class AddServiceRequestAction extends AbstractActionLifecycle
    {
       protected ConfigTree _config;
    
       public AddServiceRequestAction(ConfigTree config)
       {
          _config = config;
       }
    
       public Message noOperation(Message message)
       {
          return message;
       }
    
       /*
        * Convert the message into a webservice request map.
        */
       public Message process(Message message) throws Exception
       {
          logHeader();
          String msgBody = (String) message.getBody().get();
          HashMap requestMap = new HashMap();
          System.out.println("msgBody is: " + msgBody);
    
          //msgBody to Double List
          String[] msgList = msgBody.split(",");
          ArrayList<double> aMsgListDoubles = new ArrayList<double>();
          for (int i=0; i < msgList.length; i++)
             aMsgListDoubles.add(Double.valueOf(msgList[i]));
          System.out.println("aMsgListDoubles(0) is: " + aMsgListDoubles.get(0));
          System.out.println("aMsgListDoubles(1) is: " + aMsgListDoubles.get(1));
    
          //Add Formula describe
          message.getProperties().setProperty("AddFormulaDesc", aMsgListDoubles.get(0) + " + " + aMsgListDoubles.get(1));
    
          requestMap.put("add.arg0", aMsgListDoubles.get(0));
          requestMap.put("add.arg1", aMsgListDoubles.get(1));
    
          // The "paramsLocation" property was set in jboss-esb.xml to
          // "helloworld-request-parameters"
          message.getBody().add(requestMap);
          System.out.println("Request map is: " + requestMap.toString());
    
          logFooter();
          return message;
       }
    
       public void exceptionHandler(Message message, Throwable exception)
       {
          logHeader();
          System.out.println("!ERROR!");
          System.out.println(exception.getMessage());
          System.out.println("For Message: ");
          System.out.println(message.getBody().get());
          logFooter();
       }
    
       // This makes it easier to read on the console
       private void logHeader()
       {
          System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
       }
    
       private void logFooter()
       {
          System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
       }
    
    }
    

  3. 新增 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\src\org\jboss\soa\esb\samples\mathservice\AddServiceResponseAction.java file 內容如下。
    • line-36:對照 jboss-esb.xml line-48 ,這裡接收參數進來後透過 new Service(ServiceCategory, ServiceName) 的方式來取得與 Service 的連結。
    • line-41:call Target Service 也就是 NotifyService
    • line-65:取得 Add Web Service 回傳的結果。
    • line-69:取得 AddServiceRequestAction 設定的運算式(Property-AddFormulaDesc)。
    • line-72:新增執行時間的訊息。
    • line-78:將訊息組成 XML 格式。
    • line-83:將訊息新增至 Message Body。
    • line-86:呼叫 NotifyService 並將 Message 傳入。
    package org.jboss.soa.esb.samples.mathservice;
    
    import org.jboss.soa.esb.actions.AbstractActionLifecycle;
    import org.jboss.soa.esb.actions.ActionUtils;
    import org.jboss.soa.esb.helpers.ConfigTree;
    
    import org.jboss.soa.esb.actions.ActionProcessingException;
    import org.jboss.soa.esb.ConfigurationException;
    import org.jboss.soa.esb.Service;
    import org.jboss.soa.esb.listeners.message.MessageDeliverException;
    import org.jboss.soa.esb.client.ServiceInvoker;
    
    import org.jboss.soa.esb.message.Body;
    import org.jboss.soa.esb.message.Message;
    import java.util.Map;
    
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class AddServiceResponseAction extends AbstractActionLifecycle
    {
       protected ConfigTree _config;
       private String addFormulaDesc;
       private Service targetService;
    
       public AddServiceResponseAction(ConfigTree config) throws ConfigurationException
       {
          //_config = config;
          String target = config.getRequiredAttribute("target");
          String[] targetTokens = target.split(":");
    
          if(targetTokens.length != 2) {
             throw new ConfigurationException("Action not configured properly - 'target' service property must be in format 'category:name'.");
          }
    
          targetService = new Service(targetTokens[0], targetTokens[1]);
       }
    
        private ServiceInvoker getInvoker() throws ActionProcessingException {
            try {
                return new ServiceInvoker(targetService);
            } catch (MessageDeliverException e) {
                throw new ActionProcessingException("Failed to create ServiceInvoker: " + e.getMessage());
            }
        }
    
       public Message noOperation(Message message)
       {
          return message;
       }
    
       /*
        * Retrieve and output the webservice response.
        */
       public Message process(Message message) throws ActionProcessingException
       {
    
          logHeader();
    
          // The "responseLocation" property was set in jboss-esb.xml to
          // "helloworld-response"
          Map responseMsg = (Map) message.getBody().get(Body.DEFAULT_LOCATION);
          System.out.println("Response Map is: " + responseMsg);
    
          String addResultMsg = String.valueOf(responseMsg);
          addResultMsg = addResultMsg.substring(addResultMsg.indexOf("=")+1, addResultMsg.length()-1);
          System.out.println("AddResultMsg is: " + addResultMsg);
    
          addFormulaDesc = (String) message.getProperties().getProperty("AddFormulaDesc");
          System.out.println("AddFormulaDesc is: " + addFormulaDesc);
    
          SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date now = new Date();
          String strDate = sdfDate.format(now);
    
    
          String msgBody;
          msgBody = "" + 
                       "" + addResultMsg + "" + 
                       "" + addFormulaDesc + "" + 
                       "" + strDate + "" + 
                    "";
          message.getBody().add(msgBody);
    
          try {
             getInvoker().deliverAsync(message);
          } catch (MessageDeliverException e) {
             throw new ActionProcessingException("Failed to deliver message: " + e.getMessage());
          }
    
          logFooter();
          return message;
       }
    
       public void exceptionHandler(Message message, Throwable exception)
       {
          logHeader();
          System.out.println("!ERROR!");
          System.out.println(exception.getMessage());
          System.out.println("For Message: ");
          System.out.println(message.getBody().get());
          logFooter();
       }
    
       // This makes it easier to read on the console
       private void logHeader()
       {
          System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
       }
    
       private void logFooter()
       {
          System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
       }
    
    }
    

  4. 新增 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\src\org\jboss\soa\esb\samples\mathservice\MultiplyServiceRequestAction.java file 因執行的內容類似 AddServiceRequestAction.java,所以僅節錄內容如下;對照 jboss-esb.xml line-61 實際呼叫的method為multiplyProcessline-24 :設定 Property - MultiplyFormulaDescline-27~28 : Multiply Web Service method 為 multiply

  5. package org.jboss.soa.esb.samples.mathservice;
    
    ....
    
    public class MultiplyServiceRequestAction extends AbstractActionLifecycle
    {
    ....
       /*
        * Multiply Service - Convert the message into a webservice request map.
        */
       public Message multiplyProcess(Message message) throws Exception
       {
          logHeader();
          String msgBody = (String) message.getBody().get();
          HashMap requestMap = new HashMap();
    
          //msgBody to Double List
          String[] msgList = msgBody.split(",");
          ArrayList<Double> aMsgListDoubles = new ArrayList<Double>();
          for (int i=0; i < msgList.length; i++)
             aMsgListDoubles.add(Double.valueOf(msgList[i]));
    
          //Multiply Formula describe
          message.getProperties().setProperty("MultiplyFormulaDesc", aMsgListDoubles.get(0) + " x " + aMsgListDoubles.get(1));
    
          // add paramaters to the web service request map
          requestMap.put("multiply.arg0", aMsgListDoubles.get(0));
          requestMap.put("multiply.arg1", aMsgListDoubles.get(1));
    
          // The "paramsLocation" property was set in jboss-esb.xml to
          // "helloworld-request-parameters"
          message.getBody().add(requestMap);
          System.out.println("Request map is(Multiply): " + requestMap.toString());
    
          logFooter();
          return message;
       }
    
    ....
    
    }
    

  6. 新增 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\src\org\jboss\soa\esb\samples\mathservice\MultiplyServiceResponseAction.java file 因執行的內容類似 AddServiceResponseAction.java,所以僅節錄內容如下。

  7. package org.jboss.soa.esb.samples.mathservice;
    
    ....
    
    public class MultiplyServiceResponseAction extends AbstractActionLifecycle
    {
    ....
    
       /*
        * Multiply Service - Retrieve and output the webservice response.
        */
       public Message multiplyProcess(Message message) throws ActionProcessingException
       {
    
          logHeader();
    
          // The "responseLocation" property was set in jboss-esb.xml to
          // "helloworld-response"
          Map responseMsg = (Map) message.getBody().get(Body.DEFAULT_LOCATION);
          System.out.println("Response Map is(Multiply): " + responseMsg);
    
          String multiplyResultMsg = String.valueOf(responseMsg);
          multiplyResultMsg = multiplyResultMsg.substring(multiplyResultMsg.indexOf("=")+1, multiplyResultMsg.length()-1);
          System.out.println("MultiplyResultMsg is: " + multiplyResultMsg);
    
          multiplyFormulaDesc = (String) message.getProperties().getProperty("MultiplyFormulaDesc");
          System.out.println("MultiplyFormulaDesc is: " + multiplyFormulaDesc);
    
          SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date now = new Date();
          String strDate = sdfDate.format(now);
    
          String msgBody;
          msgBody = "" + 
                      "" + multiplyResultMsg + "" + 
                      "" + multiplyFormulaDesc + "" + 
                      "" + strDate + "" + 
                    "";
          message.getBody().add(msgBody);
    
          try {
             getInvoker().deliverAsync(message);
          } catch (MessageDeliverException e) {
             throw new ActionProcessingException("Failed to deliver message: " + e.getMessage());
          }
    
          logFooter();
          return message;
       }
    
    ....
    
    }
    

  8. 修改 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\jbm-queue-service.xml,所有 jboss-esb.xml 有使用到的 JMS 都要設定,包含 math_service_topic_result
  9. <?xml version="1.0" encoding="UTF-8"?>
    <server>
    
        <mbean code="org.jboss.jms.server.destination.TopicService"
               name="jboss.messaging.destination:service=Topic,name=math_service_topic_input_num"
               xmbean-dd="xmdesc/Topic-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
            <attribute name="SecurityConfig">
               <security>
                  <role name="guest" read="true" write="true" create="true"/>
               </security>
            </attribute>
        </mbean>
    
        <mbean code="org.jboss.jms.server.destination.QueueService"
               name="jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_add"
               xmbean-dd="xmdesc/Queue-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
        </mbean>
        <mbean code="org.jboss.jms.server.destination.QueueService"
               name="jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_multiply"
               xmbean-dd="xmdesc/Queue-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
        </mbean>
    
        <mbean code="org.jboss.jms.server.destination.QueueService"
               name="jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_nofify_gtw"
               xmbean-dd="xmdesc/Queue-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
        </mbean>
        <mbean code="org.jboss.jms.server.destination.QueueService"
               name="jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_nofify_srv"
               xmbean-dd="xmdesc/Queue-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
        </mbean>
    
        <mbean code="org.jboss.jms.server.destination.TopicService"
               name="jboss.messaging.destination:service=Topic,name=math_service_topic_result"
               xmbean-dd="xmdesc/Topic-xmbean.xml">
            <depends optional-attribute-name="ServerPeer">jboss.messaging:service=ServerPeer</depends>
            <depends>jboss.messaging:service=PostOffice</depends>
            <attribute name="SecurityConfig">
               <security>
                  <role name="guest" read="true" write="true" create="true"/>
               </security>
            </attribute>
        </mbean>
    
    </server>
    

  10. deployment.xml,修改 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\deployment.xml,所有 jboss-esb.xml 有使用到的 JMS 都要設定,包含 math_service_topic_result ,另外還須設定 SoapUIClientService
  11. <jbossesb-deployment>
        <depends>jboss.esb:deployment=soap.esb</depends>
        <depends>jboss.esb:service=SoapUIClientService</depends>
        <depends>jboss.messaging.destination:service=Topic,name=math_service_topic_input_num</depends>
        <depends>jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_add</depends>
        <depends>jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_multiply</depends>
        <depends>jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_nofify_gtw</depends>
        <depends>jboss.esb.mathservice.destination:service=Queue,name=math_service_queue_nofify_srv</depends>
        <depends>jboss.messaging.destination:service=Topic,name=math_service_topic_result</depends>
    </jbossesb-deployment>
    

  12. build.xml,修改 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\build.xml ,主要修改 project name:esb_math_service_mutil_notify, runtest 執行的程式改為 SendMathParaMessage 並設定參數為二個數字。
  13. <project name="esb_math_service_mutil_notify" default="deploy" basedir=".">
    
        <description>
            ${ant.project.name}
            ${line.separator}
        </description>
    
        <!-- Import the base Ant build script... -->
        <import file="../conf/base-build.xml"/>
    
        <target name="runtest" depends="compile" description="Send a message to the JMS Topic(input unknow number - a,b)">
            <java fork="yes" classname="org.jboss.soa.esb.samples.mathservice.SendMathParaMessage" failonerror="true">
                <arg value="3.4,8.7"/>
                <classpath refid="exec-classpath"/>
            </java>
        </target>
    
    </project>
    

  14. 於 DOS Command 輸入 ant deploy,執行成功則進入下一階段的測試。

Math Service by Mutil Service Test

  1. 新增 %JBOSS_HOME%\samples\quickstarts\esb_math_service_mutil_notify\src\org\jboss\soa\esb\samples\mathservice\SendMathParaMessage.java file,內容如下,Topic Name: /topic/math_service_topic_input_num
  2. package org.jboss.soa.esb.samples.mathservice;
    
    import java.util.Properties;
    
    import javax.jms.JMSException;
    import javax.jms.ObjectMessage;
    
    import javax.jms.Topic;
    import javax.jms.TopicConnection;
    import javax.jms.TopicConnectionFactory;
    import javax.jms.TopicPublisher;
    import javax.jms.TopicSubscriber;
    import javax.jms.TopicSession;
    
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    public class SendMathParaMessage {
        TopicConnectionFactory tcf;
        TopicSession session;
        Topic topic;
        TopicConnection tc;
    
        public void setupConnection() throws JMSException, NamingException
        {
            Properties properties1 = new Properties();
         properties1.put(Context.INITIAL_CONTEXT_FACTORY,
                         "org.jnp.interfaces.NamingContextFactory");
         properties1.put(Context.URL_PKG_PREFIXES,
                         "org.jboss.naming:org.jnp.interfaces");
         properties1.put(Context.PROVIDER_URL, "jnp://127.0.0.1:1099");
         InitialContext iniCtx = new InitialContext(properties1);
    
         tcf = (TopicConnectionFactory) iniCtx.lookup("ConnectionFactory");
         topic = (Topic) iniCtx.lookup("/topic/math_service_topic_input_num");
        }
    
        public void stop() throws JMSException 
        { 
         tc.stop();
         session.close();
         tc.close();
        }
    
        public void sendAMessage(String msg) throws JMSException {
         tc = tcf.createTopicConnection("guest", "guest");
         tc.setClientID("clientMathService");
         session = tc.createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
         TopicPublisher tp = session.createPublisher(topic); 
            ObjectMessage tm = session.createObjectMessage(msg);
         tp.publish(tm);
        }
    
        public static void main(String args[]) throws Exception
        {              
         SendMathParaMessage sm = new SendMathParaMessage();
         sm.setupConnection();
         sm.sendAMessage(args[0]); 
         sm.stop();
        }
    
    }
    

  3. Start MySQL/JBoss SOA ,於 DOS Command 輸入 ant deploy,於 DOS Command 輸入 ant runtest,執行畫面如下,另外也可看到 math_service_results.log 的輸出。

  4. 接著測試 Receive Topic: topic/math_service_topic_result ,測試畫面如下。


相關設定可參考:
JBoss ESB學習筆記10——第九個ESB應用JMS Topic
JBoss ESB學習筆記4——第三個ESB應用Hello World Notification
JBoss ESB學習筆記16——第十五個ESB應用Web Service Consumer 1

沒有留言:

張貼留言