|
@@ -4,64 +4,82 @@ import com.xxl.job.core.biz.model.ReturnT;
|
|
|
import com.xxl.job.core.handler.IJobHandler;
|
|
|
import com.xxl.job.core.handler.annotation.JobHandler;
|
|
|
import com.xxl.job.core.log.XxlJobLogger;
|
|
|
-import org.eclipse.jetty.client.HttpClient;
|
|
|
-import org.eclipse.jetty.client.api.ContentResponse;
|
|
|
-import org.eclipse.jetty.client.api.Request;
|
|
|
-import org.eclipse.jetty.http.HttpMethod;
|
|
|
-import org.eclipse.jetty.http.HttpStatus;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import java.util.concurrent.TimeUnit;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.URL;
|
|
|
|
|
|
/**
|
|
|
* 跨平台Http任务
|
|
|
*
|
|
|
* @author xuxueli 2018-09-16 03:48:34
|
|
|
*/
|
|
|
-@JobHandler(value="httpJobHandler")
|
|
|
+@JobHandler(value = "httpJobHandler")
|
|
|
@Component
|
|
|
public class HttpJobHandler extends IJobHandler {
|
|
|
|
|
|
- @Override
|
|
|
- public ReturnT<String> execute(String param) throws Exception {
|
|
|
+ @Override
|
|
|
+ public ReturnT<String> execute(String param) throws Exception {
|
|
|
|
|
|
- // valid
|
|
|
- if (param==null || param.trim().length()==0) {
|
|
|
- XxlJobLogger.log("URL Empty");
|
|
|
- return FAIL;
|
|
|
- }
|
|
|
+ // request
|
|
|
+ HttpURLConnection connection = null;
|
|
|
+ BufferedReader bufferedReader = null;
|
|
|
+ try {
|
|
|
+ // connection
|
|
|
+ URL realUrl = new URL(param);
|
|
|
+ connection = (HttpURLConnection) realUrl.openConnection();
|
|
|
|
|
|
- // httpclient
|
|
|
- HttpClient httpClient = null;
|
|
|
- try {
|
|
|
- httpClient = new HttpClient();
|
|
|
- httpClient.setFollowRedirects(false); // Configure HttpClient, for example:
|
|
|
- httpClient.start(); // Start HttpClient
|
|
|
+ // connection setting
|
|
|
+ connection.setRequestMethod("GET");
|
|
|
+ connection.setDoOutput(true);
|
|
|
+ connection.setDoInput(true);
|
|
|
+ connection.setUseCaches(false);
|
|
|
+ connection.setReadTimeout(5 * 1000);
|
|
|
+ connection.setConnectTimeout(3 * 1000);
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
|
|
+ connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
|
|
|
|
|
|
- // request
|
|
|
- Request request = httpClient.newRequest(param);
|
|
|
- request.method(HttpMethod.GET);
|
|
|
- request.timeout(5000, TimeUnit.MILLISECONDS);
|
|
|
+ // do connection
|
|
|
+ connection.connect();
|
|
|
|
|
|
- // invoke
|
|
|
- ContentResponse response = request.send();
|
|
|
- if (response.getStatus() != HttpStatus.OK_200) {
|
|
|
- XxlJobLogger.log("Http StatusCode({}) Invalid.", response.getStatus());
|
|
|
- return FAIL;
|
|
|
- }
|
|
|
+ //Map<String, List<String>> map = connection.getHeaderFields();
|
|
|
|
|
|
- String responseMsg = response.getContentAsString();
|
|
|
- XxlJobLogger.log(responseMsg);
|
|
|
- return SUCCESS;
|
|
|
- } catch (Exception e) {
|
|
|
- XxlJobLogger.log(e);
|
|
|
- return FAIL;
|
|
|
- } finally {
|
|
|
- if (httpClient != null) {
|
|
|
- httpClient.stop();
|
|
|
- }
|
|
|
- }
|
|
|
+ // valid StatusCode
|
|
|
+ int statusCode = connection.getResponseCode();
|
|
|
+ if (statusCode != 200) {
|
|
|
+ throw new RuntimeException("Http Request StatusCode(" + statusCode + ") Invalid.");
|
|
|
+ }
|
|
|
|
|
|
- }
|
|
|
+ // result
|
|
|
+ bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ String responseMsg = result.toString();
|
|
|
+
|
|
|
+ XxlJobLogger.log(responseMsg);
|
|
|
+ return SUCCESS;
|
|
|
+ } catch (Exception e) {
|
|
|
+ XxlJobLogger.log(e);
|
|
|
+ return FAIL;
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (bufferedReader != null) {
|
|
|
+ bufferedReader.close();
|
|
|
+ }
|
|
|
+ if (connection != null) {
|
|
|
+ connection.disconnect();
|
|
|
+ }
|
|
|
+ } catch (Exception e2) {
|
|
|
+ XxlJobLogger.log(e2);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|