Previous overview
This article is a sequel to "Remotely triggering the Jenkins Pipeline task" . The previous article actually used the Pipeline script to trigger the specified Jenkins task remotely through the Http request, and passed the parameters to the Jenkins task. deal with;
Problems with triggering Jenkins remotely
For the Jenkins service, it is likely that multiple Http requests are received at a certain time, and these requests all want to trigger the same task. In actual use, it is found that Jenkins does not run a task for each request at this time. The next actual combat , We will reproduce and solve this problem;
use Java code to implement multiple concurrent requests
- Use Java code to simulate the situation that multiple Http requests arrive at Jenkins at the same time: write a Java program and send 10 Http requests at a time, all of which remotely trigger the Jenkins task in the previous chapter. The reference source code is as follows:
package com.bolingcavalry;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
public class App {
public static void main( String[] args ) throws Exception {
for(int i=0;i<10;i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("ref", "ref-"+i);
jsonObject.put("repositoryURL","https://github.com/zq2599/jenkinsdemo.git");
jsonObject.put("branch", "master");
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://192.168.133.149:32049/generic-webhook-trigger/invoke?token=token-remote-test");
httpPost.addHeader("Content-Type", "application/json");
httpPost.setEntity(new StringEntity(jsonObject.toJSONString()));
CloseableHttpResponse response = httpClient.execute(httpPost);
response.close();
httpClient.close();
System.out.println("response code : " + response.getStatusLine().getStatusCode() + "\n");
}
}
- Execute the above Java code, the console output is as shown below, the return code is 200, which proves that these 10 requests are all successful:
- I went to the Jenkins webpage and found that only one task was executed, but the ref parameters of the 10 requests were printed, as shown below:
- In 10 requests, Jenkins only performed one task. This result is obviously not what we want. After groping and trying, I finally found a solution to this problem;
Fix concurrency issues
- Open the setting page of the task remote-test , as shown below, check the red box 1 (This project is parameterized), click the red box 2 to add a parameter, and then click the red box 3 to add a string type parameter:
- In the parameter edit box, fill in ref in the Name field , as shown in the red box below. Note that this parameter was set in the parameter of the Generic Webhook Trigger in the previous article . It is a fixed parameter, and the plugin will use:
- Click the Save button at the bottom to save the settings;
- Run the previous Java program again and initiate 10 requests. This time Jenkins created 10 tasks, as shown below:
- Click one of them to view the log, as shown in the figure below, you can see that the parameters are correct and the task execution is successful:
At this point, the concurrency problem of remotely triggering the Jenkins task has been fixed. If you also encounter type problems, I hope this article can give you some reference.