For BE/B.Tech/BCA/MCA/ME/M.Tech Major/Minor Project for CS/IT branch at minimum price Text Message @ 9424820157

Java Transformer in Mule | Call Java Class in Mule

  Java Transformer in Mule | Call Java Class in Mule






Java Transformer delegates to a java class. We can call java class from it and can execute our own logic.


When Mule does not provide an out-of-the-box transformer to meet your application integration needs, the Java transformer enables you to package custom-coded Java logic (that is, a Java class) that is triggered when the transformer processes the message. In most cases, this Mule processor transforms a message from its original format to a new, modified format.


To configure a Java transformer, you need to specify the following:


1. The Java class your transformer calls.
2. An encoding format the transformer applies to messages.
3. A MIME type for your data.


The Java transformer applies these configuration settings to each message, then sends the transformed message to the next processor in your flow. Additionally, if you need to execute logic on a particular property within your Java class, the Java transformer allows you to configure Spring beans, which are containers that include developer-defined logic that executes on a specific property within your Java class. The modified Spring data can be saved in persistent storage, then recalled later in the flow. If you choose to add a property to a class, it is possible to inject additional values into the Spring property by configuring the property sub-elements. This allows you to reference arrays, maps, beans, etc. within the defined bean factory (class) or an external bean factory.


In general, MuleSoft recommends that you use a Java transformer if your transformation logic is complex or if message transformation requires the use of multiple out-of-the-box transformers. For example, when you need to create an application that returns all acceptable payment methods a customer can apply when an order is received, you can use a custom Java transformer to host a script that executes the application and returns the information to the client


flow of java transformer :





Java class is present in src/main/java named as Demo.java


package testjava;


import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;

public class Demo extends AbstractMessageTransformer {



 @Override

 public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {

  return "Hello World";

 }


}



URL : http://localhost:8085/api/java

Method : GET

Output :





XML project code :



<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8085" basePath="/api" doc:name="HTTP Listener Configuration"/>
 
    <flow name="test_javatransformerFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/java" doc:name="HTTP"/>
        <custom-transformer class="testjava.Demo" doc:name="Java"/>
        <set-payload value="#[payload]" doc:name="Set Payload"/>
    </flow>
</mule>

No comments:

Post a Comment



Please go through below tutorials:


Mule 4 Tutorials

DEPLOY TO CLOUDHUB C4E CLIENT ID ENFORCEMENT CUSTOM POLICY RABBIT MQ INTEGRATION
XML TO JSON WEBSERVICE CONSUMER VM CONNECTOR VALIDATION UNTIL SUCCESSFUL
SUB FLOW SET & REMOVE VARIABLE TRANSACTION ID SCATTER GATHER ROUND ROBIN
CONSUME REST WEBSERVICE CRUD OPERATIONS PARSE TEMPLATE OBJECT TO JSON LOAD STATIC RESOURCE
JSON TO XML INVOKE IDEMPOTENT FILTER FOR EACH FLAT TO JSON
FIXWIDTH TO JSON FIRST SUCCESSFUL FILE OPERATIONS EXECUTE ERROR HANDLING
EMAIL FUNCTIONALITY DYNAMIC EVALUATE CUSTOM BUSINESS EVENT CSV TO JSON COPYBOOK TO JSON
CHOICE ASYNC

Widely used Connectors in Mule 3

CMIS JETTY VM CONNECTOR SALESFORCE POP3
JMS TCP/IP WEBSERVICE CONSUMER QUARTZ MONGO DB
FILE CONNECTOR DATABASE CONNECTOR


Widely used Scopes in Mule 3

SUB FLOW REQUEST REPLY PROCESSOR CHAIN FOR EACH CACHE
ASYNC TCP/IP COMPOSITE SOURCE POLL UNTIL SUCCESSFUL
TRANSACTIONAL FLOW

Widely used Components in Mule 3

EXPRESSION CXF SCRIPT RUBY PYTHON
JAVASCRIPT JAVA INVOKE CUSTOM BUSINESS EVENT GROOVY
ECHO LOGGER


Widely used Transformers in Mule 3

MONGO DB XSLT TRANSFORMER REFERENCE SCRIPT RUBY
PYTHON MESSAGE PROPERTIES JAVA TRANSFORMER GZIP COMPRESS/UNCOMPRESS GROOVY
EXPRESSION DOM TO XML STRING VALIDATION COMBINE COLLECTIONS BYTE ARRAY TO STRING
ATTACHMENT TRANSFORMER FILE TO STRING XML TO DOM APPEND STRING JAVASCRIPT
JSON TO JAVA COPYBOOK TO JSON MAP TO JSON JSON TO XML FLATFILE TO JSON
FIXWIDTH TO JSON CSV TO JSON


Widely used Filters in Mule 3

WILDCARD SCHEMA VALIDATION REGEX PAYLOAD OR
NOT MESSAGE PROPERTY MESSAGE IDEMPOTENT FILTER REFERNCE
EXPRESSION EXCEPTION CUSTOM AND


Exception Strategy in Mule 3

REFERENCE EXCEPTION STRATEGY CUSTOM EXCEPTION STRATEGY CHOICE EXCEPTION STRATEGY CATCH EXCEPTION STRATEGY GLOBAL EXCEPTION STRATEGY


Flow Control in Mule 3

CHOICE COLLECTION AGGREGATOR COLLECTION SPLITTER CUSTOM AGGREGATOR FIRST SUCCESSFUL
MESSAGE CHUNK AGGREGATOR MESSAGE CHUNK SPLITTER RESEQUENCER ROUND ROBIN SOAP ROUTER