如何使用请求路径在Mule ESB中路由

时间:2015-05-26 13:26:51

标签: java xml mule

您好我正在尝试在流程中添加路由器,以便我可以重复使用我的代码并避免重复。

我通常为每个请求路径执行一个流程,例如:

HTTP LISTENER = localhost:8080 / mule1 HTTP LISTENER = localhost:8080 / mule2

我想知道是否可以使用请求路径通过Router / Choice连接器进行路由。我无法做到,因为它告诉我只有一个听众/

这是我的代码:

<flow name="Tickets">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <logger message="#[message.inboundProperties.'http.listener.path']" level="INFO" doc:name="Logger"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.'http.request.path' == &quot;/getTicketByTicketCode&quot;]">
                <set-payload value="#[message.inboundProperties.'http.query.params'.ticketcode]" doc:name="Set Payload"/>
                <scripting:component doc:name="Groovy">
                    <scripting:script engine="Groovy"><![CDATA[def writer = new StringWriter() 
def xml = new groovy.xml.MarkupBuilder(writer)
xml.mkp.xmlDeclaration(version: "1.0", encoding: "UTF-8")
xml.GetTicketByCode(xmlns: 'http://tempuri.org/') { 
  code(payload)
} 
result = writer.toString() ]]></scripting:script>
                </scripting:component>
                <ws:consumer config-ref="Web_Service_Consumer" operation="GetTicketByCode" doc:name="Web Service Consumer"/>
            </when>
            <when expression="#[message.inboundProperties.'http.request.path' == &quot;/validateTickets&quot;]">
                <scripting:component doc:name="Groovy">
                    <scripting:script engine="Groovy"><![CDATA[]]></scripting:script>
                </scripting:component>
                <ws:consumer config-ref="Web_Service_Consumer" operation="ValidateTicket" doc:name="Web Service Consumer"/>
            </when>
        </choice>
        <mulexml:xslt-transformer xsl-file="removeattributes.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>
        <json:xml-to-json-transformer doc:name="XML to JSON"/>
    </flow>


INFO  2015-05-26 09:25:40,303 [[billeterie].HTTP_Listener_Configuration.worker.01] org.mule.module.http.internal.listener.HttpListenerRegistry: No listener found for request: (GET)/getTicketByTicketCode
INFO  2015-05-26 09:25:40,303 [[billeterie].HTTP_Listener_Configuration.worker.01] org.mule.module.http.internal.listener.HttpListenerRegistry: Available listeners are: [(*)/]

有没有办法让这项工作?或者唯一的方法是添加另一个像路径一样的queryParam。谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用以下path="/*",我已修改您的流程如下: -

<flow name="Tickets">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/*" doc:name="HTTP"/>
        <logger message="#[message.inboundProperties.'http.listener.path']" level="INFO" doc:name="Logger"/>
        <choice doc:name="Choice">
            <when expression="#[message.inboundProperties.'http.request.path'.contains('/getTicketByTicketCode')]">
                 <logger message="getTicketByTicketCode flow" level="INFO" doc:name="Logger"/>
                 <set-payload value="#[message.inboundProperties.'http.query.params'.ticketcode]" doc:name="Set Payload"/>
                <scripting:component doc:name="Groovy">
                    <scripting:script engine="Groovy"><![CDATA[def writer = new StringWriter() 
def xml = new groovy.xml.MarkupBuilder(writer)
xml.mkp.xmlDeclaration(version: "1.0", encoding: "UTF-8")
xml.GetTicketByCode(xmlns: 'http://tempuri.org/') { 
  code(payload)
} 
result = writer.toString() ]]></scripting:script>
                </scripting:component>
                <ws:consumer config-ref="Web_Service_Consumer" operation="GetTicketByCode" doc:name="Web Service Consumer"/>

            </when>
            <when expression="#[message.inboundProperties.'http.request.path'.contains('/validateTickets')]">
                 <logger message="validateTickets flow" level="INFO" doc:name="Logger"/> 
                  <scripting:component doc:name="Groovy">
                    <scripting:script engine="Groovy"><![CDATA[]]></scripting:script>
                </scripting:component>
                <ws:consumer config-ref="Web_Service_Consumer" operation="ValidateTicket" doc:name="Web Service Consumer"/>
            </when>
            <otherwise>
               <logger message="Other than this url " level="INFO" doc:name="Logger"/> 

            </otherwise>

           <mulexml:xslt-transformer xsl-file="removeattributes.xsl" maxIdleTransformers="2" maxActiveTransformers="5" doc:name="XSLT"/>
        <json:xml-to-json-transformer doc:name="XML to JSON"/> 
        </choice>

    </flow>

现在,如果您的网址包含 getTicketByTicketCode ,那么它将转到第一个流程,如果它包含 validateTickets ,它将转到第二个流程....

或者如果它没有这两个中的任何一个,它将在否则条件下打印记录器