springboot集成xxljob

发布时间 2023-08-17 14:47:34作者: 品书读茶

1、导包

compile 'com.xxl.job:xxl-job-core:1.8.0'

2、添加配置项

#任务调度xxl-job
xxl.job.admin.addresses = http://10.111.222.111:8081/xxl-job-admin-1.8.0-SNAPSHOT/
xxl.job.executor.appname = ${spring.application.name}
xxl.job.executor.ip =
xxl.job.executor.port = 2280
xxl.job.executor.logpath = /data/logs/${spring.application.name}/xxl/

3、添加配置类

package com.xxxx.xxxx.xxxxx.config;

import com.xxl.job.core.executor.XxlJobExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * xxl-job config inject
 * @author 
 */
@Configuration
public class XxlJobConfig {
    private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);

    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;
    @Value("${xxl.job.executor.appname}")
    private String appName;
    @Value("${xxl.job.executor.ip}")
    private String ip;
    @Value("${xxl.job.executor.port}")
    private int port;
    @Value("${xxl.job.executor.logpath}")
    private String logPath;

    @Bean(initMethod = "start", destroyMethod = "destroy")
    public XxlJobExecutor xxlJobExecutor() {
        logger.info(">>>>>>>>>>> xxl-job config init.<<<<<<<<<<<");
        XxlJobExecutor xxlJobExecutor = new XxlJobExecutor();
        xxlJobExecutor.setAdminAddresses(adminAddresses);
        xxlJobExecutor.setAppName(appName);
        xxlJobExecutor.setIp(ip);
        xxlJobExecutor.setPort(port);
        xxlJobExecutor.setLogPath(logPath);
        return xxlJobExecutor;
    }

}

4、可能出现的问题

4.1、NotFound org.eclipse.jetty.server.Server

image
解决方案:引入

compile 'org.eclipse.jetty:jetty-servlet:9.2.26.v20180806'

4.2、NotFound org.codehaus.jackson.map.ObjectMapper

image
解决方案:引入

compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'

全部相关包(仅参考)

compile 'com.xxl.job:xxl-job-core:1.8.0'
compile 'org.eclipse.jetty:jetty-servlet:9.2.26.v20180806'
compile 'org.eclipse.jetty:jetty-server:9.2.26.v20180806'
compile 'org.eclipse.jetty:jetty-util:9.2.26.v20180806'
compile 'org.eclipse.jetty:jetty-http:9.2.26.v20180806'
compile 'org.eclipse.jetty:jetty-io:9.2.26.v20180806'
compile 'org.eclipse.jetty:jetty-client:9.2.26.v20180806'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'com.caucho:hessian:4.0.52'