You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2020/07/18 03:08:01 UTC

[shardingsphere-elasticjob] branch master updated: Add job identification strategy. (#472) (#1106)

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.git


The following commit(s) were added to refs/heads/master by this push:
     new e938491  Add job identification strategy. (#472) (#1106)
e938491 is described below

commit e938491e844def7a2f0fd3156b0b35da143999d3
Author: Long Chen <54...@qq.com>
AuthorDate: Sat Jul 18 11:07:50 2020 +0800

    Add job identification strategy. (#472) (#1106)
    
    * Add job identification strategy
    
    * Format code and fix checkstyle error
    
    * Revert jobIdentification to jobClassName
    
    * Rename JobIdentificationStrategy to JobClassNameProvider
    
    * Remove unused parameter jobName
---
 .../lite/internal/schedule/JobScheduler.java       |  4 +-
 .../lite/internal/setup/JobClassNameProvider.java  | 34 +++++++++++++++
 .../setup/JobClassNameProviderFactory.java         | 51 ++++++++++++++++++++++
 .../internal/setup/SimpleJobClassNameProvider.java | 31 +++++++++++++
 .../setup/JobClassNameProviderFactoryTest.java     | 31 +++++++++++++
 .../setup/SpringProxyJobClassNameProvider.java     | 37 ++++++++++++++++
 ...ticjob.lite.internal.setup.JobClassNameProvider | 18 ++++++++
 .../setup/JobClassNameProviderFactoryTest.java     | 32 ++++++++++++++
 8 files changed, 237 insertions(+), 1 deletion(-)

diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/schedule/JobScheduler.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/schedule/JobScheduler.java
index 12e7229..fface2f 100644
--- a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/schedule/JobScheduler.java
+++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/schedule/JobScheduler.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.elasticjob.infra.exception.JobSystemException;
 import org.apache.shardingsphere.elasticjob.infra.handler.sharding.JobInstance;
 import org.apache.shardingsphere.elasticjob.lite.api.listener.AbstractDistributeOnceElasticJobListener;
 import org.apache.shardingsphere.elasticjob.lite.internal.guarantee.GuaranteeService;
+import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProviderFactory;
 import org.apache.shardingsphere.elasticjob.lite.internal.setup.SetUpFacade;
 import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
 import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
@@ -89,7 +90,8 @@ public final class JobScheduler {
         this.tracingConfig = tracingConfig;
         setUpFacade = new SetUpFacade(regCenter, jobConfig.getJobName(), this.elasticJobListeners);
         schedulerFacade = new SchedulerFacade(regCenter, jobConfig.getJobName());
-        this.jobConfig = setUpFacade.setUpJobConfiguration(elasticJob.getClass().getName(), jobConfig);
+        String jobClassName = JobClassNameProviderFactory.getProvider().getJobClassName(elasticJob);
+        this.jobConfig = setUpFacade.setUpJobConfiguration(jobClassName, jobConfig);
         setGuaranteeServiceForElasticJobListeners(regCenter, this.elasticJobListeners);
         jobScheduleController = createJobScheduleController();
     }
diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProvider.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProvider.java
new file mode 100644
index 0000000..08f613c
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProvider.java
@@ -0,0 +1,34 @@
+/*
+ * 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.lite.internal.setup;
+
+import org.apache.shardingsphere.elasticjob.api.ElasticJob;
+
+/**
+ * Job class name provider.
+ */
+public interface JobClassNameProvider {
+    
+    /**
+     * Get job class name.
+     *
+     * @param elasticJob job instance
+     * @return job class name
+     */
+    String getJobClassName(ElasticJob elasticJob);
+}
diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactory.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactory.java
new file mode 100644
index 0000000..8f5c440
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactory.java
@@ -0,0 +1,51 @@
+/*
+ * 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.lite.internal.setup;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.ServiceLoader;
+
+/**
+ * Job class name provider factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class JobClassNameProviderFactory {
+    
+    private static final List<JobClassNameProvider> PROVIDERS = new LinkedList<>();
+    
+    private static final JobClassNameProvider DEFAULT_STRATEGY = new SimpleJobClassNameProvider();
+    
+    static {
+        for (JobClassNameProvider each : ServiceLoader.load(JobClassNameProvider.class)) {
+            PROVIDERS.add(each);
+        }
+    }
+    
+    /**
+     * Get the first job class name provider.
+     *
+     * @return job class name provider
+     */
+    public static JobClassNameProvider getProvider() {
+        return PROVIDERS.isEmpty() ? DEFAULT_STRATEGY : PROVIDERS.get(0);
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/SimpleJobClassNameProvider.java b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/SimpleJobClassNameProvider.java
new file mode 100644
index 0000000..928018a
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-core/src/main/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/SimpleJobClassNameProvider.java
@@ -0,0 +1,31 @@
+/*
+ * 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.lite.internal.setup;
+
+import org.apache.shardingsphere.elasticjob.api.ElasticJob;
+
+/**
+ * Simple provider for get job class name.
+ */
+public final class SimpleJobClassNameProvider implements JobClassNameProvider {
+    
+    @Override
+    public String getJobClassName(final ElasticJob elasticJob) {
+        return elasticJob.getClass().getName();
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactoryTest.java b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactoryTest.java
new file mode 100644
index 0000000..1d390cf
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-core/src/test/java/org/apache/shardingsphere/elasticjob/lite/internal/setup/JobClassNameProviderFactoryTest.java
@@ -0,0 +1,31 @@
+/*
+ * 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.lite.internal.setup;
+
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+
+public final class JobClassNameProviderFactoryTest {
+    
+    @Test
+    public void assertGetDefaultStrategy() {
+        assertThat(JobClassNameProviderFactory.getProvider(), instanceOf(SimpleJobClassNameProvider.class));
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/SpringProxyJobClassNameProvider.java b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/SpringProxyJobClassNameProvider.java
new file mode 100644
index 0000000..8307912
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/SpringProxyJobClassNameProvider.java
@@ -0,0 +1,37 @@
+/*
+ * 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.lite.spring.setup;
+
+import org.apache.shardingsphere.elasticjob.api.ElasticJob;
+import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProvider;
+import org.apache.shardingsphere.elasticjob.lite.spring.job.util.AopTargetUtils;
+import org.springframework.aop.support.AopUtils;
+
+/**
+ * Provider for get job class name.
+ * <p>
+ * Consider the proxy object that generated by cglib or jdk dynamic proxy.
+ * </p>
+ */
+public final class SpringProxyJobClassNameProvider implements JobClassNameProvider {
+
+    @Override
+    public String getJobClassName(final ElasticJob elasticJob) {
+        return AopUtils.isAopProxy(elasticJob) ? AopTargetUtils.getTarget(elasticJob).getClass().getName() : elasticJob.getClass().getName();
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/services/org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProvider b/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/services/org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProvider
new file mode 100644
index 0000000..4487c3f
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/services/org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProvider
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.elasticjob.lite.spring.setup.SpringProxyJobClassNameProvider
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/JobClassNameProviderFactoryTest.java b/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/JobClassNameProviderFactoryTest.java
new file mode 100644
index 0000000..5d6ddff
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/setup/JobClassNameProviderFactoryTest.java
@@ -0,0 +1,32 @@
+/*
+ * 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.lite.spring.setup;
+
+import org.apache.shardingsphere.elasticjob.lite.internal.setup.JobClassNameProviderFactory;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertThat;
+
+public final class JobClassNameProviderFactoryTest {
+    
+    @Test
+    public void assertGetStrategy() {
+        assertThat(JobClassNameProviderFactory.getProvider(), instanceOf(SpringProxyJobClassNameProvider.class));
+    }
+}