mule: java component
TRANSCRIPT
![Page 1: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/1.jpg)
MULE: JAVA COMPONENTENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE
![Page 2: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/2.jpg)
BASIC CONSIDERATIONS• Anypoint Studio provides a set of transformers to
handle the most common data transformation scenarios•Developer can chain transformers if a transformer
did not exist for specific needs• The DataWeave Transform Message component can
be used in place of most other transformers
![Page 3: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/3.jpg)
SPECIAL CASES• Transforming complex data structures• Applying complex business rules• The available transformers cannot meet the requirement• Simply throw the old lines of code into a component
instead of having to reengineer the code’s behavior through a series of different Mule components
![Page 4: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/4.jpg)
POSSIBLE SOLUTIONS•Building custom components and/or transformers• Turning to the most favorite Programming
Languages:o Javao .NETo Scripting languages: Groovy, Javascript, Python or Ruby
![Page 5: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/5.jpg)
JAVA• Java is the native language in which Mule is coded• The Java component enables the developer to package
custom Java code that executes when the component receives a message• The Java component can be used to enhance the
functionality and capability of your web-based applications written in Java
![Page 6: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/6.jpg)
JAVA COMPONENT (1/2)To configure the Java component, selecting a class is the only required entry.
![Page 7: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/7.jpg)
JAVA COMPONENT (2/2)Selecting a class:•Browse for an
existing Java class• Add a new Java class
![Page 8: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/8.jpg)
BASIC JAVA CLASS• A class shall implements
org.mule.api.lifecycle.Callable• A class which does not implement it, shall defines
the Entry Point Resolver• A class must be referenced in a fully-qualified name
![Page 9: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/9.jpg)
JAVA CLASS EXAMPLE (1/2)package com.mulesoft.learning;
import org.mule.api.MuleEventContext;import org.mule.api.lifecycle.Callable;
public class HelloWorld implements Callable {
@Override public Object onCall(MuleEventContext eventContext) throws Exception { eventContext.getMessage().setInvocationProperty("myProperty", "Hello World!"); return eventContext.getMessage().getPayload(); }
}
![Page 10: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/10.jpg)
JAVA CLASS EXAMPLE (2/2)Run/debug a simple flow• It will produce a variable
myProperty, and its value is Hello World!• It will keep the payload as
is
![Page 11: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/11.jpg)
ENTRY POINT RESOLVER•Determine how a message is passed to a component
in Java• Each entry point resolver is tried in turn until one
succeeds in delivering the message to the component•Needed if the class does not implement
org.mule.api.lifecycle.Callable nor extend org.mule.transformer.AbstractMessageTransformer (will be discussed in different presentation)
![Page 12: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/12.jpg)
SIMPLE CLASS (1/2)A class with only one method with an argument•No need to implement or extend anything•No need to define an Entry Point Resolver
package com.mulesoft.learning;
public class SingleMethod {
public String sayHello(String payload) { return "Hello, " + payload; }}
![Page 13: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/13.jpg)
SIMPLE CLASS (2/2)Once the payload data type equals to the required argument type, then it will be handled directly by the only one method on that class.
<component class="com.mulesoft.learning.SingleMethod" doc:name="Java"/>
![Page 14: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/14.jpg)
METHOD ENTRY POINT RESOLVER (1/2)What if, there are more than one method in the same class?package com.mulesoft.learning;
public class MultipleMethod {
public String firstMethod(String payload) { return "1. " + payload; }
public String secondMethod(String payload) { return "2. " + payload; }}
![Page 15: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/15.jpg)
METHOD ENTRY POINT RESOLVER (2/2)Solution: define the method-entry-point-resolver
<component class="com.mulesoft.learning.MultipleMethod" doc:name="Java"> <method-entry-point-resolver> <include-entry-point method="secondMethod"/> </method-entry-point-resolver></component>
![Page 16: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/16.jpg)
REFLECTION ENTRY POINT RESOLVER (1/2)How to invoke a method based on argument types and number of arguments?public class MultipleArgument { public String multipleArguments(String arg1, String arg2) { return "Multiple arguments: " + arg1 + ", " + arg2; } public String singleArgument(String arg1) { return "Single argument: " + arg1; } public String noArgument() { return "No argument"; }}
![Page 17: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/17.jpg)
REFLECTION ENTRY POINT RESOLVER (2/2)Solution: define the reflection-entry-point-resolver<expression-transformer> <return-argument expression="message.inboundProperties.'http.query.params'.arg1" optional="true"/> <return-argument expression="message.inboundProperties.'http.query.params'.arg2" optional="true"/> <!-- <return-argument expression="null" optional="true"/> --></expression-transformer><component class="com.mulesoft.learning.MultipleArgument" doc:name="Java"> <reflection-entry-point-resolver/></component>
![Page 18: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/18.jpg)
PROPERTY ENTRY POINT RESOLVERHow to invoke a method based on message property?•Use property-entry-point-resolver•Make sure the method name and the required
arguments are provided accordingly
<component class="com.mulesoft.learning.MultipleArgument" doc:name="Java"> <property-entry-point-resolver property="methodname"/></component>
![Page 19: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/19.jpg)
ENTRY POINT RESOLVER SETHow to use more than one Entry Point Resolver?
<component class="com.mulesoft.learning.MultipleArgument" doc:name="Java"> <entry-point-resolver-set> <reflection-entry-point-resolver/> <method-entry-point-resolver> <include-entry-point method="noArgument"/> </method-entry-point-resolver> </entry-point-resolver-set></component>
![Page 20: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/20.jpg)
SUMMARYMule allows developers to:•Build their own component and/or transformer• Simply write their favorite programming language
![Page 21: Mule: Java Component](https://reader035.vdocuments.us/reader035/viewer/2022070516/586fdcb71a28ab18428b65cf/html5/thumbnails/21.jpg)
RESOURCES:• https://
docs.mulesoft.com/mule-user-guide/v/3.8/script-component-reference• https://
docs.mulesoft.com/mule-user-guide/v/3.8/java-component-reference• https://
docs.mulesoft.com/mule-user-guide/v/3.8/entry-point-resolver-configuration-reference