见官方文档:https://www.xuxueli.com/xxl-job/

spring boot引入

properties文件

  1. # web port
  2. server.port=8081
  3. # no web
  4. #spring.main.web-environment=false
  5. # log config
  6. logging.config=classpath:logback.xml
  7. ### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
  8. xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin
  9. ### xxl-job, access token
  10. xxl.job.accessToken=
  11. ### xxl-job executor appname
  12. xxl.job.executor.appname=xxl-job-executor-sample
  13. ### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
  14. xxl.job.executor.address=
  15. ### xxl-job executor server-info
  16. xxl.job.executor.ip=
  17. xxl.job.executor.port=9999
  18. ### xxl-job executor log-path
  19. xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
  20. ### xxl-job executor log-retention-days
  21. xxl.job.executor.logretentiondays=30

读取文件生成XxlJobSpringExecutor注入Spring

  1. @Configuration
  2. public class XxlJobConfig {
  3. private Logger logger = LoggerFactory.getLogger(XxlJobConfig.class);
  4. @Value("${xxl.job.admin.addresses}")
  5. private String adminAddresses;
  6. @Value("${xxl.job.accessToken}")
  7. private String accessToken;
  8. @Value("${xxl.job.executor.appname}")
  9. private String appname;
  10. @Value("${xxl.job.executor.address}")
  11. private String address;
  12. @Value("${xxl.job.executor.ip}")
  13. private String ip;
  14. @Value("${xxl.job.executor.port}")
  15. private int port;
  16. @Value("${xxl.job.executor.logpath}")
  17. private String logPath;
  18. @Value("${xxl.job.executor.logretentiondays}")
  19. private int logRetentionDays;
  20. @Bean
  21. public XxlJobSpringExecutor xxlJobExecutor() {
  22. logger.info(">>>>>>>>>>> xxl-job config init.");
  23. XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
  24. xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
  25. xxlJobSpringExecutor.setAppname(appname);
  26. xxlJobSpringExecutor.setAddress(address);
  27. xxlJobSpringExecutor.setIp(ip);
  28. xxlJobSpringExecutor.setPort(port);
  29. xxlJobSpringExecutor.setAccessToken(accessToken);
  30. xxlJobSpringExecutor.setLogPath(logPath);
  31. xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
  32. return xxlJobSpringExecutor;
  33. }

创建执行器

@Component
public class SampleXxlJob {
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);


    /**
     * 1、简单任务示例(Bean模式)
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }

}

spring 项目引入

public class FrameLessXxlJobConfig {
    private static Logger logger = LoggerFactory.getLogger(FrameLessXxlJobConfig.class);


    private static FrameLessXxlJobConfig instance = new FrameLessXxlJobConfig();
    public static FrameLessXxlJobConfig getInstance() {
        return instance;
    }


    private XxlJobSimpleExecutor xxlJobExecutor = null;

    /**
     * init
     */
    public void initXxlJobExecutor() {

        // load executor prop
        Properties xxlJobProp = loadProperties("xxl-job-executor.properties");

        // init executor
        xxlJobExecutor = new XxlJobSimpleExecutor();
        xxlJobExecutor.setAdminAddresses(xxlJobProp.getProperty("xxl.job.admin.addresses"));
        xxlJobExecutor.setAccessToken(xxlJobProp.getProperty("xxl.job.accessToken"));
        xxlJobExecutor.setAppname(xxlJobProp.getProperty("xxl.job.executor.appname"));
        xxlJobExecutor.setAddress(xxlJobProp.getProperty("xxl.job.executor.address"));
        xxlJobExecutor.setIp(xxlJobProp.getProperty("xxl.job.executor.ip"));
        xxlJobExecutor.setPort(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.port")));
        xxlJobExecutor.setLogPath(xxlJobProp.getProperty("xxl.job.executor.logpath"));
        xxlJobExecutor.setLogRetentionDays(Integer.valueOf(xxlJobProp.getProperty("xxl.job.executor.logretentiondays")));

        // registry job bean
        xxlJobExecutor.setXxlJobBeanList(Arrays.asList(new SampleXxlJob()));

        // start executor
        try {
            xxlJobExecutor.start();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}

    public static void main(String[] args) {

        try {
            // start
            FrameLessXxlJobConfig.getInstance().initXxlJobExecutor();

            // Blocks until interrupted
            while (true) {
                try {
                    TimeUnit.HOURS.sleep(1);
                } catch (InterruptedException e) {
                    break;
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            // destory
            FrameLessXxlJobConfig.getInstance().destoryXxlJobExecutor();
        }

    }
public class SampleXxlJob {
    private static Logger logger = LoggerFactory.getLogger(SampleXxlJob.class);


    /**
     * 1、简单任务示例(Bean模式)
     */
    @XxlJob("demoJobHandler")
    public void demoJobHandler() throws Exception {
        XxlJobHelper.log("XXL-JOB, Hello World.");

        for (int i = 0; i < 5; i++) {
            XxlJobHelper.log("beat at:" + i);
            TimeUnit.SECONDS.sleep(2);
        }
        // default success
    }