springmvc maven 项目接入《分布式任务调度平台XXL-JOB》

1、首先需要部署调度中心,具体参考《分布式任务调度平台XXL-JOB》

2、在已有的spring项目pom.xml中引入

<!-- xxl-job-core -->
<dependency>
 <groupId>com.xuxueli</groupId>
 <artifactId>xxl-job-core</artifactId>
 <version>2.0.1</version>
</dependency>

注意:2.0.1为与调度中心适配版本

3、 在配置目录下增加applicationContext-xxl-job.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 扫描路径 -->
 <context:component-scan base-package="test" />
​
 <!-- 配置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></pre>

4、在classpath下添加xxl-job-executor.properties文件

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

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

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

5、在执行类中添加注入代码

@JobHandler(value="demoJobHandler")
@Component
public class DemoJobHandler extends IJobHandler {
​
 @Override
 public ReturnT<String> execute(String param) throws Exception {

 XxlJobLogger.log("XXL-JOB, Hello World.");
 System.out.println("cti"+param);
 for (int i = 0; i < 5; i++) {
 XxlJobLogger.log("beat at:" + i);
 TimeUnit.SECONDS.sleep(2);
 }
 return SUCCESS;
 }

}

其中"demoJobHandler"为调度中心任务管理设置JobHandler=demoJobHandler

6、在调度中心设置对应的任务调度信息;

  • 执行器管理,新增对应的应用信息 AppName:为配置xxl-job-executor.properties文件中(xxl.job.executor.appname=xxl-job-executor-cti) 然后注入方式一般为自动注入,如果出现需要固定ip时候,需要手动注入ip带端口
    TIM图片20190826135854.png
  • 任务管理,新增执行任务


    TIM图片20190826140309.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容