xxl-job 任务调度中心(定时器)(v2.1.0)

1.运行任务调度中心

地址

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

下载好项目,doc文件夹中有sql,修改对应的application.properties配置

xxl-job 任务调度中心(定时器)(v2.1.0)

如上配置:运行地址

http://127.0.0.1:8090/xxl-job-admin/

2.项目配置

1.集成springboot

application.properties

### xxl-job 访问地址
xxl.job.admin.addresses=http://127.0.0.1:8090/xxl-job-admin
### xxl-job 执行器名称
xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.ip=
###    执行器注册端口号
xxl.job.executor.port=9091
### xxl-job, access token
xxl.job.accessToken=
### xxl-job 日志地址
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
### xxl-job 日志保留天数
xxl.job.executor.logretentiondays=-1

java配置类

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;

@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") public XxlJobSpringExecutor xxlJobExecutor() { logger.info(">>>>>>>>>>> xxl-job config init."); XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor(); xxlJobSpringExecutor.setAdminAddresses(adminAddresses); xxlJobSpringExecutor.setAppName(appName); xxlJobSpringExecutor.setIp(ip); xxlJobSpringExecutor.setPort(port); xxlJobSpringExecutor.setAccessToken(accessToken); xxlJobSpringExecutor.setLogPath(logPath); xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays); return xxlJobSpringExecutor; } /** * 针对多网卡、容器内部署等情况,可借助 "spring-cloud-commons" 提供的 "InetUtils" 组件灵活定制注册IP; * * 1、引入依赖: * <dependency> * <groupId>org.springframework.cloud</groupId> * <artifactId>spring-cloud-commons</artifactId> * <version>${version}</version> * </dependency> * * 2、配置文件,或者容器启动变量 * spring.cloud.inetutils.preferred-networks: ‘xxx.xxx.xxx.‘ * * 3、获取IP * String ip_ = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); */ }

调用

import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import org.springframework.stereotype.Component;

/**
 * @author fueen
 * 编写自己的定时任务Handler
 * 继承IJobHandler
 *
 */
@JobHandler(value = "MyJobHandler")
@Component
public class MyJobHandler extends IJobHandler {
    /**
     * 重写execute方法,里面执行相应的功能
     * @param param
     * @return
     * @throws Exception
     */
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        System.out.println("我们不能失去信仰");
        return SUCCESS;
    }
}

2.集成spring

1.application.properties同springboot一样

2.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="fileEncoding" value="utf-8" />
        <property name="locations">
            <list>
                <value>classpath*:xxl-job-executor.properties</value>
            </list>
        </property>
    </bean>

    <!-- ********************************* 基础配置 ********************************* -->

    <!-- 配置01、JobHandler 扫描路径 使用@JobHandler注解的包-->
    <context:component-scan base-package="com.xxl.job.executor.service.jobhandler" />

    <!-- 配置02、执行器 -->
    <bean id="xxlJobSpringExecutor" class="com.xxl.job.core.executor.impl.XxlJobSpringExecutor" init-method="start" destroy-method="destroy" >
        <!-- 执行器注册中心地址[选填],为空则关闭自动注册 -->
        <property name="adminAddresses" value="${xxl.job.admin.addresses}" />
        <!-- 执行器AppName[选填],为空则关闭自动注册 -->
        <property name="appName" value="${xxl.job.executor.appname}" />
        <!-- 执行器IP[选填],为空则自动获取 -->
        <property name="ip" value="${xxl.job.executor.ip}" />
        <!-- 执行器端口号[选填],小于等于0则自动获取 -->
        <property name="port" value="${xxl.job.executor.port}" />
        <!-- 访问令牌[选填],非空则进行匹配校验 -->
        <property name="accessToken" value="${xxl.job.accessToken}" />
        <!-- 执行器日志路径[选填],为空则使用默认路径 -->
        <property name="logPath" value="${xxl.job.executor.logpath}" />
        <!-- 日志保存天数[选填],值大于3时生效 -->
        <property name="logRetentionDays" value="${xxl.job.executor.logretentiondays}" />
    </bean>
</beans>

调用同springboot

开发步骤:
* 1、继承"IJobHandler":“com.xxl.job.core.handler.IJobHandler”;
* 2、注册到Spring容器:添加“@Component”注解,被Spring容器扫描为Bean实例;
* 3、注册到执行器工厂:添加“@JobHandler(value="自定义jobhandler名称")”注解,注解value值对应的是调度中心新建任务的JobHandler属性的值。
* 4、执行日志:需要通过 "XxlJobLogger.log" 打印执行日志;

xxl-job 上的配置

新建执行器 配置中的名称,自动注册(推荐)

xxl-job 任务调度中心(定时器)(v2.1.0)

xxl-job 任务调度中心(定时器)(v2.1.0)

新建任务  JobHandler处填写自己@JobHandler处的名字

xxl-job 任务调度中心(定时器)(v2.1.0)

保存之后,启动,然后执行一次,就可以查看日志了

xxl-job 任务调度中心(定时器)(v2.1.0)

xxl-job 任务调度中心(定时器)(v2.1.0)

 

xxl-job 任务调度中心(定时器)(v2.1.0)

上一篇:【Luogu P4406】「CQOI2005」三角形面积并


下一篇:写脚本