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