mule: java component

21
MULE: JAVA COMPONENT ENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE

Upload: sulthony-hartanto

Post on 07-Jan-2017

142 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Mule: Java Component

MULE: JAVA COMPONENTENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE

Page 2: Mule: Java Component

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

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

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

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

JAVA COMPONENT (1/2)To configure the Java component, selecting a class is the only required entry.

Page 7: Mule: Java Component

JAVA COMPONENT (2/2)Selecting a class:•Browse for an

existing Java class• Add a new Java class

Page 8: Mule: Java Component

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

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

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

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

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

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

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

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

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

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

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

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

SUMMARYMule allows developers to:•Build their own component and/or transformer• Simply write their favorite programming language