This is an automated email from the ASF dual-hosted git repository. zhangliang pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/shardingsphere-elasticjob-lite.git
The following commit(s) were added to refs/heads/master by this push: new 7e278ca Simplify ShardingContext's constructor (#1026) 7e278ca is described below commit 7e278ca91fb7b421a8b66d6cc7b7d5d8948d464c Author: Liang Zhang <terrym...@163.com> AuthorDate: Sat Jul 11 14:00:50 2020 +0800 Simplify ShardingContext's constructor (#1026) * Remove useless Encryption * Simplify ShardingContext's constructor --- .../elasticjob/cloud/api/ShardingContext.java | 39 +++-------------- .../cloud/executor/AbstractElasticJobExecutor.java | 2 +- .../cloud/executor/ShardingContexts.java | 12 +++++ .../elasticjob/cloud/util/digest/Encryption.java | 51 ---------------------- .../elasticjob/cloud/api/ShardingContextTest.java | 46 ------------------- .../cloud/util/digest/EncryptionTest.java | 31 ------------- .../config/job/CloudJobConfiguration.java | 8 ++-- .../elasticjob/lite/api/job/ShardingContext.java | 12 +---- .../lite/executor/ElasticJobExecutor.java | 5 +-- .../elasticjob/lite/executor/ShardingContexts.java | 11 +++++ .../ShardingContextsTest.java} | 18 +++----- 11 files changed, 43 insertions(+), 192 deletions(-) diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContext.java b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContext.java index 4f8ae8b..dd4a335 100755 --- a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContext.java +++ b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContext.java @@ -17,56 +17,27 @@ package org.apache.shardingsphere.elasticjob.cloud.api; -import org.apache.shardingsphere.elasticjob.cloud.executor.ShardingContexts; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.ToString; /** * Sharding context. */ +@RequiredArgsConstructor @Getter @ToString public final class ShardingContext { - /** - * job name. - */ private final String jobName; - /** - * task ID. - */ private final String taskId; - /** - * sharding total count. - */ private final int shardingTotalCount; - - /** - * job parameter. - * - * <p>Can configure for same job class, but use different parameter for different job schedule instance.</p> - * - */ + private final String jobParameter; - - /** - * Sharding item assigned for this sharding. - */ + private final int shardingItem; - - /** - * Sharding parameter assigned for this sharding. - */ - private final String shardingParameter; - public ShardingContext(final ShardingContexts shardingContexts, final int shardingItem) { - jobName = shardingContexts.getJobName(); - taskId = shardingContexts.getTaskId(); - shardingTotalCount = shardingContexts.getShardingTotalCount(); - jobParameter = shardingContexts.getJobParameter(); - this.shardingItem = shardingItem; - shardingParameter = shardingContexts.getShardingItemParameters().get(shardingItem); - } + private final String shardingParameter; } diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/AbstractElasticJobExecutor.java b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/AbstractElasticJobExecutor.java index 8d6b5f2..0e5cd5b 100755 --- a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/AbstractElasticJobExecutor.java +++ b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/AbstractElasticJobExecutor.java @@ -199,7 +199,7 @@ public abstract class AbstractElasticJobExecutor { log.trace("Job '{}' executing, item is: '{}'.", jobName, item); JobExecutionEvent completeEvent; try { - process(new ShardingContext(shardingContexts, item)); + process(shardingContexts.createShardingContext(item)); completeEvent = startEvent.executionSuccess(); log.trace("Job '{}' executed, item is: '{}'.", jobName, item); if (shardingContexts.isAllowSendJobEvent()) { diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/ShardingContexts.java b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/ShardingContexts.java index 48be43f..3dedebe 100755 --- a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/ShardingContexts.java +++ b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/executor/ShardingContexts.java @@ -21,6 +21,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.Setter; import lombok.ToString; +import org.apache.shardingsphere.elasticjob.cloud.api.ShardingContext; import java.io.Serializable; import java.util.Map; @@ -87,4 +88,15 @@ public final class ShardingContexts implements Serializable { this.shardingItemParameters = shardingItemParameters; this.jobEventSamplingCount = jobEventSamplingCount; } + + + /** + * Create sharding context. + * + * @param shardingItem sharding item + * @return sharding context + */ + public ShardingContext createShardingContext(final int shardingItem) { + return new ShardingContext(jobName, taskId, shardingTotalCount, jobParameter, shardingItem, shardingItemParameters.get(shardingItem)); + } } diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/Encryption.java b/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/Encryption.java deleted file mode 100755 index 17fb18b..0000000 --- a/elasticjob-cloud/elasticjob-cloud-common/src/main/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/Encryption.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * 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.cloud.util.digest; - -import org.apache.shardingsphere.elasticjob.cloud.exception.JobSystemException; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -/** - * Encryption. - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class Encryption { - - private static final String MD5 = "MD5"; - - /** - * Use md5 to encrypt string. - * - * @param str string to be encrypted - * @return encrypted string - */ - public static String md5(final String str) { - try { - MessageDigest messageDigest = MessageDigest.getInstance(MD5); - messageDigest.update(str.getBytes()); - return new BigInteger(1, messageDigest.digest()).toString(16); - } catch (final NoSuchAlgorithmException ex) { - throw new JobSystemException(ex); - } - } -} diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContextTest.java b/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContextTest.java deleted file mode 100755 index 8300738..0000000 --- a/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/api/ShardingContextTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.cloud.api; - -import org.apache.shardingsphere.elasticjob.cloud.executor.ShardingContexts; -import org.apache.shardingsphere.elasticjob.cloud.fixture.ShardingContextsBuilder; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public final class ShardingContextTest { - - @Test - public void assertNew() { - ShardingContexts shardingContexts = ShardingContextsBuilder.getMultipleShardingContexts(); - ShardingContext actual = new ShardingContext(shardingContexts, 1); - assertThat(actual.getJobName(), is(shardingContexts.getJobName())); - assertThat(actual.getTaskId(), is(shardingContexts.getTaskId())); - assertThat(actual.getShardingTotalCount(), is(shardingContexts.getShardingTotalCount())); - assertThat(actual.getJobParameter(), is(shardingContexts.getJobParameter())); - assertThat(actual.getShardingItem(), is(1)); - assertThat(actual.getShardingParameter(), is(shardingContexts.getShardingItemParameters().get(1))); - } - - @Test - public void assertToString() { - assertThat(new ShardingContext(ShardingContextsBuilder.getMultipleShardingContexts(), 1).toString(), - is("ShardingContext(jobName=test_job, taskId=fake_task_id, shardingTotalCount=2, jobParameter=, shardingItem=1, shardingParameter=B)")); - } -} diff --git a/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/EncryptionTest.java b/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/EncryptionTest.java deleted file mode 100755 index d673398..0000000 --- a/elasticjob-cloud/elasticjob-cloud-common/src/test/java/org/apache/shardingsphere/elasticjob/cloud/util/digest/EncryptionTest.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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.cloud.util.digest; - -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; - -public final class EncryptionTest { - - @Test - public void assertMd5() { - assertThat(Encryption.md5("test"), is("98f6bcd4621d373cade4e832627b4f6")); - } -} diff --git a/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/config/job/CloudJobConfiguration.java b/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/config/job/CloudJobConfiguration.java index 91ab5f8..180dfc9 100755 --- a/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/config/job/CloudJobConfiguration.java +++ b/elasticjob-cloud/elasticjob-cloud-scheduler/src/main/java/org/apache/shardingsphere/elasticjob/cloud/scheduler/config/job/CloudJobConfiguration.java @@ -17,17 +17,17 @@ package org.apache.shardingsphere.elasticjob.cloud.scheduler.config.job; -import org.apache.shardingsphere.elasticjob.cloud.config.JobTypeConfiguration; -import org.apache.shardingsphere.elasticjob.cloud.config.JobRootConfiguration; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.RequiredArgsConstructor; +import org.apache.shardingsphere.elasticjob.cloud.config.JobRootConfiguration; +import org.apache.shardingsphere.elasticjob.cloud.config.JobTypeConfiguration; /** * Cloud job configuration. */ -@RequiredArgsConstructor @AllArgsConstructor +@RequiredArgsConstructor @Getter public final class CloudJobConfiguration implements JobRootConfiguration { @@ -43,7 +43,7 @@ public final class CloudJobConfiguration implements JobRootConfiguration { private String beanName; - private String applicationContext; + private String applicationContext; /** * Get job name. diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContext.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContext.java index d2370a4..43e19df 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContext.java +++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContext.java @@ -18,12 +18,13 @@ package org.apache.shardingsphere.elasticjob.lite.api.job; import lombok.Getter; +import lombok.RequiredArgsConstructor; import lombok.ToString; -import org.apache.shardingsphere.elasticjob.lite.executor.ShardingContexts; /** * Sharding context. */ +@RequiredArgsConstructor @Getter @ToString public final class ShardingContext { @@ -39,13 +40,4 @@ public final class ShardingContext { private final int shardingItem; private final String shardingParameter; - - public ShardingContext(final ShardingContexts shardingContexts, final int shardingItem) { - jobName = shardingContexts.getJobName(); - taskId = shardingContexts.getTaskId(); - shardingTotalCount = shardingContexts.getShardingTotalCount(); - jobParameter = shardingContexts.getJobParameter(); - this.shardingItem = shardingItem; - shardingParameter = shardingContexts.getShardingItemParameters().get(shardingItem); - } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ElasticJobExecutor.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ElasticJobExecutor.java index 1f98d52..8129e25 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ElasticJobExecutor.java +++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ElasticJobExecutor.java @@ -19,7 +19,6 @@ package org.apache.shardingsphere.elasticjob.lite.executor; import lombok.extern.slf4j.Slf4j; import org.apache.shardingsphere.elasticjob.lite.api.job.ElasticJob; -import org.apache.shardingsphere.elasticjob.lite.api.job.ShardingContext; import org.apache.shardingsphere.elasticjob.lite.api.job.JobConfiguration; import org.apache.shardingsphere.elasticjob.lite.api.listener.ElasticJobListener; import org.apache.shardingsphere.elasticjob.lite.exception.ExceptionUtils; @@ -30,12 +29,12 @@ import org.apache.shardingsphere.elasticjob.lite.handler.error.JobErrorHandler; import org.apache.shardingsphere.elasticjob.lite.handler.error.JobErrorHandlerFactory; import org.apache.shardingsphere.elasticjob.lite.handler.threadpool.JobExecutorServiceHandlerFactory; import org.apache.shardingsphere.elasticjob.lite.internal.schedule.LiteJobFacade; +import org.apache.shardingsphere.elasticjob.lite.util.env.IpUtils; import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter; import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration; import org.apache.shardingsphere.elasticjob.tracing.event.JobExecutionEvent; import org.apache.shardingsphere.elasticjob.tracing.event.JobExecutionEvent.ExecutionSource; import org.apache.shardingsphere.elasticjob.tracing.event.JobStatusTraceEvent.State; -import org.apache.shardingsphere.elasticjob.lite.util.env.IpUtils; import java.util.Collection; import java.util.List; @@ -180,7 +179,7 @@ public final class ElasticJobExecutor { log.trace("Job '{}' executing, item is: '{}'.", jobConfig.getJobName(), item); JobExecutionEvent completeEvent; try { - jobItemExecutor.process(elasticJob, jobConfig, jobFacade, new ShardingContext(shardingContexts, item)); + jobItemExecutor.process(elasticJob, jobConfig, jobFacade, shardingContexts.createShardingContext(item)); completeEvent = startEvent.executionSuccess(); log.trace("Job '{}' executed, item is: '{}'.", jobConfig.getJobName(), item); jobFacade.postJobExecutionEvent(completeEvent); diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContexts.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContexts.java index 3bab810..d1d0ba3 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContexts.java +++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContexts.java @@ -20,6 +20,7 @@ package org.apache.shardingsphere.elasticjob.lite.executor; import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.ToString; +import org.apache.shardingsphere.elasticjob.lite.api.job.ShardingContext; import java.io.Serializable; import java.util.Map; @@ -59,4 +60,14 @@ public final class ShardingContexts implements Serializable { * Sharding items and parameters map. */ private final Map<Integer, String> shardingItemParameters; + + /** + * Create sharding context. + * + * @param shardingItem sharding item + * @return sharding context + */ + public ShardingContext createShardingContext(final int shardingItem) { + return new ShardingContext(jobName, taskId, shardingTotalCount, jobParameter, shardingItem, shardingItemParameters.get(shardingItem)); + } } diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContextTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContextsTest.java similarity index 73% rename from elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContextTest.java rename to elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContextsTest.java index 8fe986b..93dc917 100644 --- a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/api/job/ShardingContextTest.java +++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/executor/ShardingContextsTest.java @@ -15,9 +15,9 @@ * limitations under the License. */ -package org.apache.shardingsphere.elasticjob.lite.api.job; +package org.apache.shardingsphere.elasticjob.lite.executor; -import org.apache.shardingsphere.elasticjob.lite.executor.ShardingContexts; +import org.apache.shardingsphere.elasticjob.lite.api.job.ShardingContext; import org.junit.Test; import java.util.HashMap; @@ -26,12 +26,12 @@ import java.util.Map; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; -public final class ShardingContextTest { +public final class ShardingContextsTest { @Test - public void assertNew() { + public void assertCreateShardingContext() { ShardingContexts shardingContexts = createShardingContexts(); - ShardingContext actual = new ShardingContext(shardingContexts, 1); + ShardingContext actual = shardingContexts.createShardingContext(1); assertThat(actual.getJobName(), is(shardingContexts.getJobName())); assertThat(actual.getTaskId(), is(shardingContexts.getTaskId())); assertThat(actual.getShardingTotalCount(), is(shardingContexts.getShardingTotalCount())); @@ -40,13 +40,7 @@ public final class ShardingContextTest { assertThat(actual.getShardingParameter(), is(shardingContexts.getShardingItemParameters().get(1))); } - @Test - public void assertToString() { - assertThat(new ShardingContext(createShardingContexts(), 1).toString(), - is("ShardingContext(jobName=test_job, taskId=fake_task_id, shardingTotalCount=2, jobParameter=, shardingItem=1, shardingParameter=B)")); - } - - private static ShardingContexts createShardingContexts() { + private ShardingContexts createShardingContexts() { Map<Integer, String> map = new HashMap<>(2, 1); map.put(0, "A"); map.put(1, "B");