mule: java component

Post on 07-Jan-2017

142 Views

Category:

Software

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

MULE: JAVA COMPONENTENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE

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

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

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

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

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

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

existing Java class• Add a new Java class

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

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(); }

}

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

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)

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; }}

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"/>

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; }}

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>

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"; }}

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>

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>

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>

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

top related