如何将变量从xml传递到mule中的java类?

时间:2013-02-26 07:50:50

标签: groovy transform mule

我在mule config xml中有一个变量,我想在Java文件中访问它。我怎么做? 我找到了以下方法:

  • 使用组件
  • 使用变压器
  • 使用诸如groovy之类的脚本

我想将该变量发送到java文件,然后将其导入java文件中的数据库。 我的配置是:

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:cxf="http://www.mulesoft.org/schema/mule/cxf" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
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="CE-3.3.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
    http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd 
    http://www.mulesoft.org/schema/mule/cxf http://www.mulesoft.org/schema/mule/cxf/current/mule-cxf.xsd 
    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/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd ">

<set-variable variableName="remoteClientAddress"
    value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
<message-properties-transformer name="setProperty"
    scope="session" doc:name="Message Properties">
    <add-message-property key="remoteClientAddress"
        value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
</message-properties-transformer>


<flow name="service-cxf-wsdlfirstFlow1" doc:name="service-cxf-wsdlfirstFlow1">
    <http:inbound-endpoint host="localhost" port="8085"
        path="Weather/Service" exchange-pattern="request-response" doc:name="HTTP">
    </http:inbound-endpoint>
    <set-variable variableName="remoteClientAddress"
        value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
    <message-properties-transformer doc:name="myproperty"
        scope="session">
        <add-message-property key="remoteClientAddress"
            value="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
    </message-properties-transformer>

    <component doc:name="classTest" class="org.mule.example.scripting.IpClient" />

    <logger
        message="#[groovy:message.getInboundProperty('MULE_REMOTE_CLIENT_ADDRESS')]"
        level="INFO" doc:name="Logger" />
    <cxf:proxy-service payload="envelope" service="Weather"
        wsdlLocation="http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl"
        namespace="http://ws.cdyne.com/WeatherWS/" validationEnabled="true"
        doc:name="SOAP">
    </cxf:proxy-service>

    <copy-properties propertyName="SOAPAction" doc:name="Property" />

    <cxf:proxy-client payload="body"
        enableMuleSoapHeaders="true" doc:name="SOAP" />

    <outbound-endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
        exchange-pattern="request-response" doc:name="Generic">
    </outbound-endpoint>


</flow>

我的mule版本是CE 3.3.0。我使用组件来定义java类的位置,但是在运行我的项目之后我有一些错误:

ERROR 2013-02-27 11:15:20,129 [[mule-sample_].connector.http.mule.default.receiver.02] org.mule.exception.DefaultMessagingExceptionStrategy: 

Message               : Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
]
Code                  : MULE_ERROR-321
--------------------------------------------------------------------------------
Exception stack is:
1. Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
] (org.mule.model.resolvers.EntryPointNotFoundException)
org.mule.model.resolvers.DefaultEntryPointResolverSet:52 (http://www.mulesoft.org/docs/site/current3/apidocs/org/mule/model/resolvers/EntryPointNotFoundException.html)
--------------------------------------------------------------------------------
Root Exception stack trace:
org.mule.model.resolvers.EntryPointNotFoundException: Failed to find entry point for component, the following resolvers tried but failed: [
CallableEntryPointResolver: Object "IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]}" does not implement required interface "interface org.mule.api.lifecycle.Callable"
ReflectionEntryPointResolver: Found too many possible methods on object "org.mule.example.scripting.IpClient" that accept parameters "{class java.lang.String}", Methods matched are "[public void org.mule.transformer.AbstractTransformer.setMimeType(java.lang.String) throws javax.activation.MimeTypeParseException, public final java.lang.Object org.mule.transformer.AbstractTransformer.transform(java.lang.Object) throws org.mule.api.transformer.TransformerException, public void org.mule.transformer.AbstractTransformer.setName(java.lang.String), public void org.mule.transformer.AbstractTransformer.setEncoding(java.lang.String)]"
AnnotatedEntryPointResolver: Component: IpClient{this=1e68a2b, name='null', ignoreBadInput=false, returnClass=SimpleDataType{type=java.lang.Object, mimeType='*/*'}, sourceTypes=[]} doesn't have any annotated methods, skipping.
MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event
]
at org.mule.model.resolvers.DefaultEntryPointResolverSet.invoke(DefaultEntryPointResolverSet.java:52)
at org.mule.component.DefaultComponentLifecycleAdapter.invoke(DefaultComponentLifecycleAdapter.java:343)
at org.mule.component.AbstractJavaComponent.invokeComponentInstance(AbstractJavaComponent.java:86)
+ 3 more (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

2 个答案:

答案 0 :(得分:1)

根据鳕鱼eoyu给出的添加属性

<set-variable variableName="remoteClientAddress" value ="#[message.inboundProperties    ['MULE_REMOTE_CLIENT_ADDRESS']]"/>

这会将属性设置为INVOCATION范围内的mulemessage。

您可以使用以下代码行访问IpClient组件中的属性

  msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION);

EX:

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
    MuleMessage msg = eventContext.getMessage();

    String remClient = msg.getProperty("remoteClientAddress", PropertyScope.INVOCATION);

希望这有帮助。

答案 1 :(得分:0)

<message-properties-transformer name="myScope"  scope="session">
    <add-message-property key="remoteClientAddress"
        value ="#[message.inboundProperties['MULE_REMOTE_CLIENT_ADDRESS']]" />
</message-properties-transformer>