Create Custom Timer Scope With Mule SDK

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:
  1. 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
  1. 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
  1. Once the basic information is provided, the project will be created in the respective folder.
  1. 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.

  1. 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 (, Additionally, the files in src/test/java and src/test/resources are not needed, so we will proceed to remove those unnecessary files.
  1. Replace the existing code in the above-mentioned files with the provided code below.

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 = "#[]") 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 + " Completed in " + elapsedMilliseconds + "ms"); //logging message to show the time takes to process for success scenario

		}, (error, previous) -> {
			long elapsedMilliseconds = System.currentTimeMillis() - startTime; //calculate the total time taken for the process to complete + " Completed with errors in " + elapsedMilliseconds + "ms"); //logging message to show the time takes to process for failure scenario

package org.mule.extension.internal;

import org.mule.runtime.extension.api.annotation.Operations;
import org.mule.runtime.extension.api.annotation.Extension;

@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:
  1. 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
  1. Create a new Mule project in Anypoint Studio and include the dependency in the POM file.

	<groupId><<same as the time of creation>></groupId>
	<artifactId><<same as the time of creation>></artifactId>

Once the dependency is added, the scope will become visible in the palette.

Note: The “Message” parameter can be modified according to your needs.

  1. Let’s create a flow to test both the success and failure scenarios.



Since the scope has been tested locally, let’s proceed to publish it to Exchange.

Publish to Exchange:
  1. Add below distributionManagement and tags in the POM.xml of the Timer Scope Project.

	<name>Exchange Repository</name>
		<name>Anypoint Exchange V2</name>
  1. Put the Settings.xml file in the .m2 folder.

Using username and password:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="" xmlns:xsi="" xsi:schemaLocation="">

Using connectedApp credentials:

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  1. Once again, open the project directory in the command prompt and execute the following command.
mvn clean deploy –DskipTests
  1. 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.


Happy Learning!

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.