Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 695
userNotFound Avatar asked Jul 10 '26 14:07

userNotFound


2 Answers

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;
}
}
like image 101
tortoise Avatar answered Jul 12 '26 03:07

tortoise


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.

like image 29
Víctor Romero Avatar answered Jul 12 '26 04:07

Víctor Romero