Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

MuleSoft: Method Entry Point Resolver

DZone's Guide to

MuleSoft: Method Entry Point Resolver

Want to utilize Java components in Anypoint Studio? Here's an example of creating and using a Java class in MuleSoft's Anypoint Studio.

· Java Zone
Free Resource

Learn how to troubleshoot and diagnose some of the most common performance issues in Java today. Brought to you in partnership with AppDynamics.

The Anypoint platform provides various components and built-in functions for integration purposes. Most of our needs in a project can be fulfilled easily, especially when we use the Enterprise Edition. Not only does this include the availability of ready-to-use connectors, but also a quick response from MuleSoft support.

Let's say we have to develop integration project using Community Edition. Obviously, with limited support from them. Although we can rely on the community forum with tons of ideas and discussions, sometimes we face a common impediment from our internal developers. In particular, they resist adopting the Anypoint platform and still engage with their preferred programming language. For example, they do not want to use MEL or expression component, but prefer to write a Java class.

However, we can actually utilize Java components in Anypoint Studio. To know how this component works, here we will create a Java class for a simple mathematic operation: power.

Firstly create an empty project, and do the following steps:

  1. Open Package Explorer, then right click on src/main/java. Choose: New >> Package.

  2. Set the Name as: com.mulesoft.training and click Finish.

  3. Right click on the newly created package, and choose: New >> Class.

  4. Set the Name as: MathOperation and click Finish.

  5. Complete the class and save it.

package com.mulesoft.training;

public class MathOperation {
    public String power(String value) {
        return String.valueOf(Math.pow(Double.valueOf(value), 2));
    }
}

At this stage we have created a Java class. The next step is to create a simple flow to implement it:

  1. Open the newly created configuration: [project name].xml.

  2. Search http from the Mule Palette and drop it to the canvas.

  3. Complete the Connector Configuration, by clicking the plus button; leave all default configurations and click OK.

  4. Set Path: /power.

  5. Set Allowed Methods: POST.

  6. Search object to string from the Mule Palette and drop it in the Process section.

  7. Search java from the Mule Palette and drop it in the Process section.

    • Note: make sure to choose Java Component, instead of Java Transformer.

  8. Set Class Name: com.mulesoft.training.MathOperation.

    • Note: we can type it directly or click the Search (magnifying glass) button.

  9. Save all and run the project.

Image title

We have finished this project. Now let us test it through REST client. For this purpose, I prefer to use Postman, an extension for Chrome browser.

  1. Set the method: POST.

  2. Set the URL: localhost:8081/power.

  3. Body, raw: 4.

  4. Click the Send button.

  5. We should get the result: 16.

We've successfully executed a Java class by utilizing the Java Component!

But, what if two or more methods are present in same class? For example, there is another method in the current class: squareRoot.

package com.mulesoft.training;

public class MathOperation {

       public String power(String value) {
              return String.valueOf(Math.pow(Double.valueOf(value), 2));
       }    

       public String squareRoot(String value) {
              return String.valueOf(Math.sqrt(Double.valueOf(value)));
       }

}

Let us try it by restarting the project and retest it through Postman.

ERROR!

Message               : Failed to find entry point for component, the following resolvers tried but failed: [

MethodHeaderPropertyEntryPointResolver: The required property "method" is not set on the event

ReflectionEntryPointResolver: Found too many possible methods on object "com.mulesoft.training.MathOperation" that accept parameters "{class java.lang.String}", Methods matched are "[public java.lang.String com.mulesoft.training.MathOperation.squareRoot(java.lang.String), public java.lang.String com.mulesoft.training.MathOperation.power(java.lang.String)]"

CallableEntryPointResolver: Object "com.mulesoft.training.MathOperation@5409ca17" does not implement required interface "interface org.mule.api.lifecycle.Callable"

AnnotatedEntryPointResolver: Component: com.mulesoft.training.MathOperation@5409ca17 doesn't have any annotated methods, skipping.

]

If we refer to the error message, Mule failed to find an entry point for the component. It tried four possible entry points but failed. In general, those entry points behave as such:

Entry Point Resolver Name

Behavior

MethodHeaderPropertyEntryPointResolver

Selects a certain method as configured.

ReflectionEntryPointResolver

Select the best match method based on arguments.

CallableEntryPointResolver

Select the onCall method if the class implements org.mule.api.lifecycle.Callable.

AnnotatedEntryPointResolver

Select the best match method based on annotation.

To fix this issue, we can choose one solution from the above list. In this blog we will use: method-entry-point-resolver.

  1. Open the configuration file and display it as Configuration XML.
  2. Modify the component script:

Current script:

<component class="com.mulesoft.training.MathOperation" doc:name="Java"/>

New Script

<component class="com.mulesoft.training.MathOperation" doc:name="Java">
    <method-entry-point-resolver>
        <include-entry-point method="power"/>
    </method-entry-point-resolver>
</component>
  1. Save, restart, and retest

  2. Success!

    • Try to change the method from power to squareRoot, and retest.

Understand the needs and benefits around implementing the right monitoring solution for a growing containerized market. Brought to you in partnership with AppDynamics.

Topics:
mulesoft

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}