MuleSoft

Scheduler and Cron Expression in MuleSoft

Introduction

  • This blog will contain the steps that are necessary to implement the basic auth and threat protection policy on top of API.
  • For demonstrating this I’ll be going with a use case where I’ll be creating records into the Salesforce custom object and how we restrict the access for API using basic auth and prevent the API from the attacks by making use of threat protection policies.

 Pre-requisites

  • We need to have an active Anypoint platform account as well as Anypoint Studio should be setup.
  • We need to have MySQL workbench installed.
  • Create a table with some fields, as per the requirement. For this instance, I have created 5 fields having datatypes as VARCHAR for all the fields.
  • We need to have a Salesforce developer account.
  • Create a Salesforce custom object with some fields, as per the requirement.
  • Click on Create in the home section, which is on the right-hand side. Then click on a custom object, then fill in the name of the custom object. Then click on fields and relationship, then select the fields with the respective type of information.
  • In this scenario, we’ll be having 5 fields i.e. StoreId   c, StoreName   c, StoreLoc   c, StoreEmail    c and StorePhone__c, where all the fields are having Text as the type of information stored in it.
  • We need to keep the security token handy for that, click on profile and then click on setting which is on the right-hand top corner of the screen type security token in the quick find section, and click on reset security token.
  • The security token will be sent to the registered mail ID.

 Scheduler and Cron Expression

  • The scheduler is one of the components in a core module that acts as an event source in a mule flow. It triggers the execution at specific intervals.
  • There are two ways through which we can configure the scheduler:
    • Fixed Frequency: It specifies the interval at which the flow will triggered.
    • Cron Expression: It specifies the interval at a lower level by specifying the date, month, year, and day of the week.
*******
1st2nd3rd4th5th6th7th
  • 1st place is for seconds which can range between 0-59.
  • 2nd place is for minutes which can range between 0-59.
  • 3rd place is for hours which can range between 0-23.
  • 4th place is for a day of the month which ranges between 1-31.
  • 5th place is for a month which can range between 1-12 or JAN-DEC.
  • 6th place is for a day of the week which can range between 1-7 or SUN-SAT.
  • 7th place is for year which can be either 0 or 4-digit value which should be between 1970-2099.

  • It also accepts some special characters which are mentioned below:

    • * => If it is there at any place, it means that it will take all the values.
    • ? => If it is there at any place, it means that it does not have any specific value for that place.
    • – => It specifies the range. For example: 1-3, JAN-MAR, MON-WED.
    • , => It specifies the additional values. For example: (10,13,17).
    • / => It specifies the incremental values. For example: (1/3)
    • L=> It specifies the last day of the month or week. For example, 3L (last Wednesday of the month).
    • # => It specifies the nth day of the month. For example #3(third day of the month).

  • Let’s take some set of examples to understand the cron expression.

    • 0 15 10 * * ? 2021
      Here, for second place is zero, for minute’s place is 15, for hours place is 10, for day of the month and month we have *, for day of the week we have? and year’s place is 2021. This implies that the scheduler will run every day at 10:15 AM in the year 2021.

    • 0 0/5 14 * * ?
      Here, the second place is zero, for the minute place is 0/5 which means every 5 minutes, for the hour’s place is 14 which means 2 PM, for a day of the month and month we have *, and for a day of the week we have ?. This implies that the scheduler will run every 5 minutes, starting at 14:00 or 02:00 PM ending a 14:55 or 02:55 PM, every day.

    • 1 1 1 1,7 * ?
      Here, for second’s place is 1, for minute’s place, is 1, for hours the place is 1, for a day of the month we have (1,7) which means every 1st and 7th of the month, for a month we have * and for a day of the week we have ?. This implies that the scheduler will run on the first second of the first minute of the first hour on the first and seventh day of every month.

  • There are some scenarios where we need to make use of Scheduler. Some of the scenarios are:
    • Data synchronization: Pulling data from one system and pushing it to another system.
    • Report Generation: Generating reports for a specific period.

  • There are some benefits of the scheduler process:
    • It ensures that the task is performed at the right time without any delay.
    • It reduces the need for manual intervention for repetitive tasks.

  • In this blog, we’ll learn about schedulers. For this instance, we’ll work on a usecase which is data sync between the Salesforce custom object and local DB, which is scheduled to run once and only process the unprocessed records.

Steps:

  1. First, we’ll place a scheduler and configure the scheduling strategy as Fixed frequency where frequency is one, start delay is zero, and time unit is days.

If we want to achieve the same thing using cron expression, then we need to specify time as in, at what time the scheduler should trigger. For this instance, we’ll consider it as midnight 12:00.

2. Next, we’ll add two set of loggers that depicts the start and end of flow.

3. Next, we’ll add a retrieve component from the object store module and configure it.

4. Next, we’ll name the key and add a default value at some time.

5. Next, we’ll add a Salesforce query component and configure it to get the records from the Salesforce custom object.

6. Next, we’ll add the query the salesforce custom object on the basis of LastModifiedDate and parameter as the LastModifiedDate which will be retrieved from the object store and the response of query component will be stored into the target variable(records).

7. Then we’ll place a choice router to check whether the size of vars.records is greater than zero.

8. Next, we’ll place a logger in the default condition log the message as “No new record” and in the when condition we’ll place a bulk insert component.

9. Then we’ll configure the bulk insert and configure the connector configuration. Next, we’ll add the insert bulk query and the parameters.

10. Next, we’ll making use of manual watermarking concept for that we’ll place a store component of object store module to store the maximum value of LastModifiedDate.

11. The application is ready. Now we need to run the application and test it.

12. We’ll verify using Salesforce workbench and DB.

Author

Aniket Pal

Leave a comment

Your email address will not be published. Required fields are marked *