Baoqi commented on a change in pull request #842: [FEATURE][#841]Add HTTP 
components(添加HTTP组件)
URL: 
https://github.com/apache/incubator-dolphinscheduler/pull/842#discussion_r327447257
 
 

 ##########
 File path: 
escheduler-server/src/main/java/cn/escheduler/server/worker/task/http/HttpTask.java
 ##########
 @@ -0,0 +1,269 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package cn.escheduler.server.worker.task.http;
+
+
+import cn.escheduler.common.enums.HttpMethod;
+import cn.escheduler.common.enums.HttpParametersType;
+import cn.escheduler.common.process.HttpProperty;
+import cn.escheduler.common.process.Property;
+import cn.escheduler.common.task.AbstractParameters;
+import cn.escheduler.common.task.http.HttpParameters;
+import cn.escheduler.common.utils.Bytes;
+import cn.escheduler.common.utils.DateUtils;
+import cn.escheduler.common.utils.ParameterUtils;
+import cn.escheduler.dao.DaoFactory;
+import cn.escheduler.dao.ProcessDao;
+import cn.escheduler.dao.model.ProcessInstance;
+import cn.escheduler.server.utils.ParamUtils;
+import cn.escheduler.server.worker.task.AbstractTask;
+import cn.escheduler.server.worker.task.TaskProps;
+import com.alibaba.fastjson.JSONObject;
+import org.apache.commons.io.Charsets;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.HttpEntity;
+import org.apache.http.ParseException;
+import org.apache.http.client.config.RequestConfig;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.apache.http.client.methods.RequestBuilder;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.util.EntityUtils;
+import org.slf4j.Logger;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * http task
+ */
+public class HttpTask extends AbstractTask {
+
+    private HttpParameters httpParameters;
+
+    /**
+     *  process database access
+     */
+    private ProcessDao processDao;
+
+    /**
+     * Convert mill seconds to second unit
+     */
+    protected static final int MAX_CONNECTION_MILLISECONDS = 60000;
+
+    protected static final String APPLICATION_JSON = "application/json";
+
+    protected String output;
+
+
+    public HttpTask(TaskProps props, Logger logger) {
+        super(props, logger);
+        this.processDao = DaoFactory.getDaoInstance(ProcessDao.class);
+    }
+
+    @Override
+    public void init() {
+        logger.info("http task params {}", taskProps.getTaskParams());
+        this.httpParameters = 
JSONObject.parseObject(taskProps.getTaskParams(), HttpParameters.class);
+
+        if (!httpParameters.checkParameters()) {
+            throw new RuntimeException("http task params is not valid");
+        }
+    }
+
+    @Override
+    public void handle() throws Exception {
+        String threadLoggerInfoName = String.format("TaskLogInfo-%s", 
taskProps.getTaskAppId());
+        Thread.currentThread().setName(threadLoggerInfoName);
+
+        long startTime = System.currentTimeMillis();
+        String statusCode = null;
+        String body = null;
+        boolean status = true;
+        try {
+            CloseableHttpResponse response = sendRequest();
+            statusCode = String.valueOf(getStatusCode(response));
+            body = getResponseBody(response);
+            response.close();
+            exitStatusCode = validResponse(body, statusCode);
+            long costTime = System.currentTimeMillis() - startTime;
+            status = StringUtils.isEmpty(output);
+            logger.info("startTime: {}, httpUrl: {}, httpMethod: {}, status : 
{}, costTime : {}Millisecond, statusCode : {}, body : {}, log : {}",
+                    DateUtils.format2Readable(startTime), 
httpParameters.getUrl(),httpParameters.getHttpMethod(), status, costTime, 
statusCode, body, output);
+        } catch (Exception e) {
+            appendMessage(e.toString());
+            exitStatusCode = -1;
+            logger.error("httpUrl[" + httpParameters.getUrl() + "] connection 
failed:"+output, e);
+        }
+    }
+
+    protected CloseableHttpResponse sendRequest() throws IOException {
+        RequestBuilder builder = createRequestBuilder();
+        ProcessInstance processInstance = 
processDao.findProcessInstanceByTaskId(taskProps.getTaskInstId());
+
+        Map<String, Property> paramsMap = 
ParamUtils.convert(taskProps.getUserDefParamsMap(),
+                taskProps.getDefinedParams(),
+                httpParameters.getLocalParametersMap(),
+                processInstance.getCmdTypeIfComplement(),
+                processInstance.getScheduleTime());
+        List<HttpProperty> httpPropertyList = new ArrayList<>();
+        if(httpParameters.getHttpParams() != null && 
httpParameters.getHttpParams().size() > 0){
+            for (HttpProperty httpProperty: httpParameters.getHttpParams()) {
+                String jsonObject = JSONObject.toJSONString(httpProperty);
+                String params = 
ParameterUtils.convertParameterPlaceholders(jsonObject,ParamUtils.convert(paramsMap));
+                logger.info("http request params:{}",params);
+                
httpPropertyList.add(JSONObject.parseObject(params,HttpProperty.class));
+            }
+        }
+        addRequestParams(builder,httpPropertyList);
+        HttpUriRequest request = 
builder.setUri(httpParameters.getUrl()).build();
+        setHeaders(request,httpPropertyList);
+        CloseableHttpClient client = createHttpClient();
 
 Review comment:
   Can this "CloseableHttpClient client = createHttpClient();" be closed? For 
example, move this output to handle() and don't forget to close

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to