xxljob 日志_分布式任务调度平台XXLJOB实战图文代码详解

        前段时间项目上需要用到任务调度,对比了Quartz的不足与XXL-JOB优缺点,整体上:XXL-JOB比Quartz更轻量化,更易上手,还具有可视化界面以及在任务调度、日志管理、弹性扩容缩容、执行器HA(分布式)、邮件报警、运行报表、全异步、跨平台、国际化、容器化、线程池隔离等方面更加人性化。

一、源码下载地址:

下载xxl-job源码

xxl-job源码地址:https://github.com/xuxueli/xxl-job

8a3f775bf53cd5ff0313b5900dcdd503.png

       只需要将数据库配置成自己的,然后生成jar包即可,XXL-JOB的管理器直接通过运行jar包就正常运行;例如:

ee43ed6792a965b4a931e73ef8f1db69.png

70c1367b2d0eea6920ee601f37dd3fc1.png

启动后效果图:

9ac552bfae477cd517005273139b460e.png

运行效果如下:

主页面:

c5822f7db8b414edc27577278bd75922.png

执行器:

b21d7a128e64c9d22a4659a2f1fd348e.png

调度日志:

5cc7ffe162f20f1c11cae1eaf89f46b6.png

用户名密码在用户(xxl_job_user)表中;

二、数据库表创建:

        基本支撑的表包括:

ee73a0c89d0d8e9083425e8b50c5c3b7.png

有需要建表语句的请关注公众号后留言即可;

三、集成到项目:

        需要本地工程修改的代码类以及配置文件:XxlJobConfig.java、SampleXxlJob.java、application.properties、logback.xml

核心代码如下:

XxlJobConfig.java(直接copy到工程即可)

package www.tonghao.com.xxl.job.executor.core.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;

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;

/**

 * Create by YSOcean

 */

@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.accessToken}")

    private String accessToken;

    @Value("${xxl.job.executor.logpath}")

    private String logPath;

    @Value("${xxl.job.executor.logretentiondays}")

    private int logRetentionDays;

//    @Bean(initMethod = "start", destroyMethod = "destroy")

    @Bean

    public XxlJobSpringExecutor xxlJobExecutor() {

        logger.info(">>>>>>>>>>> xxl-job config init.");

        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();

        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);

//        xxlJobSpringExecutor.setAddress(address);

        xxlJobSpringExecutor.setAppname(appName);

        xxlJobSpringExecutor.setIp(ip);

        xxlJobSpringExecutor.setPort(port);

        xxlJobSpringExecutor.setAccessToken(accessToken);

        xxlJobSpringExecutor.setLogPath(logPath);

        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);

        return xxlJobSpringExecutor;

    }

}

SampleXxlJob.java(调度执行类)

package www.tonghao.com.xxl.job.executor.service.jobhandler;

import com.xxl.job.core.biz.model.ReturnT;

import com.xxl.job.core.handler.IJobHandler;

import com.xxl.job.core.handler.annotation.XxlJob;

import com.xxl.job.core.log.XxlJobLogger;

import org.springframework.stereotype.Component;

import www.tonghao.common.utils.SpringUtil;

import www.tonghao.wc.service.WcinOrdersService;

import www.tonghao.wc.web.WcinOrderApiController;

/**

 * 任务Handler示例(Bean模式)

 *

 * 开发步骤:

 * 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;

 * 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;

 * 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。

 * 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;

 */

//@JobHandler(value="demoJobHandler")

@Component

public class SampleXxlJob extends IJobHandler{

@XxlJob("demoJobHandler") 

    @Override

    public ReturnT execute(String s) throws Exception {

        XxlJobLogger.log("XXL-JOB, Hello World.");

        System.out.println("This is example!");

       //更换即可--start

        WcinOrderApiController contr=new WcinOrderApiController();

        String msg=contr.getOrder();

        //更换即可--end

        return SUCCESS;

    }

}

logback.xml(直接copy到工程即可)

    logback

            %d{HH:mm:ss.SSS} %contextName [%thread] %-5level %logger{36} - %msg%n

        ${log.path}

            ${log.path}.%d{yyyy-MM-dd}.zip

            %date %level [%thread] %logger{36} [%file : %line] %msg%n

application.properties(核心配置)

# no web

#spring.main.web-environment=false

# log config

logging.config=classpath:logback.xml

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"

xxl.job.admin.addresses=http://127.0.0.1:6081/xxl-job-admin

#xxl.job.admin.addresses=http://11.0.38.229:8080/xxl-job-admin

### xxl-job, access token

xxl.job.accessToken=

### xxl-job executor appname

xxl.job.executor.appname=testgmm

### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null

xxl.job.executor.address=

### xxl-job executor server-info

xxl.job.executor.ip=

#xxl.job.executor.ip=127.0.0.1

xxl.job.executor.port=9999

### xxl-job executor log-path

xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler

### xxl-job executor log-retention-days

xxl.job.executor.logretentiondays=30

至此,XXL-JOB集成到项目,完成;

使用操作的详细配置,后续会更新,请持续关注!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
>