简体   繁体   中英

How to route in Mule ESB using request path

Hello I am trying to add a router in flow, so that I can re-use my code and to avoid duplication.

I normally do a flow for each request path for example :

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

I would like to know if it's possible to route via the Router/Choice connector using the request path. I am not able to do it as it tells me there is only a listener for /

Here's my code :

<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: [(*)/]

Is there a way to make this work? Or is the only way is to add another queryParam that would act like the path. Thank you

You can use the following path="/*" and I have modified your flow as following:-

<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>

Now if your url contain getTicketByTicketCode then it will go to the first flow, and if it contains validateTickets it will go to second flow....

or else if it doesn't have any of these 2 it will print the logger in otherwise condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM