XXL-JOB

Official document address

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

Configure and deploy the dispatch center and executor project according to the official document

Routing Policy: Polling Demo

Prerequisite: One dispatch center, two executor projects
The first actuator project application.properties configuration (each actuator is a separate springboot project)
# web port 应用的端口
server.port=8082
# 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:8088/xxl-job-admin
#xxl.job.admin.addresses=http://190.168.1.40:8088/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### 执行器的Appname
xxl.job.executor.appname=xxljob-clearfolder
### xxl-job executor registry-address: default use address to registry , otherwise use ip:port if address is null
xxl.job.executor.address=
### 执行器ip(可自动或手动录入)
xxl.job.executor.ip=
# 执行器内部端口 rpc 内部调用端口
xxl.job.executor.port=8182
### 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
The second executor project application.properties configuration
# web port
server.port=8083
# 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:8088/xxl-job-admin
#xxl.job.admin.addresses=http://190.168.1.40:8088/xxl-job-admin
### xxl-job, access token
xxl.job.accessToken=default_token
### xxl-job executor appname
#xxl.job.executor.appname=xxl-job-executor-sample
xxl.job.executor.appname=xxljob-clearfolder
### 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.port=8183
### 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
The point is that the respective server.port and xxl.job.executor.port remain different, and the xxl.job.executor.appname remains the same.
You can view the results after adding executors in the dispatch center

insert image description here

Add new task in task management, select polling mode

insert image description here

Define pollingTask in two executor projects
/**
 * @Description:
 * @Author: k23
 * @Date: 2022-10-08 15:30
 * @Version: 1.0
 */
@Component
public class MyJobHandler {
    
    
    @XxlJob("pollingTask")
    public ReturnT pollingTask(){
    
    
        System.out.println("演示调度策略轮询执行器");
        return ReturnT.SUCCESS;
    }
}
View the output order after execution
The same goes for the first, last, and random routing strategies

Parent-child task (just fill in the sub-task id in the corresponding place)

insert image description here

Dynamic parameter task (pass file path on the web side, delete the specified file path on the server side small case)

@Component
public class MyJobHandler {
    
    
    static int flag = 1;//用来判断文件是否删除成功
    @XxlJob("ClearFolder")//清空文件夹任务
    public void ClearFolder() throws Exception {
    
    
        String param = XxlJobHelper.getJobParam(); // 获取参数
        String[] methodParams = param.split(",");
        if (methodParams.length ==1){
    
    
            XxlJobHelper.handleFail("参数不能为空");
            return;
        }
        //删除一个文件夹下的所有文件(包括子目录内的文件)
        //File file = new File("E:\\testkk");
        String path = methodParams[0];
        File file = new File(path);
        //判断文件为null或文件目录存在
        if (!file.exists()){
    
    
            flag = 0;
            XxlJobHelper.handleFail("文件删除失败,文件夹不存在");
            return;
        }
        File[] listFiles = file.listFiles();
        if (listFiles.length ==0){
    
    
            flag = 0;
            XxlJobHelper.handleFail("文件删除失败,文件夹为空");
            return;
        }
        deleteFile(file);
        if (flag == 1){
    
    
            XxlJobHelper.handleSuccess("文件删除成功");
            return;
        }
    }

    public static void deleteFile(File file){
    
    
        //取得这个目录下的所有子文件对象
        File[] files = file.listFiles();
        //遍历该目录下的文件对象
        for (File f: files){
    
    
            //打印文件名
            String name = f.getName();
            System.out.println(name);
            Date d=new Date(f.lastModified());
            DateFormat df=new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
            String format = df.format(d);
            System.out.println(format);
            //判断子目录是否存在子目录,如果是文件则删除
            if (f.isDirectory()){
    
    
                deleteFile(f);
            }else {
    
    
                f.delete();
            }
        }
        //删除空文件夹  for循环已经把上一层节点的目录清空。
       // file.delete();
    }
}

Guess you like

Origin blog.csdn.net/weixin_42436236/article/details/127948305