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

Creating a Simple Synchronous File Reader

DZone's Guide to

Creating a Simple Synchronous File Reader

Learn how to create a quick and easy Java component that will allow you to read your files in a Mule flow.

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

Hello, everyone!

Here I will show you create a very simple synchronous file reader inside your Mule flow.

You just need to add these lines of code in your Java component class, which will enable you to read a file in between the flow.

I hope this helps!

package org.rahul.util;

import java.io.File;

import org.mule.api.MuleEventContext;
import org.mule.api.lifecycle.Callable;

public class SynchronousFileReader implements Callable {

    public File getFileContent(String fileLocation)
    {
        File file = new File(fileLocation);
        return file;
    }

    @Override
    public Object onCall(MuleEventContext eventContext) throws Exception {

        String filepath = eventContext.getMessage().getInvocationProperty("filepath");
        File file = getFileContent(filepath);
        return file;
    }
}


Sample usage:

<component class="org.rahul.util.SynchronousFileReader" doc:name="Java"/>
        <!-- File as Binary -->
        <file:file-to-byte-array-transformer doc:name="File to Byte Array" mimeType="binary/octet-stream"/>


Use transformers like File-to-String or File-to-Byte-Array as you require after using this Java component.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
mulesoft ,mule esb ,component ,java ,custom component

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}