Executing Dynamic Parallel Tasks using Groovy

Ajanthan Eliyathamby 🇱🇰
2 min readDec 12, 2023

--

[Article Moved from ajanthane.blogspot.com]

Consider a scenario where we need to create tasks for multiple projects and need to execute those projects parallely. To do this dynamically we are populating the projects-names in a file and then using that file data we are creating dynamic tasks and executing them parallely. The below article explains the same using Groovy script. This groovy script will be useful when we are doing the same using Jenkins.

Create the Folder structure as below to execute using gradle:

The file “parallel-project-list” contains the below data:

Test1
Test2
Test3
Test4

The main build.gradle contains the below groovy script.

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath "org.codehaus.gpars:gpars:1.2.1"
}
}

group 'com.test.parallel'
version '1.0-SNAPSHOT'

apply plugin: 'java'

sourceCompatibility = 1.8

import java.util.concurrent.*
import groovyx.gpars.GParsExecutorsPool
import java.time.*

task parallelTests << {

def tasksToRun = [];
File file = new File("parallel-project-list");
def lines = file.readLines();

def countOfProjects = 0;
lines.each { String line ->
println line
tasksToRun << "task$line";

task "task$line" << {
println new Date().format('yyyy-MM-dd-HH:mm:ss') + "Task $line started";
Thread.sleep(5000);
println new Date().format('yyyy-MM-dd-HH:mm:ss') + "Task $line ended";
}
countOfProjects++;
}

println "Total Projects: $countOfProjects";

GParsExecutorsPool.withPool(countOfProjects) {
ExecutorService exService ->
tasksToRun.eachParallel {
taskToRun ->
exService.submit({
tasks[taskToRun].execute()
}
as Runnable)
}
}
}

Once execute you will see that the tasks created dynamically and get executed parallely.

engineer@engineer-VirtualBox:~/gradle-script$ ./gradlew parallelTests
:parallelTests
Test1
Test2
Test3
Test4
Total Projects: 4
2019-04-16-11:55:53Task Test1 started
2019-04-16-11:55:53Task Test2 started
2019-04-16-11:55:53Task Test3 started
2019-04-16-11:55:53Task Test4 started
2019-04-16-11:55:58Task Test2 ended
2019-04-16-11:55:58Task Test4 ended
2019-04-16-11:55:58Task Test1 ended
2019-04-16-11:55:58Task Test3 ended

BUILD SUCCESSFUL

Total time: 6.312 secs
engineer@engineer-VirtualBox:~/gradle-script$

Hope this help’s…

--

--

Ajanthan Eliyathamby 🇱🇰
Ajanthan Eliyathamby 🇱🇰

Written by Ajanthan Eliyathamby 🇱🇰

Associate Architect — Enterprise Integration | 14x WSO2 | 1x HashiCorp | 1× Azure | Runner-Up WCPY 2020 | https://ajanthane.blogspot.com

No responses yet