如何在Java组件中引用Mule的主目录,例如:

String directory = ${Mule_Home};

最佳答案

一种方法是首先将#[mule.home]的值保存在变量中:

    <set-variable value="#[mule.home]" variableName="mule_home"
        doc:name="Variable" />


然后可以从组件中按如下方式访问它:

    MuleMessage message = eventContext.getMessage();
    String mule_home = message.getProperty("mule_home", PropertyScope.INVOCATION);


实际上更好的另一种方法是实现MuleContextAware并评估组件中的component子表达式,如下所示:

public class MyComponent implements Callable, MuleContextAware {

MuleContext context;

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {

    ExpressionLanguage el = context.getExpressionLanguage();
    String result = el.evaluate("mule.home");
    return result;
}

@Override
public void setMuleContext(MuleContext context) {
    this.context = context;
}
// include getters and setters for context

09-30 11:22