Author: Abhishek Bathwal
This blog will help you set up your timer scope so you can calculate the time taken by a Mule process to execute.
Code for Scope:
- Open the command prompt and execute the following command in the folder where you intend to create the project.
mvn org.mule.extensions:mule-extensions-archetype-maven-plugin:generate

- Upon executing the command, provide the requested basic information.
Name of the extension - << as per requirement>> (e.g custom-timerscope)
Extension’s groupId - << Org group Id>>
Extension’s artifactId - << as per requirement>> (e.g custom-timerscope)
Extension’s version - 1.0.0
Extension’s main package- org.mule.extension
- Once the basic information is provided, the project will be created in the respective folder.

- Import the project into Anypoint Studio.

The project name can be renamed as per requirement.
Once the project is imported, the folder structure and classes will become visible.

- The initial steps are similar to creating a custom connector. However, unlike a custom connector, not all Java files are necessary. Only two files are required (CustomtimerscopeConfiguration.java, CustomtimerscopeOperations.java). Additionally, the files in src/test/java and src/test/resources are not needed, so we will proceed to remove those unnecessary files.

- Replace the existing code in the above-mentioned files with the provided code below.
CustomtimerscopeOperations.java
package org.mule.extension.internal;
import org.mule.runtime.extension.api.annotation.param.Optional;
import org.mule.runtime.extension.api.runtime.process.CompletionCallback;
import org.mule.runtime .extension.api.runtime.route.Chain;
import org.slf4j.*;
public class CustomtimerscopeOperations {
private final org.slf4j.Logger classLogger = LoggerFactory.getLogger(CustomtimerscopeOperations.class);
public void timerScope(Chain operations,
CompletionCallback<Object, Object> callback,
@Optional(defaultValue = "#[flow.name]") String Message) { //adding parameter and default value to it
long startTime = System.currentTimeMillis(); //Define variable to lock the start time
operations.process(result -> {
long elapsedMilliseconds = System.currentTimeMillis() - startTime; //logging message to show the time takes to process for success scenario
classLogger.info(Message + " Completed in " + elapsedMilliseconds + "ms"); //logging message to show the time takes to process for success scenario
callback.success(result);
}, (error, previous) -> {
long elapsedMilliseconds = System.currentTimeMillis() - startTime; //calculate the total time taken for the process to complete
classLogger.info(Message + " Completed with errors in " + elapsedMilliseconds + "ms"); //logging message to show the time takes to process for failure scenario
callback.error(error);
});
}
}
CustomtimerscopeConfiguration.java
package org.mule.extension.internal;
import org.mule.runtime.extension.api.annotation.Operations;
import org.mule.runtime.extension.api.annotation.Extension;
@Operations(CustomtimerscopeOperations.class)
@Extension(name = "TimerScope")
public class CustomtimerscopeConfiguration {}
Note: Marked the comments in BOLD.
With the code development completed, we will now proceed to the next step, which involves testing the scope locally.
Test the Scope locally:
- Open the command prompt at the project location and execute the following command. This will generate the dependency JAR in the .m2 repository folder.
mvn clean install –DskipTests

- Create a new Mule project in Anypoint Studio and include the dependency in the POM file.
<dependency>
<groupId><<same as the time of creation>></groupId>
<artifactId><<same as the time of creation>></artifactId>
<version>1.0.0</version>
<classifier>mule-plugin</classifier>
</dependency>
Once the dependency is added, the scope will become visible in the palette.


Note: The “Message” parameter can be modified according to your needs.
- Let’s create a flow to test both the success and failure scenarios.

Success:

Failure:

Since the scope has been tested locally, let’s proceed to publish it to Exchange.
Publish to Exchange:
- Add below distributionManagement and tags in the POM.xml of the Timer Scope Project.
<distributionManagement>
<repository>
<id>anypoint-exchange</id>
<name>Exchange Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v2/organizations/<<OrgId>>/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>
<repositories>
<repository>
<id>anypoint-exchange-v2</id>
<name>Anypoint Exchange V2</name>
<url>https://maven.anypoint.mulesoft.com/api/v2/maven</url>
<layout>default</layout>
</repository>
</repositories>
- Put the Settings.xml file in the .m2 folder.
Using username and password:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>anypoint-exchange</id>
<username><<username>></username>
<password><<password>></password>
</server>
</servers>
</settings>
Using connectedApp credentials:
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<servers>
<server>
<id>anypoint-exchange</id>
<username>~~~Client~~~</username>
<password><<client_Id>>~?~<<client_secret>></password>
</server>
</servers>
</settings>
- Once again, open the project directory in the command prompt and execute the following command.
mvn clean deploy –DskipTests

- Once it’s published on Exchange, it will become visible, and the same can be downloaded in Studio from the Exchange.


Note: We can further enhance its functionality according to specific requirements and customize it to create the desired scope.
References:
- https://docs.mulesoft.com/mule-sdk/1.1/scopes
- https://blogs.mulesoft.com/dev-guides/api-connectors-templates/custom-connector-mule-sdk/
Happy Learning!