简体   繁体   中英

Use Java object in mule

I am using a java object which should return me an endpoint, then I want to invoke a service hosted at the specified endpoint. Please assist.

Below is my effort

In mule.xml

<spring:beans>
        <spring:bean id="reqUrl" class="com.mule.sbus.drools.RequestUrl"
            scope="singleton" />
    </spring:beans>

    <bpm:drools />
    <http:listener-config name="NorthboundSingleEntrypoint"
        host="0.0.0.0" port="8191" doc:name="HTTP Listener Configuration" />

    <http:request-config name="HTTP_Request_Configuration"
        host="acdc3a38cffc411e5a18606a62b4ee07-877599714.us-west-1.elb.amazonaws.com"
        port="80" doc:name="HTTP Request Configuration" />

    <flow name="sbusdroolsFlow">
        <http:listener config-ref="NorthboundSingleEntrypoint"
            path="/*" doc:name="HTTP" />

        <set-variable variableName="requestUrl"
            value="#[message.inboundProperties.'http.request.path']" doc:name="RequestUrl" />

        <script:component doc:name="Script">
            <script:script engine="groovy">
                <![CDATA[
                    return requestUrl;
                ]]>
            </script:script>
        </script:component>

        <bpm:rules rulesDefinition="routingRules.drl"
            initialFacts-ref="reqUrl" />
        <expression-transformer evaluator="groovy"
            expression="message.getPayload().getObject()" doc:name="Expression" />

        <logger message="#[groovy:message.getPayload().getObject()]" level="INFO"
            doc:name="LoggerResp" />

    </flow>

Below is my drools .drl

#default dialect for the semantic code will be MVEL
global org.mule.module.bpm.MessageService mule;

import com.mule.sbus.drools.RequestUrl

dialect "mvel"

declare RequestUrl
    @role(event)
end

rule "test123"
    lock-on-active
when
    $url:RequestUrl(url=="test123")
then
    #order.setDestination("WAREHOUSE_A");
    modify($url){setEndPoint("test123")}
end

rule "test234"
    lock-on-active
when
    $url:RequestUrl(url=="test234")
then
    #order.setDestination("WAREHOUSE_A");
    modify($url){setEndPoint("test234")}
end

and my java class

package com.mule.sbus.drools;

public class RequestUrl {

    private String url;

    private String endPoint;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getEndPoint() {
        return endPoint;
    }

    public void setEndPoint(String endPoint) {
        /*if(endPoint=="test123")
            this.endPoint = endPoint;
        else*/
            this.endPoint = "/checkcibil";
            System.out.println("inside java :::: " + endPoint);
    }

    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "url : " + url + " endPoint : " + endPoint;
    }

}

As you can see I am invoking my setter from the drools file and once I get the string I want to print the same using

<logger message="#[groovy:message.getPayload().getObject()]" level="INFO"
            doc:name="LoggerResp" />

but I don't know what should be the message value to use. Please assist

Got the answer,

As I am using groovy, have commented drools, and the updated the code as below

 <script:component doc:name="Script">
            <script:script engine="groovy">
                <![CDATA[
                reqUrl.setEndPoint(requestUrl);
                String endpnt = reqUrl.getEndPoint();
                message.setProperty('endpnt', endpnt,org.mule.api.transport.PropertyScope.INVOCATION);
                ]]>
            </script:script>
        </script:component>

                <logger message="#[flowVars['endpnt']]" level="INFO" doc:name="LoggerResp" />

Using groovy I am invoking the setter and the call the getter to have the value in endpt variable. This can now be set as a property in the message and later we can retrieve the same (outside groovy script tags), using #[flowVars['endpnt']]

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