parent
66de2818ba
commit
8a6d462d3b
@ -0,0 +1,23 @@
|
|||||||
|
package com.xxl.job.dao.impl;
|
||||||
|
|
||||||
|
import com.xxl.job.core.biz.model.RegistryParam;
|
||||||
|
import com.xxl.job.core.biz.model.ReturnT;
|
||||||
|
import com.xxl.job.core.enums.RegistryConfig;
|
||||||
|
import com.xxl.job.core.util.AdminApiUtil;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by xuxueli on 17/5/10.
|
||||||
|
*/
|
||||||
|
public class AdminApiTest {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
try {
|
||||||
|
RegistryParam registryParam = new RegistryParam(RegistryConfig.RegistType.EXECUTOR.name(), "aaa", "112312312312");
|
||||||
|
ReturnT<String> registryResult = AdminApiUtil.callApi("http://localhost:8080/xxl-job-admin"+AdminApiUtil.REGISTRY, registryParam);
|
||||||
|
System.out.println(registryResult);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package com.xxl.job.core.biz;
|
|
||||||
|
|
||||||
import com.xxl.job.core.biz.model.HandleCallbackParam;
|
|
||||||
import com.xxl.job.core.biz.model.ReturnT;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by xuxueli on 17/3/1.
|
|
||||||
*/
|
|
||||||
public interface AdminBiz {
|
|
||||||
|
|
||||||
public ReturnT<String> callback(HandleCallbackParam handleCallbackParam);
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,54 @@
|
|||||||
|
package com.xxl.job.core.biz.model;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by xuxueli on 2017-05-10 20:22:42
|
||||||
|
*/
|
||||||
|
public class RegistryParam implements Serializable {
|
||||||
|
private static final long serialVersionUID = 42L;
|
||||||
|
|
||||||
|
private String registGroup;
|
||||||
|
private String registryKey;
|
||||||
|
private String registryValue;
|
||||||
|
|
||||||
|
public RegistryParam(){}
|
||||||
|
public RegistryParam(String registGroup, String registryKey, String registryValue) {
|
||||||
|
this.registGroup = registGroup;
|
||||||
|
this.registryKey = registryKey;
|
||||||
|
this.registryValue = registryValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegistGroup() {
|
||||||
|
return registGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistGroup(String registGroup) {
|
||||||
|
this.registGroup = registGroup;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegistryKey() {
|
||||||
|
return registryKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryKey(String registryKey) {
|
||||||
|
this.registryKey = registryKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRegistryValue() {
|
||||||
|
return registryValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRegistryValue(String registryValue) {
|
||||||
|
this.registryValue = registryValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "RegistryParam{" +
|
||||||
|
"registGroup='" + registGroup + '\'' +
|
||||||
|
", registryKey='" + registryKey + '\'' +
|
||||||
|
", registryValue='" + registryValue + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.xxl.job.core.enums;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by xuxueli on 17/5/10.
|
||||||
|
*/
|
||||||
|
public class RegistryConfig {
|
||||||
|
|
||||||
|
public static final int BEAT_TIMEOUT = 30;
|
||||||
|
public static final int DEAD_TIMEOUT = BEAT_TIMEOUT * 3;
|
||||||
|
|
||||||
|
public enum RegistType{ EXECUTOR, ADMIN }
|
||||||
|
|
||||||
|
}
|
@ -1,13 +0,0 @@
|
|||||||
package com.xxl.job.core.registry;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by xuxueli on 16/9/30.
|
|
||||||
*/
|
|
||||||
public interface RegistHelper {
|
|
||||||
|
|
||||||
public static final int TIMEOUT = 15;
|
|
||||||
public enum RegistType{ EXECUTOR, ADMIN }
|
|
||||||
|
|
||||||
public int registry(String registGroup, String registryKey, String registryValue);
|
|
||||||
|
|
||||||
}
|
|
@ -1,28 +0,0 @@
|
|||||||
package com.xxl.job.core.registry.impl;
|
|
||||||
|
|
||||||
import com.xxl.job.core.registry.RegistHelper;
|
|
||||||
import com.xxl.job.core.util.DBUtil;
|
|
||||||
|
|
||||||
import javax.sql.DataSource;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by xuxueli on 16/9/30.
|
|
||||||
*/
|
|
||||||
public class DbRegistHelper implements RegistHelper {
|
|
||||||
|
|
||||||
private DataSource dataSource;
|
|
||||||
public void setDataSource(DataSource dataSource) {
|
|
||||||
this.dataSource = dataSource;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int registry(String registGroup, String registryKey, String registryValue) {
|
|
||||||
String updateSql = "UPDATE XXL_JOB_QRTZ_TRIGGER_REGISTRY SET `update_time` = NOW() WHERE `registry_group` = ? AND `registry_key` = ? AND `registry_value` = ?";
|
|
||||||
String insertSql = "INSERT INTO XXL_JOB_QRTZ_TRIGGER_REGISTRY( `registry_group` , `registry_key` , `registry_value`, `update_time`) VALUES(? , ? , ?, NOW())";
|
|
||||||
int ret = DBUtil.update(dataSource, updateSql, new Object[]{registGroup, registryKey, registryValue});
|
|
||||||
if (ret<1) {
|
|
||||||
ret = DBUtil.update(dataSource, insertSql, new Object[]{registGroup, registryKey, registryValue});
|
|
||||||
}
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,116 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import com.xxl.job.core.biz.model.ReturnT;
|
||||||
|
import com.xxl.job.core.executor.XxlJobExecutor;
|
||||||
|
import org.apache.http.HttpEntity;
|
||||||
|
import org.apache.http.HttpResponse;
|
||||||
|
import org.apache.http.client.config.RequestConfig;
|
||||||
|
import org.apache.http.client.methods.HttpPost;
|
||||||
|
import org.apache.http.entity.StringEntity;
|
||||||
|
import org.apache.http.impl.client.CloseableHttpClient;
|
||||||
|
import org.apache.http.impl.client.HttpClients;
|
||||||
|
import org.apache.http.util.EntityUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuxueli 2017-05-10 21:28:15
|
||||||
|
*/
|
||||||
|
public class AdminApiUtil {
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(AdminApiUtil.class);
|
||||||
|
|
||||||
|
public static final String CALLBACK = "/api/callback";
|
||||||
|
public static final String REGISTRY = "/api/registry";
|
||||||
|
|
||||||
|
public static ReturnT<String> callApiFailover(String subUrl, Object requestObj) throws Exception {
|
||||||
|
|
||||||
|
// admin assress list
|
||||||
|
List<String> adminAddressList = new ArrayList<String>();
|
||||||
|
if (XxlJobExecutor.adminAddresses != null) {
|
||||||
|
for (String adminAddressItem: XxlJobExecutor.adminAddresses.split(",")) {
|
||||||
|
if (adminAddressItem.trim().length()>0 && !adminAddressList.contains(adminAddressItem)) {
|
||||||
|
adminAddressList.add(adminAddressItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (adminAddressList==null || adminAddressList.size()==0) {
|
||||||
|
return ReturnT.FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String adminAddress: adminAddressList) {
|
||||||
|
ReturnT<String> registryResult = null;
|
||||||
|
try {
|
||||||
|
String apiUrl = adminAddress.concat(subUrl);
|
||||||
|
registryResult = callApi(apiUrl, requestObj);
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
}
|
||||||
|
if (registryResult!=null && registryResult.getCode()==ReturnT.SUCCESS_CODE) {
|
||||||
|
return ReturnT.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReturnT.FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ReturnT<String> callApi(String finalUrl, Object requestObj) throws Exception {
|
||||||
|
HttpPost httpPost = new HttpPost(finalUrl);
|
||||||
|
CloseableHttpClient httpClient = HttpClients.createDefault();
|
||||||
|
try {
|
||||||
|
|
||||||
|
// timeout
|
||||||
|
RequestConfig requestConfig = RequestConfig.custom()
|
||||||
|
.setConnectionRequestTimeout(10000)
|
||||||
|
.setSocketTimeout(10000)
|
||||||
|
.setConnectTimeout(10000)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
httpPost.setConfig(requestConfig);
|
||||||
|
|
||||||
|
// data
|
||||||
|
if (requestObj != null) {
|
||||||
|
String json = JacksonUtil.writeValueAsString(requestObj);
|
||||||
|
|
||||||
|
StringEntity entity = new StringEntity(json, "utf-8");
|
||||||
|
entity.setContentEncoding("UTF-8");
|
||||||
|
entity.setContentType("application/json");
|
||||||
|
|
||||||
|
httpPost.setEntity(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
// do post
|
||||||
|
HttpResponse response = httpClient.execute(httpPost);
|
||||||
|
HttpEntity entity = response.getEntity();
|
||||||
|
if (null != entity) {
|
||||||
|
if (response.getStatusLine().getStatusCode() != 200) {
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
return ReturnT.FAIL;
|
||||||
|
}
|
||||||
|
|
||||||
|
String responseMsg = EntityUtils.toString(entity, "UTF-8");
|
||||||
|
EntityUtils.consume(entity);
|
||||||
|
if (responseMsg!=null && responseMsg.startsWith("{")) {
|
||||||
|
ReturnT<String> result = JacksonUtil.readValue(responseMsg, ReturnT.class);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ReturnT.FAIL;
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error("", e);
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, e.getMessage());
|
||||||
|
} finally {
|
||||||
|
if (httpPost!=null) {
|
||||||
|
httpPost.releaseConnection();
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
httpClient.close();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue