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

How to Use Poll Reference, For Each, and Properties Files in Mule

DZone's Guide to

How to Use Poll Reference, For Each, and Properties Files in Mule

Are you on a quest to become a Mule master? Read on to learn how to use these three different techniques while working with this popular platform.

· Integration Zone ·
Free Resource

SnapLogic is the leading self-service enterprise-grade integration platform. Download the 2018 GartnerMagic Quadrant for Enterprise iPaaS or play around on the platform, risk free, for 30 days.

Hi all, today, in this tutorial, we'll look at how to use the following Mule functionalities: Poll Concept, For Each Scope and Properties Files. Before going into the use case, let's go over Poll References and For Each Scopes.

Poll Reference

According to the MuleSoft documentation, "In Mule, some connectors like HTTP and FTP, use a polling process to actively retrieve messages from an external resource, most message processors in Mule are triggered when called by a previous element in a flow." If you want to arrange for a message processor to actively call a resource at regular intervals, use a Poll scope.

Image title


For Each Scope

According to MuleSoft's docs, "The For Each scope splits a collection into elements and processes them iteratively through the processors embedded in the scope."

Image title

Here in this tutorial, I have considered a simple use case where multiple records from a database are fetched every minute and stored individually in a Target File Location. Here, we have configured a properties file with all the database credentials and target file location. Here is the complete flow:

Image title

  • Create a new project and go to src/main/resources and create a new file with .properties extension. Here, I have created a details.properties file as shown below:

Image title

  • Now, enter all the properties that need to be used for database configuration, like Host, Port, Instance, user and Password, and Target File Directory Location, as shown below:

Image title

  • Go to the Global Elements Tab in the Design Canvas and add a property placeholder and configure it with the created .properties file as shown below:

Image title

  • Once you drag and drop the database connector from the Mule palette on to the design canvas, Poll Reference is automatically specified and you can configure the polling frequency by using a fixed frequency scheduler, or Cron Scheduler, which uses specific Cron Expressions. You can also use the Watermark Concept. Please Configure it as shown; for this example, I have used a fixed frequency schedule that fires every minute, as shown below:

Image title

  • Click on Add Configuration and select the database type and configure it. As we have already created the properties file, we can use those parameters directly here, as shown below:

Image title

  • Don’t forget to add the dependency file in the required Dependencies field, and click on Test Connection. This opens a new window stating that the test connection is successful, as shown below:

Image title

  • Since we all know that the database payload type is List, drag and drop the For Each Scope from the Mule Palette in the Process Side.

  • Drag and drop the object to the JSON Transformer from the Mule Palette inside the For Each Scope.

  • Drag and Drop a Session variable and configure it with a FileName which contains the value #[server.dateTime.format('yyyy-MM-dd-hh-mm-ss-SSS')].json, as shown below:

Image title

  • Last but not the least, drag and Drop the file connector inside the For Each and configure it as shown below:

Image title

  • Now, right click on Design Canvas and click on Run Project. After the project gets deployed, the application automatically fetches the records every minute and stores them in a particular location, as shown below:

Image title

Here is the complete flow's source code:

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

<mule xmlns:file="http://www.mulesoft.org/schema/mule/file" xmlns:db="http://www.mulesoft.org/schema/mule/db" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:context="http://www.springframework.org/schema/context" 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/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-current.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd
http://www.mulesoft.org/schema/mule/file http://www.mulesoft.org/schema/mule/file/current/mule-file.xsd">
    <context:property-placeholder location="details.properties"/>
    <db:oracle-config name="Oracle_Configuration" host="${Host}" port="${Port}" instance="${Instance}" user="${User}" password="${Password}" doc:name="Oracle Configuration"/>
    <flow name="poll_foreach_propertiesFlow">
        <poll doc:name="Poll">
            <fixed-frequency-scheduler frequency="1" timeUnit="MINUTES"/>
            <db:select config-ref="Oracle_Configuration" doc:name="Database">
                <db:parameterized-query><![CDATA[select * from EMPLOYEE]]></db:parameterized-query>
            </db:select>
        </poll>
        <foreach doc:name="For Each">
            <json:object-to-json-transformer doc:name="Object to JSON"/>
            <set-session-variable variableName="FileName" value="#[server.dateTime.format('yyyy-MM-dd-hh-mm-ss-SSS')].json" doc:name="Session Variable"/>
            <file:outbound-endpoint path="${TargetDirectory}" outputPattern=" #[sessionVars.FileName]" responseTimeout="10000" doc:name="File"/>
        </foreach>
    </flow>
</mule>

Hope this helps.

As always, feel free to leave any feedback in the comments section.

Thank you.

With SnapLogic’s integration platform you can save millions of dollars, increase integrator productivity by 5X, and reduce integration time to value by 90%. Sign up for our risk-free 30-day trial!

Topics:
anypoint connector ,poll ,for loops ,mulesoft ,integration

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}