当前位置:   article > 正文

xxl-job-admin2.3远程调用APi_xxl-job 登录接口远程调用

xxl-job 登录接口远程调用

xxl-job-admin2.3远程调用APi

本文主要描述xxl-job的接口调用实现对job动态增删改查。

代码结构

在这里插入图片描述

一、pom引入

  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.3</version>
    </parent>

    <properties>
        <!-- 项目源码及编译输出的编码 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <!-- 项目编译JDK版本 -->
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.xuxueli/xxl-job-core -->
        <dependency>
            <groupId>com.xuxueli</groupId>
            <artifactId>xxl-job-core</artifactId>
            <version>2.3.0</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.6.3</version>
        </dependency>
    </dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

二、配置文件

# web port
server.port=8088
# no web
#spring.main.web-environment=false

### xxl-job admin address list, such as "http://address" or "http://address01,http://address02"
xxl.job.admin.addresses=http://127.0.0.1:8080/xxl-job-admin

### xxl-job, access token
xxl.job.accessToken=

### xxl-job executor appname
xxl.job.executor.appname=xxl-job-executor-sample
### 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=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

#XxlJobTemplate相关新增
xxl.job.admin.username=admin
xxl.job.admin.password=123456

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

三、相关实体类

import lombok.Data;
import lombok.experimental.Accessors;

/**
 * 添加Job
 */
@Data
@Accessors(chain = true)
public class AddXxlJob {
    /**
     * 执行器主键ID
     */
    private int jobGroup;
    /**
     * job描述
     */
    private String jobDesc;
    /**
     * 负责人
     */
    private String author;
    /**
     * 调度类型
     */
    private String scheduleType;
    /**
     * 调度配置,值含义取决于调度类型
     */
    private String scheduleConf;
    /**
     * 执行器,任务Handler名称
     */
    private String executorHandler;
    /**
     * 任务参数
     */
    private String executorParam = "";
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
/**
 * 修改Job
 */
@Data
@Accessors(chain = true)
public class UpdateXxlJob {
    /**
     * 任务id
     */
    private int id;
    /**
     * 执行器主键ID
     */
    private int jobGroup;
    /**
     * job描述
     */
    private String jobDesc;
    /**
     * 负责人
     */
    private String author;
    /**
     * 调度类型
     */
    private String scheduleType;
    /**
     * 调度配置,值含义取决于调度类型
     */
    private String scheduleConf;
    /**
     * 执行器,任务Handler名称
     */
    private String executorHandler;
    /**
     * 调度过期策略
     */
    private String misfireStrategy;
    /**
     * 执行器路由策略
     */
    private String executorRouteStrategy;
    /**
     * 执行器,任务参数
     */
    private String executorParam;
    /**
     * 阻塞处理策略
     */
    private String executorBlockStrategy;

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
/**
 * Created by xuxueli on 16/9/30.
 */
@Data
public class XxlJobGroup {

    private int id;
    private String appname;
    private String title;
    private int addressType;        // 执行器地址类型:0=自动注册、1=手动录入
    private String addressList;     // 执行器地址列表,多地址逗号分隔(手动录入)
    private Date updateTime;

    // registry list
    private List<String> registryList;  // 执行器地址列表(系统注册)

    public List<String> getRegistryList() {
        if (addressList != null && addressList.trim().length() > 0) {
            registryList = new ArrayList<String>(Arrays.asList(addressList.split(",")));
        }
        return registryList;
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
/**
 * xxl-job info
 *
 * @author xuxueli  2016-1-12 18:25:49
 */
@Data
public class XxlJobInfo {
	
	private int id;				// 主键ID
	
	private int jobGroup;		// 执行器主键ID
	private String jobDesc;
	
	private Date addTime;
	private Date updateTime;
	
	private String author;		// 负责人
	private String alarmEmail;	// 报警邮件

	private String scheduleType;			// 调度类型
	private String scheduleConf;			// 调度配置,值含义取决于调度类型
	private String misfireStrategy;			// 调度过期策略

	private String executorRouteStrategy;	// 执行器路由策略
	private String executorHandler;		    // 执行器,任务Handler名称
	private String executorParam;		    // 执行器,任务参数
	private String executorBlockStrategy;	// 阻塞处理策略
	private int executorTimeout;     		// 任务执行超时时间,单位秒
	private int executorFailRetryCount;		// 失败重试次数
	
	private String glueType;		// GLUE类型	#com.xxl.job.core.glue.GlueTypeEnum
	private String glueSource;		// GLUE源代码
	private String glueRemark;		// GLUE备注
	private Date glueUpdatetime;	// GLUE更新时间

	private String childJobId;		// 子任务ID,多个逗号分隔

	private int triggerStatus;		// 调度状态:0-停止,1-运行
	private long triggerLastTime;	// 上次调度时间
	private long triggerNextTime;	// 下次调度时间
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

枚举

@AllArgsConstructor
@Getter
public enum XxlJobPathEnum {
    LOGIN("/login", "登录"),
    ADD("/jobinfo/add", "添加Job"),
    UPDATE("/jobinfo/update", "更新Job"),
    REMOVE("/jobinfo/remove", "删除Job"),
    PAGE_JOB("/jobinfo/pageList", "查询Job"),
    PAGE_GROUP("/jobgroup/pageList", "查询Job组");

    private String path;
    private String desc;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

四、工具类


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpStatus;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xxl.job.admin.dto.AddXxlJob;
import com.xxl.job.admin.dto.UpdateXxlJob;
import com.xxl.job.admin.dto.XxlJobGroup;
import com.xxl.job.admin.dto.XxlJobInfo;
import com.xxl.job.admin.enums.XxlJobPathEnum;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class XxlJobTemplate {
    @Value("${xxl.job.admin.addresses}")
    private String addresses;

    @Value("${xxl.job.admin.username}")
    private String username;

    @Value("${xxl.job.admin.password}")
    private String password;

    private String cookie;

    /**
     * 查询执行器
     *
     * @param appname
     * @param title
     * @return
     */
    public List<XxlJobGroup> listGroup(String appname, String title) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("appname", appname);
        paramMap.put("title", title);

        JSONObject pageGroup = doRequest(XxlJobPathEnum.PAGE_GROUP, paramMap);

        List<XxlJobGroup> jobGroupList = JSONUtil.toList(pageGroup.getJSONArray("data"), XxlJobGroup.class);
        return jobGroupList;
    }

    /**
     * 查询job列表
     *
     * @param jobGroup        -1
     * @param triggerStatus   -1
     * @param jobDesc
     * @param executorHandler
     * @param author
     * @return
     */
    public List<XxlJobInfo> listJob(int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("jobGroup", jobGroup);
        paramMap.put("triggerStatus", triggerStatus);
        paramMap.put("jobDesc", jobDesc);
        paramMap.put("executorHandler", executorHandler);
        paramMap.put("author", author);

        JSONObject pageJob = doRequest(XxlJobPathEnum.PAGE_JOB, paramMap);

        List<XxlJobInfo> jobList = JSONUtil.toList(pageJob.getJSONArray("data"), XxlJobInfo.class);
        return jobList;
    }

    /**
     * 条件查询一个Job
     *
     * @param jobGroup        -1
     * @param triggerStatus   -1
     * @param jobDesc
     * @param executorHandler
     * @param author
     * @return
     */
    public XxlJobInfo getOneJob(int jobGroup, int triggerStatus, String jobDesc, String executorHandler, String author) {
        List<XxlJobInfo> xxlJobInfos = listJob(jobGroup, triggerStatus, jobDesc, executorHandler, author);

        if (xxlJobInfos.size() > 1) {
            throw new RuntimeException(String.format("xxl-job-admin任务查询结果不唯一:%s", xxlJobInfos.toString()));
        }

        return xxlJobInfos.isEmpty() ? null : xxlJobInfos.get(0);
    }

    /**
     * 添加job
     *
     * @param addJob
     * @return
     */
    public Integer addJob(AddXxlJob addJob) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("jobGroup", addJob.getJobGroup());
        paramMap.put("jobDesc", addJob.getJobDesc());
        paramMap.put("author", addJob.getAuthor());
        paramMap.put("scheduleType", addJob.getScheduleType());
        paramMap.put("scheduleConf", addJob.getScheduleConf());
        paramMap.put("misfireStrategy", "DO_NOTHING");
        paramMap.put("executorRouteStrategy", "FIRST");
        paramMap.put("executorHandler", addJob.getExecutorHandler());
        //paramMap.put("executorParam", "");
        paramMap.put("executorBlockStrategy", "SERIAL_EXECUTION");

        paramMap.put("executorTimeout", 0);
        paramMap.put("executorFailRetryCount", 0);
        paramMap.put("glueType", "BEAN");
        paramMap.put("glueSource", "");
        paramMap.put("glueRemark", "GLUE代码初始化");
        paramMap.put("glueUpdatetime", null);
        paramMap.put("childJobId", "");
        paramMap.put("triggerStatus", 0);
        paramMap.put("triggerLastTime", 0);
        paramMap.put("triggerNextTime", 0);

        JSONObject result = doRequest(XxlJobPathEnum.ADD, paramMap);

        return result.getInt("content");
    }

    /**
     * 更新job
     *
     * @param updateXxlJob
     */
    public void updateJob(UpdateXxlJob updateXxlJob) {
        updateJob(JSONUtil.parseObj(updateXxlJob));
    }

    private void updateJob(Map<String, Object> paramMap) {
        JSONObject result = doRequest(XxlJobPathEnum.UPDATE, paramMap);

        if (HttpStatus.HTTP_OK != result.getInt("code")) {
            throw new RuntimeException(String.format("xxl-job-admin更新Job失败:%s", result.getStr("msg")));
        }
    }

    /**
     * 删除job
     *
     * @param jobId
     */
    public void removeJob(int jobId) {
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", jobId);

        doRequest(XxlJobPathEnum.REMOVE, paramMap);
    }

    /**
     * 获取登录cookie
     *
     * @return
     */
    private String getCookie() {
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.put("userName", username);
        paramsMap.put("password", password);
        HttpResponse response = HttpRequest.post(String.format("%s%s", addresses, XxlJobPathEnum.LOGIN.getPath()))
                .form(paramsMap).execute();
        if (HttpStatus.HTTP_OK != response.getStatus()) {
            throw new RuntimeException(String.format("xxl-job-admin登录失败:statusCode=%s", response.getStatus()));
        }

        List<HttpCookie> cookies = response.getCookies();

        if (cookies.isEmpty()) {
            throw new RuntimeException(String.format("xxl-job-admin登录失败:[userName=%s,password=%s]", username, password));
        }

        return cookies.stream().map(cookie -> cookie.toString()).collect(Collectors.joining());
    }

    /**
     * 远程调用xxl-job-admin
     *
     * @param xxlJobPathEnum
     * @param paramMap
     * @return
     */
    private JSONObject doRequest(XxlJobPathEnum xxlJobPathEnum, Map<String, Object> paramMap) {
        if (StrUtil.isBlank(cookie)) {
            cookie = getCookie();
        }

        HttpResponse response = HttpRequest.post(String.format("%s%s", addresses, xxlJobPathEnum.getPath()))
                .cookie(cookie).form(paramMap).execute();
        if (HttpStatus.HTTP_OK != response.getStatus()) {
            throw new RuntimeException(String.format("xxl-job-admin%s请求失败:statusCode=%s",
                    xxlJobPathEnum.getDesc(), response.getStatus()));
        }

        JSONObject result = JSONUtil.parseObj(response.body());

        Integer code = result.getInt("code");
        if (code != null && HttpStatus.HTTP_OK != code) {
            throw new RuntimeException(String.format("xxl-job-admin%s失败:msg=%s", xxlJobPathEnum.getDesc(), result.getStr("msg")));
        }

        return result;
    }

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215

五、测试类


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.xxl.job.admin.dto.AddXxlJob;
import com.xxl.job.admin.dto.UpdateXxlJob;
import com.xxl.job.admin.dto.XxlJobGroup;
import com.xxl.job.admin.dto.XxlJobInfo;
import com.xxl.job.admin.template.XxlJobTemplate;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class TestXxlJobTemplate {

    @Autowired
    private XxlJobTemplate xxlJobTemplate;

    @Test
    public void pageGroupTest() {
        String appname = "";
        String title = "";
        List<XxlJobGroup> xxlJobGroups = xxlJobTemplate.listGroup(appname, title);
        System.out.println(xxlJobGroups);
    }

    @Test
    public void pageJobTest() {
        int jobGroup = 2;
        int triggerStatus = -1;
        String jobDesc = "";
        String executorHandler = "";
        String author = "";
        List<XxlJobInfo> xxlJobInfos = xxlJobTemplate.listJob(jobGroup, triggerStatus, jobDesc, executorHandler, author);
        System.out.println(xxlJobInfos);
    }

    @Test
    public void addJobTest() {
        AddXxlJob addXxlJob = new AddXxlJob()
                .setJobGroup(2)
                .setJobDesc("测试任务2")
                .setAuthor("dunyage")
                .setScheduleType("CRON")
                .setScheduleConf("0/8 * * * * ?")
                .setExecutorHandler("demo1JobHandler");
        Integer jobId = xxlJobTemplate.addJob(addXxlJob);
        System.out.println("添加成功");
        System.out.println(jobId);
    }

    @Test
    public void removeJobTest() {
        xxlJobTemplate.removeJob(3);
        System.out.println("删除成功");
    }

    @Test
    public void updatejobTest() {
        String beanName = "demo1JobHandler";
        XxlJobInfo xxlJobInfo = xxlJobTemplate.getOneJob(-1, -1, "", beanName, "");

        Assert.notNull(xxlJobInfo, String.format("更新job失败:job[executorHandler=%s]不存在", beanName));

        String param = xxlJobInfo.getExecutorParam();

        UpdateXxlJob updateXxlJob = new UpdateXxlJob();
        BeanUtil.copyProperties(xxlJobInfo, updateXxlJob);

        //修改
        updateXxlJob.setScheduleType("CRON");
        updateXxlJob.setScheduleConf("0/8 * * * * ?");
        updateXxlJob.setExecutorParam("job参数");

        xxlJobTemplate.updateJob(updateXxlJob);
        System.out.println("更新成功");
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/96076
推荐阅读
相关标签
  

闽ICP备14008679号