16
MULE: JAVA TRANSFORMER ENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE

Mule: Java Transformer

Embed Size (px)

Citation preview

Page 1: Mule: Java Transformer

MULE: JAVA TRANSFORMERENABLES THE DEVELOPER TO PACKAGE CUSTOM JAVA CODE

Page 2: Mule: Java Transformer

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 Transformer

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 Transformer

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 Transformer

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 Transformer

JAVA TRANSFORMER (1/2)• Transforms a message from its original

format to a new, modified format.• If rather than just changing the

message/payload, trigger a more complex set of processes coded in Java, use a Java Component.

Page 7: Mule: Java Transformer

JAVA TRANSFORMER (2/2)To configure the Java Transformer, selecting a class is the only required entry:•Browse for an existing

Java class• Add a new Java class

Page 8: Mule: Java Transformer

BASIC JAVA CLASS• A class must extends:

o org.mule.transformer.AbstractMessageTransformerModifies the message and returns it as the output message of the transformer.

o org.mule.transformer.AbstractTransformerModifies the payload and returns it as the output payload.

• A class must be referenced in a fully-qualified name

Page 9: Mule: Java Transformer

JAVA CLASS: MESSAGE (1/3)package com.mulesoft.learning;

import java.util.Map;import org.mule.api.MuleMessage;import org.mule.api.transformer.TransformerException;import org.mule.transformer.AbstractMessageTransformer;

public class MessageTransformer extends AbstractMessageTransformer { @Override public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException { Map<String, String> params = message.getInboundProperty("http.query.params"); message.setInvocationProperty("myProperty", "Hello, " + params.get("name")); return message; }}

Page 10: Mule: Java Transformer

JAVA CLASS: MESSAGE (2/3)The class has access to Mule Message:• Inbound Properties•Outbound Properties• Payload• Attachments

Page 11: Mule: Java Transformer

JAVA CLASS: MESSAGE (3/3)Run/debug a simple flow• http://localhost:8081/?name=Max• It will produce a variable

myProperty, and its value is: Hello, Max• It will keep the payload as is

Page 12: Mule: Java Transformer

JAVA CLASS: PAYLOAD (1/3)package com.mulesoft.learning;

import org.mule.api.transformer.TransformerException;import org.mule.transformer.AbstractTransformer;

public class PayloadTransformer extends AbstractTransformer {

@Override protected Object doTransform(Object src, String enc) throws TransformerException { return "Hello, " + src; }}

Page 13: Mule: Java Transformer

JAVA CLASS: PAYLOAD (2/3)The class has only access to Payload, therefore:• Add new Byte Array to String Transformer•Update the custom-transformer class to refer to the new

class

Page 14: Mule: Java Transformer

JAVA CLASS: PAYLOAD (3/3)Run/debug the new flow• Post a raw message: Max• Send it to http://localhost:8081• It will produce a new payload: Hello, Max

Page 15: Mule: Java Transformer

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