apache synapse使用(2)

來源:互聯網
上載者:User

接著上面看官方的樣本

訊息中介樣本

1,本地註冊項,可重複使用的端點和序列

<!-- Local Registry entry definitions, reusable endpoints and sequences --><definitions xmlns="http://ws.apache.org/ns/synapse"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">    <!-- define a string resource entry to the local registry -->    <localEntry key="version">0.1</localEntry>    <!-- define a reuseable endpoint definition -->    <endpoint name="simple">        <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>    </endpoint>    <!-- define a reusable sequence -->    <sequence name="stockquote">        <!-- log the message using the custom log level. illustrates custom properties for log -->        <log level="custom">            <property name="Text" value="Sending quote request"/>            <property name="version" expression="get-property('version')"/>            <property name="direction" expression="get-property('direction')"/>        </log>        <!-- send message to real endpoint referenced by key "simple" endpoint definition -->        <send>            <endpoint key="simple"/>        </send>    </sequence>    <sequence name="main">        <in>            <property name="direction" value="incoming"/>            <sequence key="stockquote"/>        </in>        <out>            <send/>        </out>    </sequence></definitions>

用戶端執行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/

 可以看到輸出的結果

 Standard :: Stock price = $80.1611906447455

過程是先進入main然後直接進入可重用序列stockqnote,最後將請求的資訊發送到http://localhost:9000/services/SimpleStockQuoteService

使用http://localhost:9000/services/SimpleStockQuoteService?wsdl可以看到顯示的結果

 

2,錯誤處理

<definitions xmlns="http://ws.apache.org/ns/synapse"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">    <!-- the default fault handling sequence used by Synapse - named 'fault' -->    <sequence name="fault">        <log level="custom">            <property name="text" value="An unexpected error occured"/>            <property name="message" expression="get-property('ERROR_MESSAGE')"/>        </log>        <drop/>    </sequence>    <sequence name="sunErrorHandler">        <log level="custom">            <property name="text" value="An unexpected error occured for stock SUN"/>            <property name="message" expression="get-property('ERROR_MESSAGE')"/>            <!--<property name="detail" expression="get-property('ERROR_DETAIL')"/>-->        </log>        <drop/>    </sequence>    <sequence name="main">        <in>            <switch xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol">                <case regex="IBM">                    <send>                        <endpoint>                            <address uri="http://localhost:9000/services/SimpleStockQuoteService"/>                        </endpoint>                    </send>                </case>                <case regex="MSFT">                    <send>                        <endpoint key="bogus"/>                    </send>                </case>                <case regex="SUN">                    <sequence key="sunSequence"/>                </case>            </switch>            <drop/>        </in>        <out>            <send/>        </out>    </sequence>    <sequence name="sunSequence" onError="sunErrorHandler">        <send>            <endpoint key="sunPort"/>        </send>    </sequence></definitions>

 用戶端執行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

執行查詢MSFT的股價,因為沒有對應的端點尋找最接近的錯誤處理,服務端看到提示

INFO LogMediator text = An unexpected error occured, message = Couldn't find the endpoint with the key : bogus

執行查看sun的股價

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

最後列印出資訊

INFO LogMediator text = An unexpected error occured for stock SUN, message = Couldn't find the endpoint with the key : sunPort

這個是在sunSeqence這個序列裡執行的。

 

3,建立錯誤的SOAP資訊並且變化訊息的方向

<!-- Creating SOAP fault messages and changing the direction of a message --><definitions xmlns="http://ws.apache.org/ns/synapse"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">    <sequence name="myFaultHandler">        <makefault response="true"><code xmlns:tns="http://www.w3.org/2003/05/soap-envelope" value="tns:Receiver"/>            <reason expression="get-property('ERROR_MESSAGE')"/>        </makefault>        <send/>    </sequence>    <sequence name="main" onError="myFaultHandler">        <in>            <switch xmlns:m0="http://services.samples" source="//m0:getQuote/m0:request/m0:symbol">                <case regex="MSFT">                    <send>                        <endpoint>                            <address uri="http://bogus:9000/services/NonExistentStockQuoteService"/>                        </endpoint>                    </send>                </case>                <case regex="SUN">                    <send>                        <endpoint>                            <address uri="http://localhost:9009/services/NonExistentStockQuoteService"/>                        </endpoint>                    </send>                </case>            </switch>            <drop/>        </in>        <out>            <send/>        </out>    </sequence></definitions>

 用戶端調用

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=MSFT

 返回

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode> <faultstring>java.net.UnknownHostException: bogus</faultstring><detail /></soapenv:Fault>

執行

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dsymbol=SUN

返回

<soapenv:Fault xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><faultcode>soapenv:Client</faultcode> <faultstring>java.net.ConnectException: Connection refused</faultstring><detail /></soapenv:Fault>

 

4,操縱SOAP協議頭,修改傳入或傳出的訊息

<!-- Manipulating SOAP headers, and filtering incoming and outgoing messages --><definitions xmlns="http://ws.apache.org/ns/synapse"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"             xsi:schemaLocation="http://ws.apache.org/ns/synapse http://synapse.apache.org/ns/2010/04/configuration/synapse_config.xsd">    <sequence name="main">        <in>            <header name="To" value="http://localhost:9000/services/SimpleStockQuoteService"/>        </in>        <send/>    </sequence></definitions>

 修改協議頭

用戶端調用

ant stockquote -Dtrpurl=http://localhost:8280/

直接指向

http://localhost:9000/services/SimpleStockQuoteService

 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.