远程触发Jenkins的Pipeline任务的并发问题处理

2020-10-12  本文已影响0人  程序员欣宸

前文概述

本文是《远程触发Jenkins的pipeline任务》的续篇,上一篇文章实战了如何通过Http请求远程触发指定的Jenkins任务,并且将参数传递给Jenkins任务去使用,文末提到了有个并发问题待处理,这就是本文的内容:处理上一篇文章提到的遗留问题。

远程触发Jenkins的问题

对Jenkins服务来说,很有可能在某一时刻同时收到多个Http请求,并且这些请求都想触发同一个任务,在实际使用中发现此时Jenkins并不对每个请求都运行一次任务,接下来的实战,我们就来重现并解决此问题;

用Java代码实现多个并发请求

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");
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读