外部应用调用xxljob接口

package com.bochao.devmgr.job;
import com.bochao.devmgr.job.domain.XxlJobInfo;
import com.xxl.job.core.biz.model.ReturnT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.util.MultiValueMap;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.util.List;

/**
 * Date:2022/5/20
 *
 * @author:zrlie
 */
//@Lazy
@Component
public class JobUtil {


    @Qualifier("restTemplate")
    @Autowired
    private RestTemplate restTemplate;

    private static String cookie = "";

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

    /**
     * xxljob登录
     *
     * @return
     * @throws IOException
     */
    @PostConstruct
    public String loginXXLJob() {
        String path = adminAddresses + "/login";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("userName", "admin");
        map.add("password", "123456");
        map.add("ifRemember", "on");
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, null);
        ResponseEntity<ReturnT> entity = restTemplate.postForEntity(path, param, ReturnT.class);
        List<String> cookies = entity.getHeaders().get("Set-Cookie");
        StringBuilder tmpcookies = new StringBuilder();
        for (String c : cookies) {
            tmpcookies.append(c + ";");
        }
        cookie = tmpcookies.toString();
        return cookie;
    }

    /**
     * 新增任务
     *
     * @param xxlJobInfo
     * @return
     */
    public ReturnT addXXLJob(XxlJobInfo xxlJobInfo) {
        String path =  adminAddresses +"/jobinfo/add";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("jobGroup", xxlJobInfo.getJobGroup());
        map.add("jobDesc", xxlJobInfo.getJobDesc());
        map.add("author", xxlJobInfo.getAuthor());
        map.add("alarmEmail", xxlJobInfo.getAlarmEmail());
        map.add("scheduleType", xxlJobInfo.getScheduleType());
        map.add("scheduleConf", xxlJobInfo.getScheduleConf());
        map.add("cronGen_display", xxlJobInfo.getScheduleConf());
        map.add("schedule_conf_CRON", xxlJobInfo.getScheduleConf());
        map.add("schedule_conf_FIX_RATE", "");
        map.add("schedule_conf_FIX_DELAY", "");
        map.add("glueType", xxlJobInfo.getGlueType());
        map.add("executorHandler", xxlJobInfo.getExecutorHandler());
        map.add("executorParam", xxlJobInfo.getExecutorParam());
        map.add("executorRouteStrategy", xxlJobInfo.getExecutorRouteStrategy());
        map.add("childJobId", xxlJobInfo.getChildJobId());
        map.add("misfireStrategy", xxlJobInfo.getMisfireStrategy());
        map.add("executorBlockStrategy", xxlJobInfo.getExecutorBlockStrategy());
        map.add("executorTimeout", xxlJobInfo.getExecutorTimeout());
        map.add("executorFailRetryCount", xxlJobInfo.getExecutorFailRetryCount());
        map.add("glueRemark", xxlJobInfo.getGlueRemark());

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("cookie", cookie);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(path, param, ReturnT.class);
    }

    /**
     * 更新任务
     *
     * @param xxlJobInfo
     * @return
     */
    public ReturnT updateXXLJob(XxlJobInfo xxlJobInfo) {
        String path = adminAddresses +"/jobinfo/update";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", xxlJobInfo.getId());
        map.add("jobGroup", xxlJobInfo.getJobGroup());
        map.add("jobDesc", xxlJobInfo.getJobDesc());
        map.add("author", xxlJobInfo.getAuthor());
        map.add("alarmEmail", xxlJobInfo.getAlarmEmail());
        map.add("scheduleType", xxlJobInfo.getScheduleType());
        map.add("scheduleConf", xxlJobInfo.getScheduleConf());
        map.add("cronGen_display", xxlJobInfo.getScheduleConf());
        map.add("schedule_conf_CRON", xxlJobInfo.getScheduleConf());
        map.add("schedule_conf_FIX_RATE", "");
        map.add("schedule_conf_FIX_DELAY", "");
        map.add("glueType", xxlJobInfo.getGlueType());
        map.add("executorHandler", xxlJobInfo.getExecutorHandler());
        map.add("executorParam", xxlJobInfo.getExecutorParam());
        map.add("executorRouteStrategy", xxlJobInfo.getExecutorRouteStrategy());
        map.add("childJobId", xxlJobInfo.getChildJobId());
        map.add("misfireStrategy", xxlJobInfo.getMisfireStrategy());
        map.add("executorBlockStrategy", xxlJobInfo.getExecutorBlockStrategy());
        map.add("executorTimeout", xxlJobInfo.getExecutorTimeout());
        map.add("executorFailRetryCount", xxlJobInfo.getExecutorFailRetryCount());
        map.add("glueRemark", xxlJobInfo.getGlueRemark());

        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("cookie", cookie);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(path, param, ReturnT.class);
    }

    /**
     * 开启任务
     *
     * @param id
     * @return
     */
    public ReturnT startXXLJob(int id) {
        String path = adminAddresses +"/jobinfo/start";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("cookie", cookie);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(path, param, ReturnT.class);
    }

    /**
     * 停止任务
     *
     * @param id
     * @return
     */
    public ReturnT stopXXLJob(int id) {
        String path =  adminAddresses +"/jobinfo/stop";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("cookie", cookie);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(path, param, ReturnT.class);
    }


    /**
     * 删除任务
     *
     * @return
     */
    public ReturnT removeXXLJob(int id) {
        String path =  adminAddresses +"/jobinfo/remove";
        MultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
        map.add("id", id);
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("cookie", cookie);
        //构造实体对象
        HttpEntity<MultiValueMap<String, Object>> param = new HttpEntity<>(map, headers);
        return restTemplate.postForObject(path, param, ReturnT.class);
    }

}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值