In the previous lesson Invoking Java Component Over HTTP, we learned how to invoke a simple method of a Java component in Mule Flow. Let’s now go a bit further and see how Mule maps a request message to a specific method in your component using Entry Point Resolvers.
At a high level, Mule uses three types of Entry Point Resolvers.
- Reflection Entry Point Resolver
- Annotated Entry Point Resolver
- Callable interface
In this article, I will focus on the Reflection Entry Point Resolver, which is used to determine the entry point on a component after a message has been received. The entry point is discovered using the message payload type(s) as the argument using reflection. For multiple parameters the payload should be an Array of objects. If the message payload is of type NullPayload the resolver will look for a no-argument method.
Creating Java Component
Let us now use the following Java class to learn how the Reflection Entry Point Resolver works.
RelectionEntryPointResolverExample contains three methods:
- multipleArgumentsMethod – takes in two String arguments. We will need to pass an array of String[] elements to invoke this method. We will see in the next steps how this can be done.
- singleArgumentMethod – takes in one String argument. This method will be invoked when the request payload contains a single String.
- noArgumentMethod – has no arguments. We need to ensure the request payload is null to invoke this method.
Building a Mule Flow
Let us build this flow in Mule Studio now. This flow will receive two request parameters: name and dept. These parameters will be transformed in such a way that each of the methods in ReflectionEntryPointResolverExample.java class are invoked.
As explained in Invoking Java Component Over HTTP, create the “learningmule” Mule Project followed by a “java” Mule Flow. Create an Inbound HTTP endpoint listening on port 8081 and path “java”.
Drag and drop an Expression Transformer from the component palette next to the HTTP Inbound Endpoint, as shown below. We will use an Expression Transformer to transform request parameter values to an Array of Strings. For example, the HTTP GET query string {name=Amjad&dept=Services} will be transformed to an array of two Strings: [“Amjad”, “Services”]. More details on Mule Transformers can be read here: http://www.mulesoft.org/documentation/display/MULE3USER/Using+Transformers.
[singlepic id=53 w=580 h=355]
Double-click on the “Expression” icon in the Flow to open the Expression properties dialog. Navigate to the “Advanced” tab.
[singlepic id=56 w=580 h=355]
Click the (+) icon under “Return Arguments” section to add “header” as evaluator and “inbound:name” as Expression. Similarly, add another return argument for “inbound:dept” parameter.
[singlepic id=57 w=580 h=355]
Request parameters by default are stored in an implicit “header” object under “inbound” scope. More details on Expressions can be read here: http://www.mulesoft.org/documentation/display/MULE3USER/Using+Expressions.
The resulting flow code looks like this:
[singlepic id=58 w=580 h=355]
Drag and drop a Java component next to “Expression Transformer and point the singleton object class to “com.mulesoft.java.ReflectionEntryPointResolverExample”, which we created earlier. Drag and drop an echo component next to it. The resulting flow looks like this:
[singlepic id=59 w=580 h=355]
The Mule flow now looks like this:
[singlepic id=60 w=580 h=355]
When we run the Mule Flow, we should be able to see the request entry point being resolved to multipleArgumentsMethod. We will confirm that very soon.
Similarly, lets create another Expression Transformer with just one return argument “inbound:name”, followed by Java and Echo components. It will be easier to copy and paste the code. The resulting code looks like this:
[singlepic id=61 w=580 h=355]
Finally, let’s use another Expression Transformer to create a null payload. We will use the Groovy expression evaluator to do this, as shown below:
[singlepic id=62]
The completed Mule flow is shown below:
[singlepic id=63 w=580 h=355]
Executing Mule Flow
Run the “java” flow as a Mule Application and access the following URL:
http://localhost:8081/java?name=Amjad&dept=Services
[singlepic id=54 w=580 h=355]
Observe the output in Mule Studio in the “Console” tab. We can see each of the three methods invoked when appropriate arguments are received.
[singlepic id=55 w=580 h=355]