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/19 08:33:11 UTC

[shardingsphere-elasticjob] branch master updated: Update example for spring boot starter (#1164)

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 39c2e8c  Update example for spring boot starter (#1164)
39c2e8c is described below

commit 39c2e8c2e0d5964b621491a281e5ccfe463a5287
Author: 吴伟杰 <ro...@me.com>
AuthorDate: Sun Jul 19 16:33:02 2020 +0800

    Update example for spring boot starter (#1164)
    
    - Show usage of OneOffJob.
    - Replace System.out with Logger.
---
 .../elasticjob-example-lite-springboot/pom.xml     |  5 +++
 .../example/controller/OneOffJobController.java    | 39 ++++++++++++++++++++++
 .../lite/example/job/SpringBootDataflowJob.java    | 12 ++++---
 .../lite/example/job/SpringBootSimpleJob.java      |  8 +++--
 .../src/main/resources/application.yml             |  4 +++
 5 files changed, 62 insertions(+), 6 deletions(-)

diff --git a/examples/elasticjob-example-lite-springboot/pom.xml b/examples/elasticjob-example-lite-springboot/pom.xml
index 3d1f79c..69aee24 100644
--- a/examples/elasticjob-example-lite-springboot/pom.xml
+++ b/examples/elasticjob-example-lite-springboot/pom.xml
@@ -82,6 +82,11 @@
             <version>${springboot.version}</version>
         </dependency>
         <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <version>${springboot.version}</version>
+        </dependency>
+        <dependency>
             <groupId>com.h2database</groupId>
             <artifactId>h2</artifactId>
             <version>${h2.version}</version>
diff --git a/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/controller/OneOffJobController.java b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/controller/OneOffJobController.java
new file mode 100644
index 0000000..9f624b5
--- /dev/null
+++ b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/controller/OneOffJobController.java
@@ -0,0 +1,39 @@
+/*
+ * 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.example.controller;
+
+import org.apache.shardingsphere.elasticjob.lite.api.bootstrap.impl.OneOffJobBootstrap;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.annotation.Resource;
+
+@RestController
+@DependsOn("org.apache.shardingsphere.elasticjob.lite.boot.ElasticJobLiteAutoConfiguration")
+public class OneOffJobController {
+
+    @Resource(name = "manualScriptJobOneOffJobBootstrap")
+    private OneOffJobBootstrap manualScriptJob;
+
+    @GetMapping("/execute")
+    public String executeOneOffJob() {
+        manualScriptJob.execute();
+        return "{\"msg\":\"OK\"}";
+    }
+}
diff --git a/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootDataflowJob.java b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootDataflowJob.java
index 42a4acf..c7f5b61 100644
--- a/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootDataflowJob.java
+++ b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootDataflowJob.java
@@ -21,6 +21,8 @@ import org.apache.shardingsphere.elasticjob.api.ShardingContext;
 import org.apache.shardingsphere.elasticjob.dataflow.job.DataflowJob;
 import org.apache.shardingsphere.elasticjob.lite.example.entity.Foo;
 import org.apache.shardingsphere.elasticjob.lite.example.repository.FooRepository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
@@ -31,20 +33,22 @@ import java.util.List;
 @Component
 public class SpringBootDataflowJob implements DataflowJob<Foo> {
 
+    private final Logger logger = LoggerFactory.getLogger(SpringBootDataflowJob.class);
+
     @Resource
     private FooRepository fooRepository;
 
     @Override
     public List<Foo> fetchData(final ShardingContext shardingContext) {
-        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
-                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH"));
+        logger.info("Item: {} | Time: {} | Thread: {} | {}",
+                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW FETCH");
         return fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
     }
 
     @Override
     public void processData(final ShardingContext shardingContext, final List<Foo> data) {
-        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
-                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS"));
+        logger.info("Item: {} | Time: {} | Thread: {} | {}",
+                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "DATAFLOW PROCESS");
         for (Foo each : data) {
             fooRepository.setCompleted(each.getId());
         }
diff --git a/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootSimpleJob.java b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootSimpleJob.java
index ea36c58..ac6cdb1 100644
--- a/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootSimpleJob.java
+++ b/examples/elasticjob-example-lite-springboot/src/main/java/org/apache/shardingsphere/elasticjob/lite/example/job/SpringBootSimpleJob.java
@@ -21,6 +21,8 @@ import org.apache.shardingsphere.elasticjob.api.ShardingContext;
 import org.apache.shardingsphere.elasticjob.lite.example.entity.Foo;
 import org.apache.shardingsphere.elasticjob.lite.example.repository.FooRepository;
 import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -31,13 +33,15 @@ import java.util.List;
 @Component
 public class SpringBootSimpleJob implements SimpleJob {
 
+    private final Logger logger = LoggerFactory.getLogger(SpringBootSimpleJob.class);
+
     @Autowired
     private FooRepository fooRepository;
 
     @Override
     public void execute(ShardingContext shardingContext) {
-        System.out.println(String.format("Item: %s | Time: %s | Thread: %s | %s",
-                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE"));
+        logger.info("Item: {} | Time: {} | Thread: {} | {}",
+                shardingContext.getShardingItem(), new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getId(), "SIMPLE");
         List<Foo> data = fooRepository.findTodoData(shardingContext.getShardingParameter(), 10);
         for (Foo each : data) {
             fooRepository.setCompleted(each.getId());
diff --git a/examples/elasticjob-example-lite-springboot/src/main/resources/application.yml b/examples/elasticjob-example-lite-springboot/src/main/resources/application.yml
index 423ca2e..8512249 100644
--- a/examples/elasticjob-example-lite-springboot/src/main/resources/application.yml
+++ b/examples/elasticjob-example-lite-springboot/src/main/resources/application.yml
@@ -27,3 +27,7 @@ elasticjob:
           shardingTotalCount: 3
           props:
             script.command.line: "echo SCRIPT Job: "
+        - jobName: manualScriptJob
+          shardingTotalCount: 9
+          props:
+            script.command.line: "echo Manual SCRIPT Job: "