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

如何使用Java组件上的注释将MuleMessage和Payload作为参数传入?,mule,mule-studio,Mule,Mule Studio,我有一个类,作为java组件连接,如下所示: <component doc:name="eduPubService"> <method-entry-point-resolver> <include-entry-point method="checkAndReserve" /> </method-entry-point-resolver> <spring-objec

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

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

我传递的有效载荷是一个字符串。我想我不太明白这些注释是如何工作的。我假设他们从EventContext之类的东西获取MuleMessage和有效负载,但我得到EntryPointNotFoundExceptions,因为它正在寻找一个只接受字符串作为参数的方法“checkAndReserve”。我如何定义一个类和方法来使用上面的注释,并使用方法名调用它?我不喜欢使用Callable,因为我必须为每个自定义组件/转换器创建一个单独的类。如果我仅仅依靠方法签名来解析有效负载,正如错误似乎表明这是必要的,我仍然不知道如何获取会话变量、Mule消息等。如果您想在流中更改消息,如设置会话变量,建议创建从AbstractMessageTransformer扩展的自定义转换器,并实现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>

如果要更改流中的消息,如设置会话变量,建议创建从AbstractMessageTransformer扩展的自定义转换器,并实现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>


您需要
MuleMessage
做什么?从概念上讲,组件不必处理消息。你在转变信息吗?然后编写一个自定义转换器。或者,如果您确实需要深入访问事件,请编写自定义消息处理器。否则,使用
invoke
消息处理器,并根据您的方法的需要(负载、特定的props/vars…)调用它。不过,我认为我可能滥用了Java组件,应该在自定义转换器或自定义消息处理器中这样做。我想我需要多读一些书来理解这三种方法之间的主要区别,以及何时使用哪种方法。关于最后一个选项,
invoke
,您能给我一个示例或任何文档的链接吗?谢谢。找不到任何文档:(查找
您需要
多消息
做什么?从概念上讲,组件不必处理消息。是否转换消息?然后编写自定义转换器。或者如果确实需要深入访问事件,则编写自定义消息处理器。否则,请使用
调用
消息处理器和call您的方法需要什么(有效载荷、特定道具/变量…)。我正在使用mule方法在Java组件中获取和设置会话变量。不过,我认为我可能滥用Java组件,并且可能应该在自定义转换器或自定义消息处理器中这样做。我认为我需要多读一点,以了解这三种方法之间的关键区别,以及何时使用要使用什么。关于最后一个选项,
invoke
,您能给我一个示例或任何文档的链接吗?谢谢。找不到任何文档:(查找