0

Currently we have a C# window service application to extract data from JIRA using REST API in a timely manner. In that application we have a method to extract the data from JIRA by passing the project name as input to the method as well as it will return a boolean flag to indicate successful extraction of the project.

As of now we have extracted only 10 projects data from JIRA in a synchronous manner by calling the method in a for loop of project list.

New requirement needs to be extract 'N' projects from JIRA? If we follow the synchronous manner we need to wait for long hours to complete. But by calling the method in a asynchronous manner we can reduce some hours.

'N' will increase each time period.

I need a sample logic to apply the same.

1 Answers1

0

You may take advantage of the async functionality in the C# language. This way you don't have to spawn any threads, but the calls to the JIRA api can be executed at the same time, by not awaiting the calls to JIRA straight away. Example:

public async Task FetchProjects(params string[] projects)
{
    var tasks = new List<Task<bool>>();
    foreach (var project in projects)
    {
        // Calling the api, but not awaiting the call just yet.
        tasks.Add(FetchProjectInfo(project));
    }

    // awaiting all api calls here.
    await Task.WhenAll(tasks);

    // You might want to check whether all calls returned true here.
}

// This would be your existing method.
private async Task<bool> FetchProjectInfo(string project)
{
    string url = CreateUrl(project);

    // TODO: Include error handling 
    // (and you should actually reuse the HttpClient between calls)
    using (var client = new HttpClient())
    using (var response = await client.GetAsync(url))
    {
        // TODO: Do something with response.
    }
}
Jesse de Wit
  • 3,867
  • 1
  • 20
  • 41