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 16:42:02 UTC
[shardingsphere-elasticjob] branch master updated: Support job-type
configuration of spring namespace #1114 (#1141)
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 7ae257d Support job-type configuration of spring namespace #1114 (#1141)
7ae257d is described below
commit 7ae257d05189712902a48737184dab0535d89860
Author: jiang2015 <27...@qq.com>
AuthorDate: Sun Jul 19 00:41:52 2020 +0800
Support job-type configuration of spring namespace #1114 (#1141)
* Support job-type configuration of spring namespace #1114
* Support job-type configuration of spring namespace #1114
* Support job-type configuration of spring namespace #1114
add test case
* modify ci error
* covert to old template: add space
---
.../spring/job/parser/JobBeanDefinitionParser.java | 7 +++-
.../lite/spring/job/tag/JobBeanDefinitionTag.java | 2 +
.../resources/META-INF/namespace/elasticjob.xsd | 5 ++-
.../spring/job/JobSpringNamespaceWithTypeTest.java | 48 ++++++++++++++++++++++
.../test/resources/META-INF/job/withJobType.xml | 36 ++++++++++++++++
.../src/test/resources/conf/job/conf.properties | 4 ++
.../src/test/resources/script/demo.bat | 18 ++++++++
.../{conf/job/conf.properties => script/demo.sh} | 24 +----------
8 files changed, 119 insertions(+), 25 deletions(-)
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/parser/JobBeanDefinitionParser.java b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/parser/JobBeanDefinitionParser.java
index bfe0b63..382c5c5 100644
--- a/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/parser/JobBeanDefinitionParser.java
+++ b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/parser/JobBeanDefinitionParser.java
@@ -50,7 +50,12 @@ public final class JobBeanDefinitionParser extends AbstractBeanDefinitionParser
factory.setInitMethodName("schedule");
}
factory.addConstructorArgReference(element.getAttribute(JobBeanDefinitionTag.REGISTRY_CENTER_REF_ATTRIBUTE));
- factory.addConstructorArgReference(element.getAttribute(JobBeanDefinitionTag.JOB_REF_ATTRIBUTE));
+ String jobType = element.getAttribute(JobBeanDefinitionTag.JOB_TYPE_ATTRIBUTE);
+ if (!Strings.isNullOrEmpty(jobType)) {
+ factory.addConstructorArgValue(jobType);
+ } else {
+ factory.addConstructorArgReference(element.getAttribute(JobBeanDefinitionTag.JOB_REF_ATTRIBUTE));
+ }
factory.addConstructorArgValue(createJobConfigurationBeanDefinition(element, parserContext));
String tracingRef = element.getAttribute(JobBeanDefinitionTag.TRACING_REF_ATTRIBUTE);
if (!Strings.isNullOrEmpty(tracingRef)) {
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/tag/JobBeanDefinitionTag.java b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/tag/JobBeanDefinitionTag.java
index 59332b6..99b6463 100644
--- a/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/tag/JobBeanDefinitionTag.java
+++ b/elasticjob-lite/elasticjob-lite-spring/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/job/tag/JobBeanDefinitionTag.java
@@ -28,6 +28,8 @@ public final class JobBeanDefinitionTag {
public static final String JOB_REF_ATTRIBUTE = "job-ref";
+ public static final String JOB_TYPE_ATTRIBUTE = "job-type";
+
public static final String REGISTRY_CENTER_REF_ATTRIBUTE = "registry-center-ref";
public static final String TRACING_REF_ATTRIBUTE = "tracing-ref";
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/namespace/elasticjob.xsd b/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/namespace/elasticjob.xsd
index 6d8bdab..577a68a 100644
--- a/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/namespace/elasticjob.xsd
+++ b/elasticjob-lite/elasticjob-lite-spring/src/main/resources/META-INF/namespace/elasticjob.xsd
@@ -21,7 +21,7 @@
xmlns:beans="http://www.springframework.org/schema/beans"
targetNamespace="http://shardingsphere.apache.org/schema/elasticjob"
elementFormDefault="qualified">
-
+
<xsd:import namespace="http://www.springframework.org/schema/beans" schemaLocation="http://www.springframework.org/schema/beans/spring-beans.xsd" />
<xsd:element name="listener">
@@ -47,6 +47,7 @@
<xsd:element ref="distributed-listener" minOccurs="0" />
</xsd:all>
<xsd:attribute name="job-ref" type="xsd:string" />
+ <xsd:attribute name="job-type" type="xsd:string" />
<xsd:attribute name="registry-center-ref" type="xsd:string" use="required" />
<xsd:attribute name="tracing-ref" type="xsd:string" />
<xsd:attribute name="sharding-total-count" type="xsd:string" use="required" />
@@ -95,7 +96,7 @@
</xsd:complexContent>
</xsd:complexType>
</xsd:element>
-
+
<xsd:element name="snapshot">
<xsd:complexType>
<xsd:complexContent>
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/job/JobSpringNamespaceWithTypeTest.java b/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/job/JobSpringNamespaceWithTypeTest.java
new file mode 100644
index 0000000..edc94d3
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/job/JobSpringNamespaceWithTypeTest.java
@@ -0,0 +1,48 @@
+/*
+ * 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.job;
+
+import static org.junit.Assert.assertTrue;
+import javax.annotation.Resource;
+import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils;
+import org.apache.shardingsphere.elasticjob.lite.internal.schedule.JobRegistry;
+import org.apache.shardingsphere.elasticjob.lite.spring.test.AbstractZookeeperJUnit4SpringContextTests;
+import org.apache.shardingsphere.elasticjob.reg.base.CoordinatorRegistryCenter;
+import org.junit.After;
+import org.junit.Test;
+import org.springframework.test.context.ContextConfiguration;
+
+@ContextConfiguration(locations = "classpath:META-INF/job/withJobType.xml")
+public final class JobSpringNamespaceWithTypeTest extends AbstractZookeeperJUnit4SpringContextTests {
+
+ private final String scriptJobName = "scriptElasticJob_job_type";
+
+ @Resource
+ private CoordinatorRegistryCenter regCenter;
+
+ @After
+ public void tearDown() {
+ JobRegistry.getInstance().shutdown(scriptJobName);
+ }
+
+ @Test
+ public void jobScriptWithJobTypeTest() {
+ BlockUtils.sleep(1000L);
+ assertTrue(regCenter.isExisted("/" + scriptJobName + "/sharding"));
+ }
+}
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/META-INF/job/withJobType.xml b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/META-INF/job/withJobType.xml
new file mode 100644
index 0000000..4b087cc
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/META-INF/job/withJobType.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ 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.
+ -->
+
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:elasticjob="http://shardingsphere.apache.org/schema/elasticjob"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://shardingsphere.apache.org/schema/elasticjob
+ http://shardingsphere.apache.org/schema/elasticjob/elasticjob.xsd
+ ">
+ <import resource="base.xml"/>
+
+ <elasticjob:job id="scriptElasticJob_job_type" job-type="SCRIPT" registry-center-ref="regCenter"
+ cron="${simpleJob.cron}" sharding-total-count="${simpleJob.shardingTotalCount}" sharding-item-parameters="${simpleJob.shardingItemParameters}"
+ disabled="${simpleJob.disabled}" overwrite="${simpleJob.overwrite}" job-executor-service-handler-type="SINGLE_THREAD" >
+ <props>
+ <prop key="script.command.line">${script.scriptCommandLine}</prop>
+ </props>
+ </elasticjob:job>
+</beans>
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties
index 878d8a6..18714ea 100644
--- a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties
@@ -37,3 +37,7 @@ dataflowJob.description=Examples of jobs that do not stop running
dataflowJob.disabled=false
dataflowJob.overwrite=true
dataflowJob.streamingProcess=true
+
+# need absolute path
+#script.scriptCommandLine=your_path/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.sh
+script.scriptCommandLine=echo test
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.bat b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.bat
new file mode 100644
index 0000000..2d573e4
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.bat
@@ -0,0 +1,18 @@
+@rem
+@rem Licensed to the Apache Software Foundation (ASF) under one or more
+@rem contributor license agreements. See the NOTICE file distributed with
+@rem this work for additional information regarding copyright ownership.
+@rem The ASF licenses this file to You under the Apache License, Version 2.0
+@rem (the "License"); you may not use this file except in compliance with
+@rem the License. You may obtain a copy of the License at
+@rem
+@rem http://www.apache.org/licenses/LICENSE-2.0
+@rem
+@rem Unless required by applicable law or agreed to in writing, software
+@rem distributed under the License is distributed on an "AS IS" BASIS,
+@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+@rem See the License for the specific language governing permissions and
+@rem limitations under the License.
+@rem
+
+@echo Sharding Context: %*
diff --git a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.sh
similarity index 52%
copy from elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties
copy to elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.sh
index 878d8a6..94792db 100644
--- a/elasticjob-lite/elasticjob-lite-spring/src/test/resources/conf/job/conf.properties
+++ b/elasticjob-lite/elasticjob-lite-spring/src/test/resources/script/demo.sh
@@ -1,3 +1,4 @@
+#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
@@ -15,25 +16,4 @@
# limitations under the License.
#
-regCenter.serverLists=localhost:3181
-regCenter.namespace=elasticjob-lite-spring-test
-regCenter.baseSleepTimeMilliseconds=1000
-regCenter.maxSleepTimeMilliseconds=3000
-regCenter.maxRetries=3
-
-simpleJob.id=simpleElasticJob_namespace_listener
-simpleJob.cron=0/1 * * * * ?
-simpleJob.shardingTotalCount=3
-simpleJob.shardingItemParameters=0=A,1=B,2=C
-simpleJob.disabled=false
-simpleJob.overwrite=true
-
-dataflowJob.cron=0/1 * * * * ?
-dataflowJob.shardingTotalCount=3
-dataflowJob.shardingItemParameters=0=A,1=B,2=C
-dataflowJob.monitorExecution=true
-dataflowJob.failover=true
-dataflowJob.description=Examples of jobs that do not stop running
-dataflowJob.disabled=false
-dataflowJob.overwrite=true
-dataflowJob.streamingProcess=true
+echo Sharding Context: $*