如何在java代码中访问Mule流变量?

时间:2015-06-11 14:04:27

标签: java mule mule-el

浏览,我发现了下面的内容:

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
import java.util.Scanner;

public class javaTest extends AbstractMessageTransformer{
    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException{

        message.getInvocationProperty("test");
        return message;
    }
}

我正在使用此代码,其中message是一个Mule流变量,我刚刚声明为“test”。问题是,当我发送有效载荷时,我得到“{NullPayload}”,这是不对的。我只是想让它返回字符串“test”。

我在这里做错了什么?我的骡子代码如下:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.6.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="localhost" port="8081" doc:name="HTTP Listener Configuration"/>
    <http:request-config name="HTTP_Request_Configuration" protocol="HTTPS" host="hooks.slack.com" doc:name="HTTP Request Configuration"/>
    <flow name="testFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>
        <set-variable variableName="test" value="Test" doc:name="Variable"/>
        <custom-transformer class="javaTest" doc:name="HelloWorld"/>
        <set-payload value="{&quot;channel&quot;: &quot;#testing&quot;, &quot;username&quot;: &quot;Testing&quot;, &quot;text&quot;: &quot;#[payload]&quot;}" doc:name="Set Payload"/>
        <logger message="#[payload]" level="INFO" doc:name="Logger"/>
        <http:request config-ref="HTTP_Request_Configuration" path="/services/slackURL" method="POST" doc:name="HTTP"/>
    </flow>
</mule>

1 个答案:

答案 0 :(得分:2)

这是否是正确的解决方案我不确定,但是我为解决这个问题所做的是在java代码中将一个变量设置为message.getInvocationProperty的值,然后返回那个java变量,就像这样:

import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class javaTest extends AbstractMessageTransformer{
    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException{
        String testS = message.getInvocationProperty("test");
        return testS;
    }
}