This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob.git
The following commit(s) were added to refs/heads/master by this push:
new 1bdc817c6 Refactor HttpParam (#2354)
1bdc817c6 is described below
commit 1bdc817c6a81cd0e0ffffc51aec25ec8d7e1f212
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Oct 31 19:08:40 2023 +0800
Refactor HttpParam (#2354)
---
.../elasticjob/http/executor/HttpJobExecutor.java | 71 +++++++----------
.../elasticjob/http/pojo/HttpParam.java | 36 ++++++++-
.../http/executor/HttpJobExecutorTest.java | 22 +----
.../elasticjob/http/pojo/HttpParamTest.java | 93 ++++++++++++++++++++++
4 files changed, 156 insertions(+), 66 deletions(-)
diff --git
a/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
b/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
index 9d913920c..19d633c40 100644
---
a/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
+++
b/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutor.java
@@ -21,14 +21,13 @@ import com.google.common.base.Strings;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
-import org.apache.shardingsphere.elasticjob.spi.executor.param.ShardingContext;
-import
org.apache.shardingsphere.elasticjob.spi.executor.param.JobRuntimeService;
-import
org.apache.shardingsphere.elasticjob.spi.executor.type.TypedJobItemExecutor;
import org.apache.shardingsphere.elasticjob.http.pojo.HttpParam;
import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
-import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobConfigurationException;
import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobExecutionException;
import org.apache.shardingsphere.elasticjob.kernel.infra.json.GsonFactory;
+import
org.apache.shardingsphere.elasticjob.spi.executor.param.JobRuntimeService;
+import org.apache.shardingsphere.elasticjob.spi.executor.param.ShardingContext;
+import
org.apache.shardingsphere.elasticjob.spi.executor.type.TypedJobItemExecutor;
import java.io.BufferedReader;
import java.io.IOException;
@@ -38,8 +37,6 @@ import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
-import java.util.Arrays;
-import java.util.Properties;
/**
* Http job executor.
@@ -49,42 +46,28 @@ public final class HttpJobExecutor implements
TypedJobItemExecutor {
@Override
public void process(final ElasticJob elasticJob, final JobConfiguration
jobConfig, final JobRuntimeService jobRuntimeService, final ShardingContext
shardingContext) {
- HttpParam httpParam = getHttpParam(jobConfig.getProps());
+ HttpParam httpParam = new HttpParam(jobConfig.getProps());
HttpURLConnection connection = null;
try {
- URL url = new URL(httpParam.getUrl());
- connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod(httpParam.getMethod());
- connection.setDoOutput(true);
- connection.setConnectTimeout(httpParam.getConnectTimeout());
- connection.setReadTimeout(httpParam.getReadTimeout());
- if (!Strings.isNullOrEmpty(httpParam.getContentType())) {
- connection.setRequestProperty("Content-Type",
httpParam.getContentType());
- }
-
connection.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY,
GsonFactory.getGson().toJson(shardingContext));
+ connection = getHttpURLConnection(httpParam, shardingContext);
connection.connect();
String data = httpParam.getData();
- if (isWriteMethod(httpParam.getMethod()) &&
!Strings.isNullOrEmpty(data)) {
+ if (httpParam.isWriteMethod() && !Strings.isNullOrEmpty(data)) {
try (OutputStream outputStream = connection.getOutputStream())
{
outputStream.write(data.getBytes(StandardCharsets.UTF_8));
}
}
- int code = connection.getResponseCode();
- InputStream resultInputStream;
- if (isRequestSucceed(code)) {
- resultInputStream = connection.getInputStream();
- } else {
- log.warn("HTTP job {} executed with response code {}",
jobConfig.getJobName(), code);
- resultInputStream = connection.getErrorStream();
- }
+ int responseCode = connection.getResponseCode();
StringBuilder result = new StringBuilder();
- try (BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(resultInputStream, StandardCharsets.UTF_8))) {
+ try (
+ InputStream inputStream =
getConnectionInputStream(jobConfig.getJobName(), connection, responseCode);
+ BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(inputStream, StandardCharsets.UTF_8))) {
String line;
while (null != (line = bufferedReader.readLine())) {
result.append(line);
}
}
- if (isRequestSucceed(code)) {
+ if (isRequestSucceed(responseCode)) {
log.debug("HTTP job execute result : {}", result);
} else {
log.warn("HTTP job {} executed with response body {}",
jobConfig.getJobName(), result);
@@ -98,24 +81,26 @@ public final class HttpJobExecutor implements
TypedJobItemExecutor {
}
}
- private HttpParam getHttpParam(final Properties props) {
- String url = props.getProperty(HttpJobProperties.URI_KEY);
- if (Strings.isNullOrEmpty(url)) {
- throw new JobConfigurationException("Cannot find HTTP URL, job is
not executed.");
+ private HttpURLConnection getHttpURLConnection(final HttpParam httpParam,
final ShardingContext shardingContext) throws IOException {
+ URL url = new URL(httpParam.getUrl());
+ HttpURLConnection result = (HttpURLConnection) url.openConnection();
+ result.setRequestMethod(httpParam.getMethod());
+ result.setDoOutput(true);
+ result.setConnectTimeout(httpParam.getConnectTimeoutMilliseconds());
+ result.setReadTimeout(httpParam.getReadTimeoutMilliseconds());
+ if (!Strings.isNullOrEmpty(httpParam.getContentType())) {
+ result.setRequestProperty("Content-Type",
httpParam.getContentType());
}
- String method = props.getProperty(HttpJobProperties.METHOD_KEY);
- if (Strings.isNullOrEmpty(method)) {
- throw new JobConfigurationException("Cannot find HTTP method, job
is not executed.");
- }
- String data = props.getProperty(HttpJobProperties.DATA_KEY);
- int connectTimeout =
Integer.parseInt(props.getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY,
"3000"));
- int readTimeout =
Integer.parseInt(props.getProperty(HttpJobProperties.READ_TIMEOUT_KEY, "5000"));
- String contentType =
props.getProperty(HttpJobProperties.CONTENT_TYPE_KEY);
- return new HttpParam(url, method, data, connectTimeout, readTimeout,
contentType);
+ result.setRequestProperty(HttpJobProperties.SHARDING_CONTEXT_KEY,
GsonFactory.getGson().toJson(shardingContext));
+ return result;
}
- private boolean isWriteMethod(final String method) {
- return Arrays.asList("POST", "PUT",
"DELETE").contains(method.toUpperCase());
+ private InputStream getConnectionInputStream(final String jobName, final
HttpURLConnection connection, final int code) throws IOException {
+ if (isRequestSucceed(code)) {
+ return connection.getInputStream();
+ }
+ log.warn("HTTP job {} executed with response code {}", jobName, code);
+ return connection.getErrorStream();
}
private boolean isRequestSucceed(final int httpStatusCode) {
diff --git
a/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParam.java
b/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParam.java
index 6ad4c839c..f9419dffe 100644
---
a/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParam.java
+++
b/ecosystem/executor/http/src/main/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParam.java
@@ -17,8 +17,14 @@
package org.apache.shardingsphere.elasticjob.http.pojo;
+import com.google.common.base.Strings;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
+import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobConfigurationException;
+
+import java.util.Arrays;
+import java.util.Properties;
/**
* Http job param.
@@ -31,11 +37,35 @@ public final class HttpParam {
private final String method;
+ private final String contentType;
+
private final String data;
- private final int connectTimeout;
+ private final int connectTimeoutMilliseconds;
- private final int readTimeout;
+ private final int readTimeoutMilliseconds;
- private final String contentType;
+ public HttpParam(final Properties props) {
+ url = props.getProperty(HttpJobProperties.URI_KEY);
+ if (Strings.isNullOrEmpty(url)) {
+ throw new JobConfigurationException("Cannot find HTTP URL, job is
not executed.");
+ }
+ method = props.getProperty(HttpJobProperties.METHOD_KEY);
+ if (Strings.isNullOrEmpty(method)) {
+ throw new JobConfigurationException("Cannot find HTTP method, job
is not executed.");
+ }
+ contentType = props.getProperty(HttpJobProperties.CONTENT_TYPE_KEY);
+ data = props.getProperty(HttpJobProperties.DATA_KEY);
+ connectTimeoutMilliseconds =
Integer.parseInt(props.getProperty(HttpJobProperties.CONNECT_TIMEOUT_KEY,
"3000"));
+ readTimeoutMilliseconds =
Integer.parseInt(props.getProperty(HttpJobProperties.READ_TIMEOUT_KEY, "5000"));
+ }
+
+ /**
+ * Is write method.
+ *
+ * @return write method or not
+ */
+ public boolean isWriteMethod() {
+ return Arrays.asList("POST", "PUT",
"DELETE").contains(method.toUpperCase());
+ }
}
diff --git
a/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
b/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
index 346cf1543..20852386c 100644
---
a/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
+++
b/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/executor/HttpJobExecutorTest.java
@@ -19,15 +19,14 @@ package org.apache.shardingsphere.elasticjob.http.executor;
import org.apache.shardingsphere.elasticjob.api.ElasticJob;
import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
-import org.apache.shardingsphere.elasticjob.spi.executor.param.ShardingContext;
-import
org.apache.shardingsphere.elasticjob.spi.executor.param.JobRuntimeService;
import
org.apache.shardingsphere.elasticjob.http.executor.fixture.InternalController;
import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
-import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobConfigurationException;
import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobExecutionException;
import org.apache.shardingsphere.elasticjob.restful.NettyRestfulService;
import
org.apache.shardingsphere.elasticjob.restful.NettyRestfulServiceConfiguration;
import org.apache.shardingsphere.elasticjob.restful.RestfulService;
+import
org.apache.shardingsphere.elasticjob.spi.executor.param.JobRuntimeService;
+import org.apache.shardingsphere.elasticjob.spi.executor.param.ShardingContext;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
@@ -93,23 +92,6 @@ class HttpJobExecutorTest {
}
}
- @Test
- void assertUrlEmpty() {
- assertThrows(JobConfigurationException.class, () -> {
-
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn("");
- jobExecutor.process(elasticJob, jobConfig, jobRuntimeService,
shardingContext);
- });
- }
-
- @Test
- void assertMethodEmpty() {
- assertThrows(JobConfigurationException.class, () -> {
-
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/getName"));
-
when(jobConfig.getProps().getProperty(HttpJobProperties.METHOD_KEY)).thenReturn("");
- jobExecutor.process(elasticJob, jobConfig, jobRuntimeService,
shardingContext);
- });
- }
-
@Test
void assertProcessWithoutSuccessCode() {
when(jobConfig.getProps().getProperty(HttpJobProperties.URI_KEY)).thenReturn(getRequestUri("/unknownMethod"));
diff --git
a/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParamTest.java
b/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParamTest.java
new file mode 100644
index 000000000..42a0694fd
--- /dev/null
+++
b/ecosystem/executor/http/src/test/java/org/apache/shardingsphere/elasticjob/http/pojo/HttpParamTest.java
@@ -0,0 +1,93 @@
+/*
+ * 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 org.apache.shardingsphere.elasticjob.http.pojo;
+
+import org.apache.shardingsphere.elasticjob.http.props.HttpJobProperties;
+import
org.apache.shardingsphere.elasticjob.kernel.infra.exception.JobConfigurationException;
+import org.junit.jupiter.api.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class HttpParamTest {
+
+ @Test
+ void assertNewWithEmptyUrl() {
+ assertThrows(JobConfigurationException.class, () -> new HttpParam(new
Properties()));
+ }
+
+ @Test
+ void assertNewWithEmptyMethod() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ assertThrows(JobConfigurationException.class, () -> new
HttpParam(props));
+ }
+
+ @Test
+ void assertNew() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ props.setProperty(HttpJobProperties.METHOD_KEY, "POST");
+ HttpParam actual = new HttpParam(props);
+ assertThat(actual.getUrl(), is("foo/url"));
+ assertThat(actual.getMethod(), is("POST"));
+ assertThat(actual.getConnectTimeoutMilliseconds(), is(3000));
+ assertThat(actual.getReadTimeoutMilliseconds(), is(5000));
+ }
+
+ @Test
+ void assertIsWriteMethodWithGet() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ props.setProperty(HttpJobProperties.METHOD_KEY, "GET");
+ HttpParam actual = new HttpParam(props);
+ assertFalse(actual.isWriteMethod());
+ }
+
+ @Test
+ void assertIsWriteMethodWithPost() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ props.setProperty(HttpJobProperties.METHOD_KEY, "POST");
+ HttpParam actual = new HttpParam(props);
+ assertTrue(actual.isWriteMethod());
+ }
+
+ @Test
+ void assertIsWriteMethodWithPut() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ props.setProperty(HttpJobProperties.METHOD_KEY, "PUT");
+ HttpParam actual = new HttpParam(props);
+ assertTrue(actual.isWriteMethod());
+ }
+
+ @Test
+ void assertIsWriteMethodWithDelete() {
+ Properties props = new Properties();
+ props.setProperty(HttpJobProperties.URI_KEY, "foo/url");
+ props.setProperty(HttpJobProperties.METHOD_KEY, "DELETE");
+ HttpParam actual = new HttpParam(props);
+ assertTrue(actual.isWriteMethod());
+ }
+}