Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Mule Dismantle, Component Mule Example

DZone's Guide to

Mule Dismantle, Component Mule Example

· Integration Zone ·
Free Resource

The Future of Enterprise Integration: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

The component is program that has some part of logic which we can use in mule flow. We can use programming language like java, Expression language, java script, python etc., for a component.

Create a mule flow, add http end point to it, with the below configuration

Host: localhost

Port: 8082(port should be unique in the flows)

Create a java class inside src/main/java folder of the mule project

public class PrintPayload {
     public void method1(String args) {
         System.out.println("String as args");
         System.out.println(args);
     }
     public void method2(byte[] values) {
         System.out.println("byte as args");
         System.out.println(values);
     }
}

In the flow add a java component, refer the PrintPayload class in the class name: property of the java component.

Add string to bytearray transformer

Again add the java component, refer the PrintPayload class in the class name property.

The flow will look like below

And the flow will have the below configuration.

<?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" version="EE-3.5.0" 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">
      <flow name="ComponentFlow1" doc:name="ComponentFlow1">
          <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8082" doc:name="HTTP"/>
          <component class="com.classes.PrintPayload" doc:name="Java"/>
          <string-to-byte-array-transformer doc:name="String to Byte Array"/>
          <component class="com.classes.PrintPayload" doc:name="Java"/>
      </flow>
</mule>

Run the application.

In the browser hit the url:

http://localhost:8082/?name=kasi

you will get the following output:

String as args

/?name=kasi

byte as args

[B@23a1f067

Mule calls the appropriate method based on payload type.

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}