當(dāng)前位置:首頁 > IT技術(shù) > Windows編程 > 正文

xxljob 基于api 添加job
2021-09-04 10:30:11

目前官方?jīng)]有直接提供類似執(zhí)行器那種能力,如果需要?jiǎng)討B(tài)創(chuàng)建job 有幾種解決方法

解決方法

  • 修改官方的admin 代碼暴露類似admin 的能力
  • 基于官方web的api 進(jìn)行操作
  • 直接基于數(shù)據(jù)庫操作(可行,而且更加簡單)

以下說明基于官方的web api 的操作說明

參考代碼

就有okhttp3,操作流程,獲取cookie,添加cookie 請求接口

package com.dalong;
?
import com.xxl.job.core.biz.model.ReturnT;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
?
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
?
public class XxlJobApi {
    public static OkHttpClient  client(){
        OkHttpClient client  =new  OkHttpClient().newBuilder().build();
        return  client;
    }
    public  static ReturnT<String> submitJob() {
        OkHttpClient client = client();
        /**
         *
         jobGroup: 1
         jobDesc: sss
         author: ssss
         alarmEmail:
         scheduleType: FIX_RATE
         scheduleConf: 111
         cronGen_display:
         schedule_conf_CRON:
         schedule_conf_FIX_RATE: 111
         schedule_conf_FIX_DELAY:
         glueType: BEAN
         executorHandler: demoJobHandler
         executorParam:
         executorRouteStrategy: FIRST
         childJobId:
         misfireStrategy: DO_NOTHING
         executorBlockStrategy: SERIAL_EXECUTION
         executorTimeout: 0
         executorFailRetryCount: 0
         glueRemark: GLUE代碼初始化
         glueSource:
         */
        // 基于map 包裝formbody, 這樣我們的數(shù)據(jù)是可以直接存儲在db中的(mongo是一個(gè)不錯(cuò)的選擇)
        Map<String,String> info = new HashMap<>();
        info.put("jobGroup","1");
        info.put("jobDesc","demo");
        info.put("cronGen_display","");
        info.put("glueType","BEAN");
        info.put("executorHandler","demoJobHandler");
        info.put("author","dalong");
        info.put("executorRouteStrategy","FIRST");
        info.put("triggerStatus","1");
        info.put("misfireStrategy","DO_NOTHING");
        info.put("executorBlockStrategy","SERIAL_EXECUTION");
        info.put("scheduleType","FIX_RATE");
        info.put("scheduleConf","11");
        FormBody.Builder builder = new FormBody.Builder();
?
       info.entrySet().forEach(new Consumer<Map.Entry<String, String>>() {
           @Override
           public void accept(Map.Entry<String, String> stringStringEntry) {
               builder.add(stringStringEntry.getKey(),stringStringEntry.getValue());
           }
       });
        try {
            FormBody formBody2 = new FormBody.Builder()
                    .add("userName", "<user>")
                    .add("password", "<password>")
                    .build();
         Request request=   new Request.Builder().post(formBody2).url("http://localhost/xxl-job-admin/login").build();
         Response response=   client.newCall(request).execute();
         String token = response.header("Set-Cookie"); 
        // 首先獲取token 基于cookie
        // 然后請求頭附帶cookie進(jìn)行操作
         Request request2= new  Request.Builder().addHeader("Cookie",token).post(builder.build()).url("http://localhost/xxl-job-admin/jobinfo/add").build();
         Response response2=    client.newCall(request2).execute();
         return new ReturnT<>(response2.body().string());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }
}

說明

以上代碼是基于okhttp3包裝的請求處理,改造官方的代碼也是一個(gè)不錯(cuò)的選擇,因?yàn)楣俜侥壳氨┞兜囊恍┠芰Σ皇翘貏e方便
直接進(jìn)行數(shù)據(jù)庫的操作也是一個(gè)不錯(cuò)的選擇(親測可行),我們直接使用jdbc 進(jìn)行包裝操作就可以了(直接復(fù)用官方的mybatis代碼就可以了)

?

本文摘自 :https://blog.51cto.com/r

開通會員,享受整站包年服務(wù)立即開通 >