简体   繁体   中英

How do I pass a parameter to a Java Component from a Mule flow?

I'm just getting started with mule and cant figure out how I can possibly pass a part of my request header as a parameter/argument to the Java component.

My Java Component is as follows

public String processHeader(String in)
{
   //process header
   System.out.print(" Header" + in);
}

Ive been able to access processHeader in the following manner from the flow

            <component> 
              <method-entry-point-resolver> 
              <include-entry-point method="processHeader" /> 
              </method-entry-point-resolver> 
              <singleton-object class="my.test.mule.Processor" /> 
            </component> 

Accessing the above using http://localhost:8080/test . Prints Header test

I'm able to dump the contents of the header using the following

<logger level="INFO" doc:name="Logger" message="#[headers:INBOUND:*]"/>

But I cant seem to figure out how to pass the message as an argument to processHeader nor can I find any relevant examples. Any help would be appreciated.

Btw, I'm using Mule 3.5 if that matters.

implement Callable interface for your java component. When default methods are overridden, you will get eventContext as parameter inside which you can find mule message which in turn gives you access to headers and payload. sample is here:

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;
import org.mule.api.transport.PropertyScope;
public class Test implements Callable {

@Override
public Object onCall(MuleEventContext eventContext) throws Exception {
    eventContext.getMessage().getProperty("header1", PropertyScope.INBOUND);
    return null;
}
}

You have a number of options:

@Mule Annotation

A parameter injection annotation that can be used on component entry points and transformer methods, this annotation can be used to execute a Mule Expression on the message payload with the result being passed into the method.

Also:

@InboundHeaders Annotation

This annotation controls how the current message inbound headers are passed into a method. The annotation supports, Map, List, single headers, wildcards and optional entries. It can be used on component entry points and @Transformer methods.

Probably used in conjunction with:

@Payload Annotation

A parameter injection annotation that can be used on component entry points and transformer methods defined using the @Transformer annotation, this annotation controls how the current message payload is passed into a method by performing automatic transformation of the message payload to match the annotated parameter type. For example, if you are expecting an XML document, this can be injected into a component entry point and automatically converted into a org.wc3.dom.Document.

You could also implement Callable as stated in a different response, but that would be much less expressive than this solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM