前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >apache synapse使用(2)

apache synapse使用(2)

作者头像
cloudskyme
发布2018-03-20 15:33:15
9920
发布2018-03-20 15:33:15
举报
文章被收录于专栏:cloudskyme

接着上面看官方的示例

消息中介示例

1,本地注册项,可重复使用的端点和序列

代码语言:javascript
复制
<!-- 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,错误处理

代码语言:javascript
复制
<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信息并且变化消息的方向

代码语言:javascript
复制
<!-- 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协议头,修改传入或传出的消息

代码语言:javascript
复制
<!-- 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/

直接指向

代码语言:javascript
复制
http://localhost:9000/services/SimpleStockQuoteService
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2011-11-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档