You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by te...@apache.org on 2020/10/20 11:38:01 UTC

[shardingsphere-elasticjob] branch master updated: Add usage for java API (#1619)

This is an automated email from the ASF dual-hosted git repository.

technoboy 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 0e8fc56  Add usage for java API (#1619)
0e8fc56 is described below

commit 0e8fc56e5fdd8205809275b7d0bf2304b9772761
Author: Yanjie Zhou <zh...@aliyun.com>
AuthorDate: Tue Oct 20 19:37:50 2020 +0800

    Add usage for java API (#1619)
    
    * Add usage error-handler for java API document
    
    * Optimize usage error-handler for java API document
---
 .../elasticjob-lite/usage/job-api/java-api.cn.md   | 249 +++++++++++++++++++
 .../elasticjob-lite/usage/job-api/java-api.en.md   | 272 +++++++++++++++++++--
 2 files changed, 506 insertions(+), 15 deletions(-)

diff --git a/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.cn.md b/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.cn.md
index 7506d5b..22356c1 100644
--- a/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.cn.md
+++ b/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.cn.md
@@ -91,3 +91,252 @@ public class JobMain {
     }
 }
 ```
+
+
+## 配置错误处理策略
+
+使用 ElasticJob-Lite 过程中当作业发生异常后,可采用以下错误处理策略。
+
+| *错误处理策略名称*         | *说明*                            |  *是否内置* | *是否默认*| *是否需要额外配置* |
+| ----------------------- | --------------------------------- |  -------  |  --------|  -------------  |
+| 记录日志策略              | 记录作业异常日志,但不中断作业执行     |   是       |     是   |                 |
+| 抛出异常策略              | 抛出系统异常并中断作业执行            |   是       |         |                 |
+| 忽略异常策略              | 忽略系统异常且不中断作业执行          |   是       |          |                 |
+| 邮件通知策略              | 发送邮件消息通知,但不中断作业执行     |            |          |      是         |
+| 企业微信通知策略           | 发送企业微信消息通知,但不中断作业执行 |            |          |      是          |
+| 钉钉通知策略              | 发送钉钉消息通知,但不中断作业执行     |            |          |      是          |
+
+### 记录日志策略
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用记录日志策略
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("LOG").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用记录日志策略
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("LOG").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
+
+### 抛出异常策略
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用抛出异常策略
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("THROW").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用抛出异常策略
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("THROW").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
+
+
+### 忽略异常策略
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用忽略异常策略
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("IGNORE").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用忽略异常策略
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("IGNORE").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
+
+### 邮件通知策略
+
+请参考 [这里](/cn/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#邮件通知策略) 了解更多。
+
+Maven POM:
+```xml
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-email</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
+```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用邮件通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("EMAIL").build();
+        setEmailConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用邮件通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("EMAIL").build();
+        setEmailConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setEmailConfiguration(final JobConfiguration jobConfig) {
+        // 设置邮件的配置
+        jobConfig.getExtraConfigurations().add(new EmailConfiguration(
+                "host", 465, "username", "password", true, "Test elasticJob error message", "from@xxx.com", "to1@xxx.com,to2xxx.com", "cc@xxx.com", "bcc@xxx.com", false));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
+
+### 企业微信通知策略
+
+请参考 [这里](/cn/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#企业微信通知策略) 了解更多。
+
+Maven POM:
+```xml
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-wechat</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
+```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用企业微信通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("WECHAT").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用企业微信通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("WECHAT").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setWechatConfiguration(final JobConfiguration jobConfig) {
+        // 设置企业微信的配置
+        jobConfig.getExtraConfigurations().add(new WechatConfiguration("webhook", 3000, 5000));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
+
+
+### 钉钉通知策略
+
+请参考 [这里](/cn/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#钉钉通知策略) 了解更多。
+
+Maven POM:
+```xml
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-dingtalk</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
+```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  定时调度作业
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // 一次性调度作业
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // 创建定时作业配置, 并且使用企业微信通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("DINGTALK").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // 创建一次性作业配置, 并且使用企业微信通知策略
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("DINGTALK").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setDingtalkConfiguration(final JobConfiguration jobConfig) {
+        // 设置钉钉的配置
+        jobConfig.getExtraConfigurations().add(new DingtalkConfiguration("webhook", 
+                "keyword", "secret", 3000, 5000));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // 配置注册中心
+        ...
+    }
+}
+```
\ No newline at end of file
diff --git a/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.en.md b/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.en.md
index 21fe727..3c1125b 100644
--- a/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.en.md
+++ b/docs/content/user-manual/elasticjob-lite/usage/job-api/java-api.en.md
@@ -79,20 +79,262 @@ Please refer to [Operation Manual](/en/user-manual/elasticjob-lite/operation/dum
 
 The example below is how to configure spring namespace for open listener port to dump.
 
+```java
+public class JobMain {
+    
+    public static void main(final String[] args) {
+        SnapshotService snapshotService = new SnapshotService(regCenter, 9888).listen();
+    }
+    
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center
+    }
+}
+```
+
+## Configuration error handler strategy
+
+In the process of using ElasticJob-Lite, when the job is abnormal, the following error handling strategies can be used.
+
+| *Error handler strategy name*            | *Description*                                                 |  *Built-in*  | *Default*| *Extra config*   |
+| ---------------------------------------- | ------------------------------------------------------------- |  -------     |  --------|  --------------  |
+| Log Strategy                             | Log error and do not interrupt job                            |   Yes        |     Yes  |                  |
+| Throw Strategy                           | Throw system exception and interrupt job                      |   Yes        |          |                  |
+| Ignore Strategy                          | Ignore exception and do not interrupt job                     |   Yes        |          |                  |
+| Email Notification Strategy              | Send email message notification and do not interrupt job      |              |          |    Yes           |
+| Wechat Enterprise Notification Strategy  | Send wechat message notification and do not interrupt job     |              |          |    Yes           |
+| Dingtalk Notification Strategy           | Send dingtalk message notification and do not interrupt job   |              |          |    Yes           |
+
+### Log Strategy
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of log strategy
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("LOG").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of log strategy
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("LOG").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center
+        ...
+    }
+}
+```
+
+### Throw Strategy
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of throw strategy.
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("THROW").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of throw strategy
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("THROW").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center
+        ...
+    }
+}
+```
+
+
+### Ignore Strategy
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of ignore strategy.
+        return JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("IGNORE").build();
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of ignore strategy.
+        return JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("IGNORE").build();
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center.
+        ...
+    }
+}
+```
+
+### Email Notification Strategy
+
+Please refer to [here](/en/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#email-notification-strategy) for more details.
+
+Maven POM:
 ```xml
-<?xml version="1.0" encoding="UTF-8"?>
-<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
-                         ">
-    <!--Configure register center -->
-    <elasticjob:zookeeper id="regCenter" server-lists="yourhost:2181" namespace="dd-job" base-sleep-time-milliseconds="1000" max-sleep-time-milliseconds="3000" max-retries="3" />
-    
-    <!--Configure snapshot for dump service -->
-    <elasticjob:snapshot id="jobSnapshot" registry-center-ref="regCenter" dump-port="9999" />    
-</beans>
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-email</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
 ```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of email notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("EMAIL").build();
+        setEmailConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of email notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("EMAIL").build();
+        setEmailConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setEmailConfiguration(final JobConfiguration jobConfig) {
+        // Set the mail configuration.
+        jobConfig.getExtraConfigurations().add(new EmailConfiguration(
+                "host", 465, "username", "password", true, "Test elasticJob error message", "from@xxx.com", "to1@xxx.com,to2xxx.com", "cc@xxx.com", "bcc@xxx.com", false));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center.
+        ...
+    }
+}
+```
+
+### Wechat Enterprise Notification Strategy
+
+Please refer to [here](/en/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#wechat-enterprise-notification-strategy) for more details.
+
+Maven POM:
+```xml
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-wechat</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
+```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs.
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs.
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of wechat enterprise notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("WECHAT").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of wechat enterprise notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("WECHAT").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setWechatConfiguration(final JobConfiguration jobConfig) {
+        // Set the configuration for the enterprise wechat.
+        jobConfig.getExtraConfigurations().add(new WechatConfiguration("webhook", 3000, 5000));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center.
+        ...
+    }
+}
+```
+
+### Dingtalk Notification Strategy
+
+Please refer to [here](/en/user-manual/elasticjob-lite/configuration/built-in-strategy/error-handler/#dingtalk-notification-strategy) for more details.
+
+Maven POM:
+```xml
+<dependency>
+    <groupId>org.apache.shardingsphere.elasticjob</groupId>
+    <artifactId>elasticjob-error-handler-dingtalk</artifactId>
+    <version>${latest.release.version}</version>
+</dependency>
+```
+```java
+public class JobDemo {
+    
+    public static void main(String[] args) {
+        //  Scheduling Jobs.
+        new ScheduleJobBootstrap(createRegistryCenter(), new MyJob(), createScheduleJobConfiguration()).schedule();
+        // One-time Scheduling Jobs.
+        new OneOffJobBootstrap(createRegistryCenter(), new MyJob(), createOneOffJobConfiguration()).execute();
+    }
+    
+    private static JobConfiguration createScheduleJobConfiguration() {
+        // Create scheduling job configuration, and the use of wechat enterprise notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myScheduleJob", 3).cron("0/5 * * * * ?").jobErrorHandlerType("DINGTALK").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+
+    }
+
+    private static JobConfiguration createOneOffJobConfiguration() {
+        // Create one-time job configuration, and the use of wechat enterprise notification strategy.
+        JobConfiguration jobConfig = JobConfiguration.newBuilder("myOneOffJob", 3).jobErrorHandlerType("DINGTALK").build();
+        setWechatConfiguration(jobConfig);
+        return jobConfig;
+    }
+
+    private static void setDingtalkConfiguration(final JobConfiguration jobConfig) {
+        // Set the configuration of the dingtalk.
+        jobConfig.getExtraConfigurations().add(new DingtalkConfiguration("webhook", 
+                "keyword", "secret", 3000, 5000));
+    }
+
+    private static CoordinatorRegistryCenter createRegistryCenter() {
+        // create registry center.
+        ...
+    }
+}
+```
\ No newline at end of file