springmvc项目接入xxl-job框架

所谓接入就是把自己业务系统改造成执行器,调用xxl-job-admin调度中心,下面就是整合步骤。

第一步:引入jar包

<!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
    <dependency>
      <groupId>com.xuxueli</groupId>
      <artifactId>xxl-job-core</artifactId>
      <version>2.1.2</version>
    </dependency>

第二步:把xxl-job执行器的xxl-job-executor.properties文件copy至项目的resources文件夹下

第三步:配置xxl-job-executor.properties文件

# 调度中心地址
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

#执行器"AppName"和地址信息配置:AppName执行器心跳注册分组依据;地址信息用于"调度中心请求并触发任务"和"执行器注册"。执行器默认端口为9999,执行器IP默认为空表示自动获取IP,多网卡时可手动设置指定IP,该IP不会绑定Host仅作为通讯实用。单机部署多个执行器时,注意要配置不同执行器端口;

xxl.job.executor.appname=task-demo02
xxl.job.executor.ip=
xxl.job.executor.port=9998
​
#执行器通讯TOKEN,非空时启用
xxl.job.accessToken=
#xxl-job log path执行器运行日志文件存储的磁盘位置,需要对该路径拥有读写权限
xxl.job.executor.logpath=/data/applogs/xxl-job/jobhandler
#xxl-job log retention days执行器Log文件定期清理功能,指定日志保存天数,日志文件过期自动删除。限制至少保持3天,否则功能不生效;
xxl.job.executor.logretentiondays=30

第四步:配置applicationContext.xml文件

applicationContext.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">


    <!-- 自动扫描装配 -->
    <context:component-scan base-package="com.taskDemo.service.jobhandler"/>

    <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>

    <!-- 配置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>

第五步:载入配置文件,在项目中创建 XxlJobConfig.class 文件:

package com.taskDemo.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;

/**
 * xxl-job config
 *
 * @author xuxueli 2017-04-28
 */
@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
    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();
     */


}

第六步:创建任务JobHandler

package com.taskDemo.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;

/**
 * @Description 调度任务类
 * @Date Created in 下午8:19 2020/2/15
 * @Version 1.0
 */
@Component
public class TaskHandler extends IJobHandler {

    @XxlJob("taskDemoHandler")
    @Override
    public ReturnT<String> execute(String param) throws Exception {
        XxlJobLogger.log("接受参数" + param +"\n执行任务");
        return SUCCESS;
    }
}

第七步:在调度任务中心配置执行器

点击执行器管理-->新增执行器 注意AppName为执行器项目应用名,即上面参数xxl.job.executor.appname=task-demo02

第八步:创建任务

点击任务管理-->新增 执行器下拉列表选择刚新增的执行器 注意JobHandler为上面调度任务类@JobHandler(value="taskDemoHandler")中value值

到这里就整合完成了。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值