0%

XXLJOB使用

并发设计案例

XXLJOB使用

基础配置

  1. 导包

    1
    2
    3
    4
    5
    6
    <!--xxl jop定时任务-->
    <dependency>
    <groupId>com.xuxueli</groupId>
    <artifactId>xxl-job-core</artifactId>
    <version>2.3.0</version>
    </dependency>
  2. yml配置

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    spring:
    cloud:
    nacos:
    ...
    config:
    ...
    shared-configs[0]:
    data-id: xxl-job-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
    group: DEFAULT_GROUP
    refresh: true
  3. 执行器config

    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
    import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;

    @Slf4j
    @Configuration
    @ConditionalOnProperty(prefix = "xxl.job", matchIfMissing = false, name = "enabled", havingValue = "true")
    public class JobExecutorConfiguration {

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${xxl.job.accessToken}")
    private String accessToken;

    @Value("${xxl.job.executor.appname}")
    private String appName;

    @Value("${xxl.job.executor.address}")
    private String address;

    @Value("${xxl.job.executor.ip}")
    private String ip;

    @Value("${xxl.job.executor.port}")
    private int port;

    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")
    private int logRetentionDays;

    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
    log.info("[XxlJobSpringExecutor] config init");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
    xxlJobSpringExecutor.setAppname(appName);
    xxlJobSpringExecutor.setAddress(address);
    xxlJobSpringExecutor.setIp(ip);
    xxlJobSpringExecutor.setPort(port);
    xxlJobSpringExecutor.setAccessToken(accessToken);
    xxlJobSpringExecutor.setLogPath(logPath);
    xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
    return xxlJobSpringExecutor;
    }

    }

建任务

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
package com.sexytea.pss.task;


import com.sexytea.pss.importRece.ImportReceTaskServiceImpl;
import com.xxl.job.core.biz.model.ReturnT;

import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* 同步数据
*
* @author hxr
**/
@Slf4j
@Component
public class SyncToHdcBranchTask extends IJobHandler {
@Autowired
private ImportReceTaskServiceImpl importReceTaskService;

@XxlJob("syncToHdcBranchJob")
@Override
public ReturnT<String> execute(String s) throws Exception{
try {
long startTime = System.currentTimeMillis();
log.info("execute SyncToHdcBranch begin");
importReceTaskService.triggerGetNoteAndGenerateRece();
log.info("execute SyncToHdcBranch end. cost {}ms", (System.currentTimeMillis() - startTime));
}catch (Exception e){
log.error(e.getCause().getMessage());
return ReturnT.FAIL;
}
return ReturnT.SUCCESS;
}

}

XXLJOB页面配置

  1. 执行器

    img

  2. 任务

    img

参考

https://www.xuxueli.com/xxl-job/