Mule自带例子之stockquote
阅读原文时间:2023年07月15日阅读:1

1 配置效果图

2 配置文件

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd http://www.mulesoft.org/schema/mule/cxf ¨C12C ¨C13C http://www.springframework.org/schema/context/spring-context-current.xsd">

 <context:property-placeholder location="stockquote.properties" system-properties-mode="OVERRIDE" />

 <object-to-string-transformer name="ObjectToString" doc:name="Object to String" />  
 <custom-transformer name="XmlDecoder" class="org.mule.transformer.codec.XmlEntityDecoder" doc:name="Java"/>  
 <mulexml:xslt-transformer name="XsltRest" xsl-file="xsl/rest-stock.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>  
 <mulexml:xslt-transformer name="XsltSoap" xsl-file="xsl\\soap-stock.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>  
 <mulexml:xml-to-object-transformer name="XmlToObject" doc:name="XML to Object"/>

 <flow name="HTTP\_input">  
     <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="${port1}" path="stockquote"  
         responseTransformer-refs="ObjectToString" doc:name="HTTP"/>  
     <http:body-to-parameter-map-transformer doc:name="Body to Parameter Map"/>  
     <choice doc:name="Choice">  
         <when expression="payload.method == 'REST'" evaluator="groovy">  
             <logger message="Enter REST when clause." level="INFO" doc:name="logger in REST"/>  
             <processor-chain>  
                 <flow-ref name="REST" doc:name="REST"/>  
                 <logger message="After HTTP\_input REST: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class \[B - \[B@1280f39 -->  
             </processor-chain>  
             <logger message="After HTTP\_input REST processor-chain: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class \[B - \[B@1280f39 -->  
         </when>  
         <when expression="payload.method == 'SOAP'" evaluator="groovy">  
             <logger message="Enter SOAP when clause." level="INFO" doc:name="logger in SOAP"/>  
             <processor-chain>  
                 <flow-ref name="SOAP" doc:name="SOAP"/>  
             </processor-chain>  
         </when>  
         <when expression="payload.method == 'WSDL'" evaluator="groovy">  
             <logger message="Enter WSDL when clause." level="INFO" doc:name="logger in WSDL"/>  
             <processor-chain>  
                 <flow-ref name="WSDL" doc:name="WSDL"/>  
             </processor-chain>  
         </when>  
     </choice>

     <logger message="Before HTTP\_input XmlToObject transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class \[B - \[B@1280f39 -->  
     <transformer ref="XmlToObject" doc:name="Xml To Object"/>  
     <!--  
     <mulexml:xml-to-object-transformer name="XmlToObject" doc:name="XML to Object"/>功能:  
             把'class \[B'类型转换为StockQuote  
     class org.mule.example.stockquote.StockQuote - StockQuote\[symbol=CSCO, name=Cisco Systems, date=6/23/2015 9:56am, last=28.82, change=-0.12, open=29.00, high=29.04, low=28.79, volume=2410178, previousClose=28.94\]  
     -->  
     <logger message="After HTTP\_input XmlToObject transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  
 </flow>

 <!--  
     功能:调用REST服务,获取symbol=CSCO的信息  
     输入的类型:class java.util.HashMap - {symbol=CSCO, method=REST}  
     输出的类型:class \[B - \[B@1280f39  
  -->  
 <sub-flow name="REST">  
     <logger message="Before REST set-payload: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class java.util.HashMap - {symbol=CSCO, method=REST} -->  
     <set-payload value="Symbol=#\[payload\['symbol'\]\]" doc:name="Extract Symbol"/>  
     <logger message="After REST set-payload: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  <!-- class java.lang.String - Symbol=CSCO -->

     <set-property propertyName="Content-Type" value="application/x-www-form-urlencoded" doc:name="Set Content-Type property"/> 

     <!--  http:outbound-endpoint 向地址做一次http连接请求, 请求响应模型,等待响应 -->  
     <logger message="Before REST http:outbount-endpoint: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class java.lang.String - Symbol=CSCO -->  
     <http:outbound-endpoint exchange-pattern="request-response" address="http://www.webservicex.net/stockquote.asmx/GetQuote" doc:name="Invoke REST service"/>  
     <logger message="After REST http:outbount-endpoint: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  <!-- class org.mule.transport.http.ReleasingInputStream - org.mule.transport.http.ReleasingInputStream@1ef9b2e -->

     <!--  
     <custom-transformer name="XmlDecoder" class="org.mule.transformer.codec.XmlEntityDecoder" ... />转换器从ReleasingInputStream中解出XML内容:  
     <?xml version="1.0" encoding="utf-8"?>  
     <string xmlns="http://www.webserviceX.NET/">  
         <StockQuotes>  
             <Stock>  
                 <Symbol>CSCO</Symbol>  
                 <Last>28.885</Last>  
                 <Date>6/23/2015</Date>  
                 <Time>9:49am</Time>  
                 <Change>-0.055</Change>  
                 <Open>29.000</Open>  
                 <High>29.036</High>  
                 <Low>28.820</Low>  
                 <Volume>1836184</Volume>  
                 <MktCap>146.91B</MktCap>  
                 <PreviousClose>28.940</PreviousClose>  
                 <PercentageChange>-0.190%</PercentageChange>  
                 <AnnRange>22.490 - 30.310</AnnRange>  
                 <Earns>1.721</Earns>  
                 <P-E>16.784</P-E>  
                 <Name>Cisco Systems</Name>  
             </Stock>  
         </StockQuotes>  
     </string>  
      -->  
     <transformer ref="XmlDecoder" doc:name="Transformer Reference"/>  
     <logger message="After REST XmlDecoder transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> 

     <!-- class \[B - \[B@c3e965  
         <mulexml:xslt-transformer name="XsltRest" .../>转换器, 把<string>...</string>转换为一个内容大概如下的类型为'\[B'的对象  
         <org.mule.example.stockquote.StockQuote>  
             <Stock>  
                 <Symbol>CSCO</Symbol>  
                 <Last>28.885</Last>  
                 <Date>6/23/2015</Date>  
                 <Time>9:49am</Time>  
                 <Change>-0.055</Change>  
                 <Open>29.000</Open>  
                 <High>29.036</High>  
                 <Low>28.820</Low>  
                 <Volume>1836184</Volume>  
                 <MktCap>146.91B</MktCap>  
                 <PreviousClose>28.940</PreviousClose>  
                 <PercentageChange>-0.190%</PercentageChange>  
                 <AnnRange>22.490 - 30.310</AnnRange>  
                 <Earns>1.721</Earns>  
                 <P-E>16.784</P-E>  
                 <Name>Cisco Systems</Name>  
             </Stock>  
         </org.mule.example.stockquote.StockQuote>  
      -->  
     <transformer ref="XsltRest" doc:name="Transformer Reference"/>  
     <logger message="After REST XsltRest transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  
 </sub-flow>

  <sub-flow name="SOAP" doc:name="SOAP">

      <logger message="Before SOAP set-payload: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class java.util.HashMap - {symbol=CSCO, method=SOAP} -->  
     <set-payload value="#\[payload\['symbol'\]\]" doc:name="Extract Symbol"/>  
     <logger message="After SOAP set-payload: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> <!-- class java.lang.String - CSCO -->

     <processor-chain>  
         <cxf:jaxws-client operation="GetQuote" clientClass="net.webservicex.StockQuote" port="StockQuoteSoap" wsdlLocation="classpath:stockquote.wsdl" doc:name="SOAP client"/>  
         <http:outbound-endpoint address="http://www.webservicex.net/stockquote.asmx" doc:name="HTTP endpoint"/>  
         <!-- class org.mule.transport.http.ReleasingInputStream - org.mule.transport.http.ReleasingInputStream@1e41c46 -->  
         <logger message="After SOAP http:outbound-endpoint: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  
     </processor-chain>  
     <!--  
     此处表面 processor-chain 会把请求的响应ReleasingInputStream流类型 处理为对应的String格式  
     class java.lang.String - <StockQuotes><Stock><Symbol>CSCO</Symbol><Last>28.88</Last> ...................  
     -->  
     <logger message="After SOAP processor-chain: #\[payload.getClass()\] - #\[payload\]" level="INFO" /> 

     <transformer ref="XmlDecoder" doc:name="XmlDecoder transformer"/>  
     <logger message="After SOAP XmlDecoder transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  <!-- class java.lang.String - <StockQuotes><Stock><Symbol>CSCO</ -->

     <transformer ref="XsltSoap" doc:name="XsltSoap transformer"/>  
     <logger message="After SOAP XsltSoap transformer: #\[payload.getClass()\] - #\[payload\]" level="INFO" />  <!-- class \[B - \[B@16dcbb9 -->  
 </sub-flow>

 <sub-flow name="WSDL">  
     <set-payload value="#\[payload\['symbol'\]\]" doc:name="Extract Symbol"/>  
     <outbound-endpoint exchange-pattern="request-response"  address="wsdl-cxf:http://www.webservicex.net/stockquote.asmx?WSDL&amp;method=GetQuote" doc:name="Wsdl"/>  
     <transformer ref="XmlDecoder" doc:name="XmlDecoder transformer"/>  
     <transformer ref="XsltSoap" doc:name="XmlSoap transformer"/>  
 </sub-flow>  

3 样式单文件

xsl/rest-stock.xsl

<xsl:template match="/sq:string/sq:StockQuotes/sq:Stock">  
    <org.mule.example.stockquote.StockQuote>  
        <name><xsl:value-of select="sq:Name"/></name>  
        <symbol><xsl:value-of select="sq:Symbol"/></symbol>  
        <date><xsl:value-of select="sq:Date"/><xsl:text> </xsl:text><xsl:value-of select="sq:Time"/></date>  
        <change><xsl:value-of select="sq:Change"/></change>  
        <last><xsl:value-of select="sq:Last"/></last>  
        <open><xsl:value-of select="sq:Open"/></open>  
        <high><xsl:value-of select="sq:High"/></high>  
        <low><xsl:value-of select="sq:Low"/></low>  
        <volume><xsl:value-of select="sq:Volume"/></volume>  
        <previousClose><xsl:value-of select="sq:PreviousClose"/></previousClose>  
    </org.mule.example.stockquote.StockQuote>  
</xsl:template>  

xsl/soap-stock.xsl

<xsl:template match="/StockQuotes/Stock">  
    <org.mule.example.stockquote.StockQuote>  
        <name><xsl:value-of select="Name"/></name>  
        <symbol><xsl:value-of select="Symbol"/></symbol>  
        <date><xsl:value-of select="Date"/><xsl:text> </xsl:text><xsl:value-of select="Time"/></date>  
        <change><xsl:value-of select="Change"/></change>  
        <last><xsl:value-of select="Last"/></last>  
        <open><xsl:value-of select="Open"/></open>  
        <high><xsl:value-of select="High"/></high>  
        <low><xsl:value-of select="Low"/></low>  
        <volume><xsl:value-of select="Volume"/></volume>  
        <previousClose><xsl:value-of select="PreviousClose"/></previousClose>  
    </org.mule.example.stockquote.StockQuote>  
</xsl:template>  

截取的http://www.webservicex.net/stockquote.asmx服务的响应:


CSCO 28.885 6/23/2015 -0.055 29.000 29.036 28.820 1836184 146.91B 28.940 -0.190% 22.490 - 30.310 1.721 16.784 Cisco Systems

4 测试

1)REST

日志输出:

INFO 2015-06-23 22:51:41,322 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Enter REST when clause.
INFO 2015-06-23 22:51:41,327 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Before REST set-payload: class java.util.HashMap - {symbol=CSCO, method=REST}
INFO 2015-06-23 22:51:41,330 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After REST set-payload: class java.lang.String - Symbol=CSCO

++++++++++++++++++++ 子流REST中的日志输出(子流REST的输入类型为String) +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO 2015-06-23 22:51:41,332 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Before REST http:outbount-endpoint: class java.lang.String - Symbol=CSCO
INFO 2015-06-23 22:51:41,335 [[stockquote].connector.http.mule.default.receiver.02] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 22:51:41,335 [[stockquote].connector.http.mule.default.receiver.02] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default response transformer: org.mule.transport.http.transformers.MuleMessageToHttpResponse
INFO 2015-06-23 22:51:41,335 [[stockquote].connector.http.mule.default.receiver.02] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 22:51:41,335 [[stockquote].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.http.mule.default.dispatcher.32667419'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 22:51:41,335 [[stockquote].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.http.mule.default.dispatcher.32667419'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 22:51:43,179 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After REST http:outbount-endpoint: class org.mule.transport.http.ReleasingInputStream - org.mule.transport.http.ReleasingInputStream@3d2056

处理器从ReleasingInputStream中解除XML字符串
INFO 2015-06-23 22:51:43,183 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After REST XmlDecoder transformer: class java.lang.String -
CSCO28.716/23/2015-0.2329.0029.0428.714101960146.02B28.94-0.79%22.49 - 30.311.7216.68Cisco Systems
INFO 2015-06-23 22:51:43,194 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After REST XsltRest transformer: class [B - [B@64c4fb
++++++++++++++++++ 子流REST结束(子流REST的输出类型为'[B') ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

INFO 2015-06-23 22:51:43,196 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After HTTP_input REST: class [B - [B@64c4fb
INFO 2015-06-23 22:51:43,198 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After HTTP_input REST processor-chain: class [B - [B@64c4fb
INFO 2015-06-23 22:51:43,199 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: Before HTTP_input XmlToObject transformer: class [B - [B@64c4fb
INFO 2015-06-23 22:51:43,211 [[stockquote].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: After HTTP_input XmlToObject transformer: class org.mule.example.stockquote.StockQuote - StockQuote[symbol=CSCO, name=Cisco Systems, date=6/23/2015 10:37am, last=28.71, change=-0.23, open=29.00, high=29.04, low=28.71, volume=4101960, previousClose=28.94]

2)SOAP

日志:

INFO 2015-06-23 23:04:20,910 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: Enter SOAP when clause.
INFO 2015-06-23 23:04:20,912 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: Before SOAP set-payload: class java.util.HashMap - {symbol=CSCO, method=SOAP}
INFO 2015-06-23 23:04:20,915 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After SOAP set-payload: class java.lang.String - CSCO
INFO 2015-06-23 23:04:20,919 [[stockquote].connector.http.mule.default.receiver.03] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 23:04:20,919 [[stockquote].connector.http.mule.default.receiver.03] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default response transformer: org.mule.transport.http.transformers.MuleMessageToHttpResponse
INFO 2015-06-23 23:04:20,920 [[stockquote].connector.http.mule.default.receiver.03] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 23:04:20,920 [[stockquote].connector.http.mule.default.receiver.03] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.http.mule.default.dispatcher.18322687'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 23:04:20,920 [[stockquote].connector.http.mule.default.receiver.03] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.http.mule.default.dispatcher.18322687'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 23:04:22,391 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After SOAP http:outbound-endpoint: class org.mule.transport.http.ReleasingInputStream - org.mule.transport.http.ReleasingInputStream@1b403a3
INFO 2015-06-23 23:04:22,400 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After SOAP processor-chain: class java.lang.String - CSCO28.72996/23/2015-0.210129.000029.036028.65004746376146.12B28.9400-0.7260%22.4900 - 30.31001.721016.6937Cisco Systems
INFO 2015-06-23 23:04:22,419 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After SOAP XmlDecoder transformer: class java.lang.String - CSCO28.72996/23/2015-0.210129.000029.036028.65004746376146.12B28.9400-0.7260%22.4900 - 30.31001.721016.6937Cisco Systems
INFO 2015-06-23 23:04:22,429 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After SOAP XsltSoap transformer: class [B - [B@1abd128
INFO 2015-06-23 23:04:22,432 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: Before HTTP_input XmlToObject transformer: class [B - [B@1abd128
INFO 2015-06-23 23:04:22,435 [[stockquote].connector.http.mule.default.receiver.03] org.mule.api.processor.LoggerMessageProcessor: After HTTP_input XmlToObject transformer: class org.mule.example.stockquote.StockQuote - StockQuote[symbol=CSCO, name=Cisco Systems, date=6/23/2015 10:50am, last=28.7299, change=-0.2101, open=29.0000, high=29.0360, low=28.6500, volume=4746376, previousClose=28.9400]

3)WSDL

日志:

INFO 2015-06-23 23:05:36,386 [[stockquote].connector.http.mule.default.receiver.04] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 23:05:36,386 [[stockquote].connector.http.mule.default.receiver.04] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default response transformer: org.mule.transport.http.transformers.MuleMessageToHttpResponse
INFO 2015-06-23 23:05:36,386 [[stockquote].connector.http.mule.default.receiver.04] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.http.transformers.ObjectToHttpClientMethodRequest
INFO 2015-06-23 23:05:36,386 [[stockquote].connector.http.mule.default.receiver.04] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'connector.http.mule.default.dispatcher.5785394'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 23:05:36,386 [[stockquote].connector.http.mule.default.receiver.04] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'connector.http.mule.default.dispatcher.5785394'. Object is: HttpClientMessageDispatcher
INFO 2015-06-23 23:05:37,407 [[stockquote].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: Before HTTP_input XmlToObject transformer: class [B - [B@19362f0
INFO 2015-06-23 23:05:37,415 [[stockquote].connector.http.mule.default.receiver.04] org.mule.api.processor.LoggerMessageProcessor: After HTTP_input XmlToObject transformer: class org.mule.example.stockquote.StockQuote - StockQuote[symbol=CSCO, name=Cisco Systems, date=6/23/2015 10:51am, last=28.725, change=-0.215, open=29.000, high=29.036, low=28.650, volume=4786820, previousClose=28.940]

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章