服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - Java教程 - SpringBoot集成ElaticJob定时器的实现代码

SpringBoot集成ElaticJob定时器的实现代码

2021-05-09 12:30面皮大师 Java教程

这篇文章主要介绍了SpringBoot集成ElaticJob定时器的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文介绍了springboot集成elaticjob定时器的实现代码,分享给大家,具体如下:

pom文件配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
     xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>com.example</groupid>
  <artifactid>demojob</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>demojob</name>
  <description>demo project for spring boot</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.2.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
 
 
    <!--elastic-job-->
    <dependency>
      <artifactid>elastic-job-common-core</artifactid>
      <groupid>com.dangdang</groupid>
      <version>2.1.5</version>
    </dependency>
    <dependency>
      <artifactid>elastic-job-lite-core</artifactid>
      <groupid>com.dangdang</groupid>
      <version>2.1.5</version>
    </dependency>
    <dependency>
      <artifactid>elastic-job-lite-spring</artifactid>
      <groupid>com.dangdang</groupid>
      <version>2.1.5</version>
    </dependency>
    <dependency>
      <artifactid>elastic-job-cloud-executor</artifactid>
      <groupid>com.dangdang</groupid>
      <version>2.1.5</version>
    </dependency>
 
 
    <!--mariadb-->
    <dependency>
      <groupid>org.mariadb.jdbc</groupid>
      <artifactid>mariadb-java-client</artifactid>
      <version>1.5.4</version>
    </dependency>
    <!--druid-->
    <dependency>
      <groupid>com.alibaba</groupid>
      <artifactid>druid-spring-boot-starter</artifactid>
      <version>1.1.9</version>
    </dependency>
    <!--mybatis plus-->
    <dependency>
      <groupid>com.baomidou</groupid>
      <artifactid>mybatisplus-spring-boot-starter</artifactid>
      <version>1.0.5</version>
    </dependency>
    <dependency>
      <groupid>com.baomidou</groupid>
      <artifactid>mybatis-plus</artifactid>
      <version>2.1.9</version>
    </dependency>
 
 
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

yaml文件配置(也可以用application.properties一样的)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 配置配置数据源
spring:
 datasource:
  type: com.alibaba.druid.pool.druiddatasource
  driver-class-name: org.mariadb.jdbc.driver
  name: elastic-job-event
  url: jdbc:mariadb://127.0.0.1:3306/elasticjob
  username: root
  password: 123456
  druid:
   validationquery: select 1
   initialsize: 10
   minidle: 10
   maxactive: 200
   minevictableidletimemillis: 180000
   testonborrow: false
   testwhileidle: true
   removeabandoned: true
   removeabandonedtimeout: 1800
   logabandoned: true
   poolpreparedstatements: true
   maxopenpreparedstatements: 100
 
# 配置zookeeper
regcenter:
 serverlist: localhost:2181
 namespace: hulk_order_task
 
# 配置定时器规则
simplejob:
 cron: 0/5 * * * * ?
 shardingtotalcount: 1
 shardingitemparameters: 0=1

开始写代码

registrycenterconfig

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.example.demojob.config;
 
import com.dangdang.ddframe.job.reg.zookeeper.zookeeperconfiguration;
import com.dangdang.ddframe.job.reg.zookeeper.zookeeperregistrycenter;
import org.springframework.beans.factory.annotation.value;
import org.springframework.boot.autoconfigure.condition.conditionalonexpression;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
 
/**
 * 注册中心配置
 * 用于注册和协调作业分布式行为的组件,目前仅支持zookeeper。
 * @author shudalei
 */
@configuration
@conditionalonexpression("'${regcenter.serverlist}'.length() > 0")
public class registrycenterconfig {
  @bean(initmethod = "init")
  public zookeeperregistrycenter regcenter(@value("${regcenter.serverlist}") final string serverlist,
                       @value("${regcenter.namespace}") final string namespace) {
    return new zookeeperregistrycenter(new zookeeperconfiguration(serverlist, namespace));
  }
}

jobeventconfig

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.example.demojob.config;
 
import com.dangdang.ddframe.job.event.jobeventconfiguration;
import com.dangdang.ddframe.job.event.rdb.jobeventrdbconfiguration;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
 
import javax.annotation.resource;
import javax.sql.datasource;
 
/**
 * 如果想把作业运行的内容写到db中,我们需要用到另一个构造器,
 * 同时定义自己的jobeventconfiguration,
 * 目前来说实现这个接口的只有一个类jobeventrdbconfiguration,
 * 通过这个可以将作业运行的痕迹进行持久化到db的操作。
 * @author shudalei
 */
@configuration
public class jobeventconfig {
  @resource
  private datasource datasource;
 
  @bean
  public jobeventconfiguration jobeventconfiguration() {
    return new jobeventrdbconfiguration(datasource);
  }
}

simplejobconfig

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.example.demojob.config;
 
import com.dangdang.ddframe.job.config.jobcoreconfiguration;
import com.dangdang.ddframe.job.config.simple.simplejobconfiguration;
import com.dangdang.ddframe.job.event.jobeventconfiguration;
import com.dangdang.ddframe.job.lite.api.jobscheduler;
import com.dangdang.ddframe.job.lite.config.litejobconfiguration;
import com.dangdang.ddframe.job.lite.spring.api.springjobscheduler;
import com.dangdang.ddframe.job.reg.zookeeper.zookeeperregistrycenter;
import com.example.demojob.job.testsimplejob;
import org.springframework.beans.factory.annotation.value;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
 
import javax.annotation.resource;
 
@configuration
public class simplejobconfig {
 
  /**
   * 注册中心
   */
  @resource
  private zookeeperregistrycenter regcenter;
  /**
   * job事件配置
   */
  @resource
  private jobeventconfiguration jobeventconfiguration;
 
  /**
   * 微信access token获取任务对象
   *
   */
  @resource
  private testsimplejob simplejob;
 
  /**
   *
   * @param cron 定时任务cron配置
   * @param shardingtotalcount 任务分片数
   * @param shardingitemparameters 任务分片参数
   * @return jobscheduler 任务调度器
   */
  @bean(initmethod = "init")
  public jobscheduler simplejobscheduler(@value("${simplejob.cron}") final string cron,
                      @value("${simplejob.shardingtotalcount}") final int shardingtotalcount,
                      @value("${simplejob.shardingitemparameters}") final string shardingitemparameters) {
    return new springjobscheduler(simplejob, regcenter,
        getlitejobconfiguration(simplejob.getclass(), cron, shardingtotalcount, shardingitemparameters),
        jobeventconfiguration);
  }
 
  /**
   *
   * @param jobclass 任务调度类
   * @param cron 定时任务cron配置
   * @param shardingtotalcount 任务分片数
   * @param shardingitemparameters 任务分片参数
   * @return litejobconfiguration 任务配置
   */
  private litejobconfiguration getlitejobconfiguration(final class<? extends com.dangdang.ddframe.job.api.simple.simplejob> jobclass, final string cron,
                             final int shardingtotalcount, final string shardingitemparameters) {
    return litejobconfiguration
        .newbuilder(
            new simplejobconfiguration(jobcoreconfiguration.newbuilder(jobclass.getname(), cron, shardingtotalcount)
                .shardingitemparameters(shardingitemparameters).build(), jobclass.getcanonicalname()))
        .overwrite(true).build();
  }
}

testsimplejob,定时器任务本身

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.example.demojob.job;
 
import com.dangdang.ddframe.job.api.shardingcontext;
import com.dangdang.ddframe.job.api.simple.simplejob;
import org.springframework.stereotype.component;
 
@component
public class testsimplejob implements simplejob {
 
  private int count;
  //任务就是每5秒执行一次控制台输出1,2,3……
  @override
  public void execute(shardingcontext shardingcontext) {
    count++;
    system.out.println("task " + count);
  }
}

最后在docker下安装 zookeeper

安装脚本compose文件如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
version: '2'
services:
 zookeeper01:
  image: zookeeper
  restart: always
  hostname: zookeeper01
  ports:
   - 2181:2181
  environment:
   zoo_my_id: 1
   zoo_servers: server.1=0.0.0.0:2888:3888 server.2=zookeeper02:2888:3888 server.3=zookeeper03:2888:3888
 zookeeper02:
  image: zookeeper
  restart: always
  hostname: zookeeper02
  ports:
   - 2182:2181
  environment:
   zoo_my_id: 2
   zoo_servers: server.1=zookeeper01:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zookeeper03:2888:3888
 zookeeper03:
  image: zookeeper
  restart: always
  hostname: zookeeper03
  ports:
   - 2183:2181
  environment:
   zoo_my_id: 3
   zoo_servers: server.1=zookeeper01:2888:3888 server.2=zookeeper02:2888:3888 server.3=0.0.0.0:2888:3888

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/62ebedc764b7

延伸 · 阅读

精彩推荐