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/10/22 02:40:04 UTC

[shardingsphere-elasticjob] branch master updated: Support jobListenerTypes for module elasticjob-lite-spring-boot-starter and complete testcases (#1634)

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 cce1e67  Support jobListenerTypes for module elasticjob-lite-spring-boot-starter and complete testcases (#1634)
cce1e67 is described below

commit cce1e6710c09241bde34d7f00f183215f9bad9e0
Author: 吴伟杰 <ro...@me.com>
AuthorDate: Thu Oct 22 10:39:04 2020 +0800

    Support jobListenerTypes for module elasticjob-lite-spring-boot-starter and complete testcases (#1634)
---
 .../job/ElasticJobConfigurationProperties.java     |  6 ++-
 .../spring/boot/job/ElasticJobSpringBootTest.java  | 30 +++++++++++++++
 .../fixture/listener/LogElasticJobListener.java    | 44 ++++++++++++++++++++++
 .../fixture/listener/NoopElasticJobListener.java   | 40 ++++++++++++++++++++
 ...e.elasticjob.infra.listener.ElasticJobListener} | 26 +------------
 .../src/test/resources/application-elasticjob.yml  |  3 ++
 6 files changed, 124 insertions(+), 25 deletions(-)

diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobConfigurationProperties.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobConfigurationProperties.java
index 50e1f27..d172fba 100644
--- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobConfigurationProperties.java
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/main/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobConfigurationProperties.java
@@ -22,6 +22,8 @@ import lombok.Setter;
 import org.apache.shardingsphere.elasticjob.api.ElasticJob;
 import org.apache.shardingsphere.elasticjob.api.JobConfiguration;
 
+import java.util.Collection;
+import java.util.LinkedList;
 import java.util.Properties;
 
 /**
@@ -61,6 +63,8 @@ public final class ElasticJobConfigurationProperties {
     
     private String jobErrorHandlerType;
     
+    private Collection<String> jobListenerTypes = new LinkedList<>();
+    
     private String description;
     
     private Properties props = new Properties();
@@ -81,7 +85,7 @@ public final class ElasticJobConfigurationProperties {
                 .monitorExecution(monitorExecution).failover(failover).misfire(misfire)
                 .maxTimeDiffSeconds(maxTimeDiffSeconds).reconcileIntervalMinutes(reconcileIntervalMinutes)
                 .jobShardingStrategyType(jobShardingStrategyType).jobExecutorServiceHandlerType(jobExecutorServiceHandlerType).jobErrorHandlerType(jobErrorHandlerType)
-                .description(description).disabled(disabled).overwrite(overwrite).build();
+                .jobListenerTypes(jobListenerTypes.toArray(new String[0])).description(description).disabled(disabled).overwrite(overwrite).build();
         props.stringPropertyNames().forEach(each -> result.getProps().setProperty(each, props.getProperty(each)));
         return result;
     }
diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java
index 00c039c..8636370 100644
--- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/ElasticJobSpringBootTest.java
@@ -22,6 +22,7 @@ import org.apache.shardingsphere.elasticjob.infra.concurrent.BlockUtils;
 import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.JobBootstrap;
 import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
 import org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.EmbedTestingServer;
+import org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.job.impl.CustomTestJob;
 import org.apache.shardingsphere.elasticjob.lite.spring.boot.reg.ZookeeperProperties;
 import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
 import org.apache.shardingsphere.elasticjob.tracing.api.TracingConfiguration;
@@ -34,11 +35,14 @@ import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
 
 import javax.sql.DataSource;
 import java.sql.SQLException;
+import java.util.Arrays;
 import java.util.Map;
 
+import static org.hamcrest.CoreMatchers.equalTo;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
 
@@ -81,6 +85,32 @@ public class ElasticJobSpringBootTest extends AbstractJUnit4SpringContextTests {
     }
     
     @Test
+    public void assertElasticJobProperties() {
+        assertNotNull(applicationContext);
+        ElasticJobProperties elasticJobProperties = applicationContext.getBean(ElasticJobProperties.class);
+        assertNotNull(elasticJobProperties);
+        assertNotNull(elasticJobProperties.getJobs());
+        assertThat(elasticJobProperties.getJobs().size(), is(2));
+        ElasticJobConfigurationProperties customTestJobProperties = elasticJobProperties.getJobs().get("customTestJob");
+        assertNotNull(customTestJobProperties);
+        assertThat(customTestJobProperties.getElasticJobClass(), is(CustomTestJob.class));
+        assertThat(customTestJobProperties.getJobBootstrapBeanName(), is("customTestJobBean"));
+        assertThat(customTestJobProperties.getShardingTotalCount(), is(3));
+        assertNull(customTestJobProperties.getElasticJobType());
+        assertThat(customTestJobProperties.getJobListenerTypes().size(), is(2));
+        assertThat(customTestJobProperties.getJobListenerTypes(), equalTo(Arrays.asList("NOOP", "LOG")));
+        ElasticJobConfigurationProperties printTestJobProperties = elasticJobProperties.getJobs().get("printTestJob");
+        assertNotNull(printTestJobProperties);
+        assertNull(printTestJobProperties.getElasticJobClass());
+        assertThat(printTestJobProperties.getElasticJobType(), is("PRINT"));
+        assertThat(printTestJobProperties.getJobBootstrapBeanName(), is("printTestJobBean"));
+        assertThat(printTestJobProperties.getShardingTotalCount(), is(3));
+        assertTrue(printTestJobProperties.getJobListenerTypes().isEmpty());
+        assertThat(printTestJobProperties.getProps().size(), is(1));
+        assertThat(printTestJobProperties.getProps().getProperty("print.content"), is("test print job"));
+    }
+    
+    @Test
     public void assertJobScheduleCreation() {
         assertNotNull(applicationContext);
         Map<String, ElasticJob> elasticJobBeans = applicationContext.getBeansOfType(ElasticJob.class);
diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/LogElasticJobListener.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/LogElasticJobListener.java
new file mode 100644
index 0000000..e213f8c
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/LogElasticJobListener.java
@@ -0,0 +1,44 @@
+/*
+ * 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.boot.job.fixture.listener;
+
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener;
+import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
+
+/**
+ * Log elastic job listener.
+ */
+@Slf4j
+public final class LogElasticJobListener implements ElasticJobListener {
+    
+    @Override
+    public void beforeJobExecuted(final ShardingContexts shardingContexts) {
+        log.info("Before job executed. {}", shardingContexts);
+    }
+    
+    @Override
+    public void afterJobExecuted(final ShardingContexts shardingContexts) {
+        log.info("After job executed. {}", shardingContexts);
+    }
+    
+    @Override
+    public String getType() {
+        return "LOG";
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/NoopElasticJobListener.java b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/NoopElasticJobListener.java
new file mode 100644
index 0000000..2b9e2d9
--- /dev/null
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/java/org/apache/shardingsphere/elasticjob/lite/spring/boot/job/fixture/listener/NoopElasticJobListener.java
@@ -0,0 +1,40 @@
+/*
+ * 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.boot.job.fixture.listener;
+
+import org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener;
+import org.apache.shardingsphere.elasticjob.infra.listener.ShardingContexts;
+
+/**
+ * No operation elastic job listener.
+ */
+public final class NoopElasticJobListener implements ElasticJobListener {
+    
+    @Override
+    public void beforeJobExecuted(final ShardingContexts shardingContexts) {
+    }
+    
+    @Override
+    public void afterJobExecuted(final ShardingContexts shardingContexts) {
+    }
+    
+    @Override
+    public String getType() {
+        return "NOOP";
+    }
+}
diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener
similarity index 54%
copy from elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml
copy to elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener
index fccac3f..387e63b 100644
--- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/META-INF/services/org.apache.shardingsphere.elasticjob.infra.listener.ElasticJobListener
@@ -15,27 +15,5 @@
 # limitations under the License.
 #
 
-spring:
-  datasource:
-    url: jdbc:h2:mem:job_event_storage
-    driver-class-name: org.h2.Driver
-    username: sa
-    password:
-
-elasticjob:
-  tracing:
-    type: RDB
-  regCenter:
-    serverLists: localhost:18181
-    namespace: elasticjob-lite-spring-boot-starter
-  jobs:
-    customTestJob:
-      elasticJobClass: org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.job.impl.CustomTestJob
-      jobBootstrapBeanName: customTestJobBean
-      shardingTotalCount: 3
-    printTestJob:
-      elasticJobType: PRINT
-      jobBootstrapBeanName: printTestJobBean
-      shardingTotalCount: 3
-      props:
-        print.content: "test print job"
+org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.listener.NoopElasticJobListener
+org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.listener.LogElasticJobListener
diff --git a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml
index fccac3f..1b058a4 100644
--- a/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml
+++ b/elasticjob-lite/elasticjob-lite-spring/elasticjob-lite-spring-boot-starter/src/test/resources/application-elasticjob.yml
@@ -33,6 +33,9 @@ elasticjob:
       elasticJobClass: org.apache.shardingsphere.elasticjob.lite.spring.boot.job.fixture.job.impl.CustomTestJob
       jobBootstrapBeanName: customTestJobBean
       shardingTotalCount: 3
+      jobListenerTypes:
+        - NOOP
+        - LOG
     printTestJob:
       elasticJobType: PRINT
       jobBootstrapBeanName: printTestJobBean