如何使用Java组件上的注释将MuleMessage和Payload作为参数传入?

时间:2013-08-14 17:40:22

标签: mule mule-studio

我有一个类,我作为一个java组件接线,如下所示:

    <component doc:name="eduPubService">
      <method-entry-point-resolver>
          <include-entry-point method="checkAndReserve" />
          </method-entry-point-resolver>
          <spring-object bean="edusPubService"/>
    </component>

我正在尝试使用Annotations来访问MuleMessage和Payload,如下所示:

public class PublicationServiceImpl implements PublicationService {
    public Object checkAndReserve(@Mule("message") MuleMessage message, @Payload String payload) throws Exception {  ..... }

我传入的Payload是一个字符串。我不认为我完全理解这些注释是如何工作的。我假设他们从类似EventContext的东西中获取了MuleMessage和Payload,但是我得到了EntryPointNotFoundExceptions,因为它正在寻找一个只接受String作为参数的方法'checkAndReserve'。如何定义使用上述注释的类和方法,并使用方法名称调用?我不喜欢使用Callable的想法,因为我必须为每个自定义组件/变换器创建一个单独的类。如果我只依靠方法签名来解析有效负载,因为错误似乎表明是必要的,我仍然不知道如何处理会话变量,骡子消息等等。

1 个答案:

答案 0 :(得分:1)

如果要更改流中的Message,比如设置会话变量,建议创建一个从AbstractMessageTransformer扩展的Custom Transformer,并实现transformMessage方法。

这是自定义变换器的示例:

package com.test;    

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

public class MyTransformer extends AbstractMessageTransformer{


    @Override
    public Object transformMessage(MuleMessage message, String outputEncoding)
            throws TransformerException {
        //Add Session Variables
        message.setProperty("variable1", "value1", PropertyScope.SESSION);
        message.setProperty("variable2", 100, PropertyScope.SESSION);
        //Get Payload
        Object payload = message.getPayload();
        return message;
    }


}

这就是你在流程中使用它的方式:

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns="http://www.mulesoft.org/schema/mule/core"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:spring="http://www.springframework.org/schema/beans"
      xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting"
      xmlns:http="http://www.mulesoft.org/schema/mule/http"
      xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
      xmlns:vm="http://www.mulesoft.org/schema/mule/vm"
      xmlns:file="http://www.mulesoft.org/schema/mule/file"
      xmlns:ftp="http://www.mulesoft.org/schema/mule/ftp"
      xmlns:db="http://www.mulesoft.org/schema/mule/db"
      xmlns:mule-xml="http://www.mulesoft.org/schema/mule/xml"
      xmlns:jersey="http://www.mulesoft.org/schema/mule/jersey"
      xmlns:json="http://www.mulesoft.org/schema/mule/json"
      xmlns:ws="http://www.mulesoft.org/schema/mule/ws"
      xmlns:smtps="http://www.mulesoft.org/schema/mule/smtps"
      xmlns:email="http://www.mulesoft.org/schema/mule/email"
      xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
      xmlns:batch="http://www.mulesoft.org/schema/mule/batch"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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
        http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
        http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
        http://www.mulesoft.org/schema/mule/vm http://www.mulesoft.org/schema/mule/vm/current/mule-vm.xsd
        http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd
        http://www.mulesoft.org/schema/mule/ftp http://www.mulesoft.org/schema/mule/ftp/current/mule-ftp.xsd
        http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
        http://www.mulesoft.org/schema/mule/xml http://www.mulesoft.org/schema/mule/xml/current/mule-xml.xsd
        http://www.mulesoft.org/schema/mule/jersey http://www.mulesoft.org/schema/mule/jersey/current/mule-jersey.xsd
        http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
        http://www.mulesoft.org/schema/mule/ws http://www.mulesoft.org/schema/mule/ws/current/mule-ws.xsd
        http://www.mulesoft.org/schema/mule/smtps http://www.mulesoft.org/schema/mule/smtps/current/mule-smtps.xsd
       http://www.mulesoft.org/schema/mule/email http://www.mulesoft.org/schema/mule/email/current/mule-email.xsd
       http://www.mulesoft.org/schema/mule/batch http://www.mulesoft.org/schema/mule/batch/current/mule-batch.xsd
    ">



    <custom-transformer name="myTransformer" class="com.test.MyTransformer" />


    <http:listener-config name="HTTP_Refactoring_Listener_Configuration" host="0.0.0.0" port="8181" >
        <http:worker-threading-profile maxThreadsActive="64" />
     </http:listener-config>




     <flow name="JobExecutorProcessor" doc:name="JobExecutorProcessor">
        <vm:inbound-endpoint exchange-pattern="request-response" path="vm.in.test" doc:name="VM"/>

        <logger message="Calling myTransformer" level="INFO" doc:name="Logger"/>
        <transformer ref="myTransformer" />     

     </flow>


</mule>