# Conflicts: # xxl-job-admin/src/main/java/com/xxl/job/admin/controller/JobApiController.java # xxl-job-admin/src/main/java/com/xxl/job/admin/core/conf/XxlJobScheduler.java # xxl-job-admin/src/main/java/com/xxl/job/admin/service/impl/AdminBizImpl.java2.1.2
commit
8512a34469
@ -0,0 +1,17 @@
|
|||||||
|
name: Java CI
|
||||||
|
|
||||||
|
on: [push]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v1
|
||||||
|
- name: Set up JDK 1.8
|
||||||
|
uses: actions/setup-java@v1
|
||||||
|
with:
|
||||||
|
java-version: 1.8
|
||||||
|
- name: Build with Maven
|
||||||
|
run: mvn -B package --file pom.xml
|
@ -1,6 +0,0 @@
|
|||||||
language: java
|
|
||||||
jdk:
|
|
||||||
- openjdk8
|
|
||||||
install: mvn install -DskipTests=true -Dmaven.javadoc.skip=true
|
|
||||||
#script: mvn test
|
|
||||||
script: mvn -DskipTests=true clean package
|
|
@ -1,34 +1,97 @@
|
|||||||
package com.xxl.job.admin.controller;
|
package com.xxl.job.admin.controller;
|
||||||
|
|
||||||
import com.xxl.job.admin.controller.annotation.PermissionLimit;
|
import com.xxl.job.admin.controller.annotation.PermissionLimit;
|
||||||
import com.xxl.job.admin.core.conf.XxlJobScheduler;
|
import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
|
||||||
import com.xxl.job.core.biz.AdminBiz;
|
import com.xxl.job.core.biz.AdminBiz;
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
import com.xxl.job.core.biz.model.HandleCallbackParam;
|
||||||
|
import com.xxl.job.core.biz.model.RegistryParam;
|
||||||
|
import com.xxl.job.core.biz.model.ReturnT;
|
||||||
|
import com.xxl.job.core.util.XxlJobRemotingUtil;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.annotation.Resource;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import java.util.List;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by xuxueli on 17/5/10.
|
* Created by xuxueli on 17/5/10.
|
||||||
*/
|
*/
|
||||||
@Controller
|
@Controller
|
||||||
public class JobApiController implements InitializingBean {
|
@RequestMapping("/api")
|
||||||
|
public class JobApiController {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private AdminBiz adminBiz;
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
|
|
||||||
|
// ---------------------- admin biz ----------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* callback
|
||||||
|
*
|
||||||
|
* @param callbackParamList
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/callback")
|
||||||
|
@ResponseBody
|
||||||
|
@PermissionLimit(limit=false)
|
||||||
|
public ReturnT<String> callback(HttpServletRequest request, @RequestBody List<HandleCallbackParam> callbackParamList) {
|
||||||
|
|
||||||
|
if (XxlJobAdminConfig.getAdminConfig().getAccessToken()!=null
|
||||||
|
&& XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length()>0
|
||||||
|
&& !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_RPC_ACCESS_TOKEN))) {
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return adminBiz.callback(callbackParamList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(AdminBiz.MAPPING)
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* registry
|
||||||
|
*
|
||||||
|
* @param registryParam
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/registry")
|
||||||
|
@ResponseBody
|
||||||
@PermissionLimit(limit=false)
|
@PermissionLimit(limit=false)
|
||||||
public void api(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
public ReturnT<String> registry(HttpServletRequest request, @RequestBody RegistryParam registryParam) {
|
||||||
XxlJobScheduler.invokeAdminService(request, response);
|
|
||||||
|
if (XxlJobAdminConfig.getAdminConfig().getAccessToken()!=null
|
||||||
|
&& XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length()>0
|
||||||
|
&& !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_RPC_ACCESS_TOKEN))) {
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return adminBiz.registry(registryParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* registry remove
|
||||||
|
*
|
||||||
|
* @param registryParam
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
@RequestMapping("/registryRemove")
|
||||||
|
@ResponseBody
|
||||||
|
@PermissionLimit(limit=false)
|
||||||
|
public ReturnT<String> registryRemove(HttpServletRequest request, @RequestBody RegistryParam registryParam) {
|
||||||
|
|
||||||
|
if (XxlJobAdminConfig.getAdminConfig().getAccessToken()!=null
|
||||||
|
&& XxlJobAdminConfig.getAdminConfig().getAccessToken().trim().length()>0
|
||||||
|
&& !XxlJobAdminConfig.getAdminConfig().getAccessToken().equals(request.getHeader(XxlJobRemotingUtil.XXL_RPC_ACCESS_TOKEN))) {
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "The access token is wrong.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return adminBiz.registryRemove(registryParam);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------------- job biz ----------------------
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,150 +0,0 @@
|
|||||||
package com.xxl.job.admin.core.conf;
|
|
||||||
|
|
||||||
import com.xxl.job.admin.core.thread.JobFailMonitorHelper;
|
|
||||||
import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper;
|
|
||||||
import com.xxl.job.admin.core.thread.JobScheduleHelper;
|
|
||||||
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
|
|
||||||
import com.xxl.job.admin.core.util.I18nUtil;
|
|
||||||
import com.xxl.job.core.biz.AdminBiz;
|
|
||||||
import com.xxl.job.core.biz.ExecutorBiz;
|
|
||||||
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
|
|
||||||
import com.xxl.rpc.remoting.invoker.XxlRpcInvokerFactory;
|
|
||||||
import com.xxl.rpc.remoting.invoker.call.CallType;
|
|
||||||
import com.xxl.rpc.remoting.invoker.reference.XxlRpcReferenceBean;
|
|
||||||
import com.xxl.rpc.remoting.invoker.route.LoadBalance;
|
|
||||||
import com.xxl.rpc.remoting.net.NetEnum;
|
|
||||||
import com.xxl.rpc.remoting.net.impl.servlet.server.ServletServerHandler;
|
|
||||||
import com.xxl.rpc.remoting.provider.XxlRpcProviderFactory;
|
|
||||||
import com.xxl.rpc.serialize.Serializer;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
import org.springframework.beans.factory.DisposableBean;
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.context.annotation.DependsOn;
|
|
||||||
import org.springframework.stereotype.Component;
|
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.ConcurrentMap;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author xuxueli 2018-10-28 00:18:17
|
|
||||||
*/
|
|
||||||
@Component
|
|
||||||
@DependsOn("xxlJobAdminConfig")
|
|
||||||
public class XxlJobScheduler implements InitializingBean, DisposableBean {
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(XxlJobScheduler.class);
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void afterPropertiesSet() {
|
|
||||||
// init i18n
|
|
||||||
initI18n();
|
|
||||||
|
|
||||||
// admin registry monitor run
|
|
||||||
JobRegistryMonitorHelper.getInstance().start();
|
|
||||||
|
|
||||||
// admin monitor run
|
|
||||||
JobFailMonitorHelper.getInstance().start();
|
|
||||||
|
|
||||||
// admin-server
|
|
||||||
initRpcProvider();
|
|
||||||
|
|
||||||
// start-schedule
|
|
||||||
JobScheduleHelper.getInstance().start();
|
|
||||||
|
|
||||||
logger.info(">>>>>>>>> init xxl-job admin success.");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void destroy() throws Exception {
|
|
||||||
|
|
||||||
// stop-schedule
|
|
||||||
JobScheduleHelper.getInstance().toStop();
|
|
||||||
|
|
||||||
// admin trigger pool stop
|
|
||||||
JobTriggerPoolHelper.toStop();
|
|
||||||
|
|
||||||
// admin registry stop
|
|
||||||
JobRegistryMonitorHelper.getInstance().toStop();
|
|
||||||
|
|
||||||
// admin monitor stop
|
|
||||||
JobFailMonitorHelper.getInstance().toStop();
|
|
||||||
|
|
||||||
// admin-server
|
|
||||||
stopRpcProvider();
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- I18n ----------------------
|
|
||||||
|
|
||||||
private void initI18n(){
|
|
||||||
for (ExecutorBlockStrategyEnum item:ExecutorBlockStrategyEnum.values()) {
|
|
||||||
item.setTitle(I18nUtil.getString("jobconf_block_".concat(item.name())));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// ---------------------- admin rpc provider (no server version) ----------------------
|
|
||||||
private static ServletServerHandler servletServerHandler;
|
|
||||||
private void initRpcProvider(){
|
|
||||||
// init
|
|
||||||
XxlRpcProviderFactory xxlRpcProviderFactory = new XxlRpcProviderFactory();
|
|
||||||
xxlRpcProviderFactory.initConfig(
|
|
||||||
NetEnum.NETTY_HTTP,
|
|
||||||
Serializer.SerializeEnum.HESSIAN.getSerializer(),
|
|
||||||
null,
|
|
||||||
0,
|
|
||||||
XxlJobAdminConfig.getAdminConfig().getAccessToken(),
|
|
||||||
null,
|
|
||||||
null);
|
|
||||||
|
|
||||||
// add services
|
|
||||||
xxlRpcProviderFactory.addService(AdminBiz.class.getName(), null, XxlJobAdminConfig.getAdminConfig().getAdminBiz());
|
|
||||||
|
|
||||||
// servlet handler
|
|
||||||
servletServerHandler = new ServletServerHandler(xxlRpcProviderFactory);
|
|
||||||
}
|
|
||||||
private void stopRpcProvider() throws Exception {
|
|
||||||
XxlRpcInvokerFactory.getInstance().stop();
|
|
||||||
}
|
|
||||||
public static void invokeAdminService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
|
|
||||||
servletServerHandler.handle(null, request, response);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// ---------------------- executor-client ----------------------
|
|
||||||
private static ConcurrentMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
|
|
||||||
public static ExecutorBiz getExecutorBiz(String address) {
|
|
||||||
// valid
|
|
||||||
if (address==null || address.trim().length()==0) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
// load-cache
|
|
||||||
address = address.trim();
|
|
||||||
ExecutorBiz executorBiz = executorBizRepository.get(address);
|
|
||||||
if (executorBiz != null) {
|
|
||||||
return executorBiz;
|
|
||||||
}
|
|
||||||
|
|
||||||
// set-cache
|
|
||||||
executorBiz = (ExecutorBiz) new XxlRpcReferenceBean(
|
|
||||||
NetEnum.NETTY_HTTP,
|
|
||||||
Serializer.SerializeEnum.HESSIAN.getSerializer(),
|
|
||||||
CallType.SYNC,
|
|
||||||
LoadBalance.ROUND,
|
|
||||||
ExecutorBiz.class,
|
|
||||||
null,
|
|
||||||
3000,
|
|
||||||
address,
|
|
||||||
XxlJobAdminConfig.getAdminConfig().getAccessToken(),
|
|
||||||
null,
|
|
||||||
null).getObject();
|
|
||||||
|
|
||||||
executorBizRepository.put(address, executorBiz);
|
|
||||||
return executorBiz;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,109 @@
|
|||||||
|
package com.xxl.job.admin.core.scheduler;
|
||||||
|
|
||||||
|
import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
|
||||||
|
import com.xxl.job.admin.core.thread.JobFailMonitorHelper;
|
||||||
|
import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper;
|
||||||
|
import com.xxl.job.admin.core.thread.JobScheduleHelper;
|
||||||
|
import com.xxl.job.admin.core.thread.JobTriggerPoolHelper;
|
||||||
|
import com.xxl.job.admin.core.util.I18nUtil;
|
||||||
|
import com.xxl.job.core.biz.ExecutorBiz;
|
||||||
|
import com.xxl.job.core.enums.ExecutorBlockStrategyEnum;
|
||||||
|
import com.xxl.rpc.remoting.invoker.call.CallType;
|
||||||
|
import com.xxl.rpc.remoting.invoker.reference.XxlRpcReferenceBean;
|
||||||
|
import com.xxl.rpc.remoting.invoker.route.LoadBalance;
|
||||||
|
import com.xxl.rpc.remoting.net.impl.netty_http.client.NettyHttpClient;
|
||||||
|
import com.xxl.rpc.serialize.impl.HessianSerializer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
import java.util.concurrent.ConcurrentMap;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuxueli 2018-10-28 00:18:17
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class XxlJobScheduler {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(XxlJobScheduler.class);
|
||||||
|
|
||||||
|
|
||||||
|
public void init() throws Exception {
|
||||||
|
// init i18n
|
||||||
|
initI18n();
|
||||||
|
|
||||||
|
// admin registry monitor run
|
||||||
|
JobRegistryMonitorHelper.getInstance().start();
|
||||||
|
|
||||||
|
// admin monitor run
|
||||||
|
JobFailMonitorHelper.getInstance().start();
|
||||||
|
|
||||||
|
// admin trigger pool start
|
||||||
|
JobTriggerPoolHelper.toStart();
|
||||||
|
|
||||||
|
// start-schedule
|
||||||
|
JobScheduleHelper.getInstance().start();
|
||||||
|
|
||||||
|
logger.info(">>>>>>>>> init xxl-job admin success.");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
|
||||||
|
// stop-schedule
|
||||||
|
JobScheduleHelper.getInstance().toStop();
|
||||||
|
|
||||||
|
// admin trigger pool stop
|
||||||
|
JobTriggerPoolHelper.toStop();
|
||||||
|
|
||||||
|
// admin monitor stop
|
||||||
|
JobFailMonitorHelper.getInstance().toStop();
|
||||||
|
|
||||||
|
// admin registry stop
|
||||||
|
JobRegistryMonitorHelper.getInstance().toStop();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------- I18n ----------------------
|
||||||
|
|
||||||
|
private void initI18n(){
|
||||||
|
for (ExecutorBlockStrategyEnum item:ExecutorBlockStrategyEnum.values()) {
|
||||||
|
item.setTitle(I18nUtil.getString("jobconf_block_".concat(item.name())));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------- executor-client ----------------------
|
||||||
|
private static ConcurrentMap<String, ExecutorBiz> executorBizRepository = new ConcurrentHashMap<String, ExecutorBiz>();
|
||||||
|
public static ExecutorBiz getExecutorBiz(String address) throws Exception {
|
||||||
|
// valid
|
||||||
|
if (address==null || address.trim().length()==0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// load-cache
|
||||||
|
address = address.trim();
|
||||||
|
ExecutorBiz executorBiz = executorBizRepository.get(address);
|
||||||
|
if (executorBiz != null) {
|
||||||
|
return executorBiz;
|
||||||
|
}
|
||||||
|
|
||||||
|
// set-cache
|
||||||
|
XxlRpcReferenceBean referenceBean = new XxlRpcReferenceBean();
|
||||||
|
referenceBean.setClient(NettyHttpClient.class);
|
||||||
|
referenceBean.setSerializer(HessianSerializer.class);
|
||||||
|
referenceBean.setCallType(CallType.SYNC);
|
||||||
|
referenceBean.setLoadBalance(LoadBalance.ROUND);
|
||||||
|
referenceBean.setIface(ExecutorBiz.class);
|
||||||
|
referenceBean.setVersion(null);
|
||||||
|
referenceBean.setTimeout(3000);
|
||||||
|
referenceBean.setAddress(address);
|
||||||
|
referenceBean.setAccessToken(XxlJobAdminConfig.getAdminConfig().getAccessToken());
|
||||||
|
referenceBean.setInvokeCallback(null);
|
||||||
|
referenceBean.setInvokerFactory(null);
|
||||||
|
|
||||||
|
executorBiz = (ExecutorBiz) referenceBean.getObject();
|
||||||
|
|
||||||
|
executorBizRepository.put(address, executorBiz);
|
||||||
|
return executorBiz;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,48 @@
|
|||||||
|
package com.xxl.job.core.biz.client;
|
||||||
|
|
||||||
|
import com.xxl.job.core.biz.AdminBiz;
|
||||||
|
import com.xxl.job.core.biz.model.HandleCallbackParam;
|
||||||
|
import com.xxl.job.core.biz.model.RegistryParam;
|
||||||
|
import com.xxl.job.core.biz.model.ReturnT;
|
||||||
|
import com.xxl.job.core.util.XxlJobRemotingUtil;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* admin api test
|
||||||
|
*
|
||||||
|
* @author xuxueli 2017-07-28 22:14:52
|
||||||
|
*/
|
||||||
|
public class AdminBizClient implements AdminBiz {
|
||||||
|
|
||||||
|
public AdminBizClient() {
|
||||||
|
}
|
||||||
|
public AdminBizClient(String addressUrl, String accessToken) {
|
||||||
|
this.addressUrl = addressUrl;
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
|
||||||
|
// valid
|
||||||
|
if (!this.addressUrl.endsWith("/")) {
|
||||||
|
this.addressUrl = this.addressUrl + "/";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String addressUrl ;
|
||||||
|
private String accessToken;
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
|
||||||
|
return XxlJobRemotingUtil.postBody(addressUrl+"api/callback", accessToken, callbackParamList, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnT<String> registry(RegistryParam registryParam) {
|
||||||
|
return XxlJobRemotingUtil.postBody(addressUrl + "api/registry", accessToken, registryParam, 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnT<String> registryRemove(RegistryParam registryParam) {
|
||||||
|
return XxlJobRemotingUtil.postBody(addressUrl + "api/registryRemove", accessToken, registryParam, 3);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,122 @@
|
|||||||
|
package com.xxl.job.core.util;
|
||||||
|
|
||||||
|
import com.xxl.job.core.biz.model.ReturnT;
|
||||||
|
import com.xxl.registry.client.util.json.BasicJson;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author xuxueli 2018-11-25 00:55:31
|
||||||
|
*/
|
||||||
|
public class XxlJobRemotingUtil {
|
||||||
|
private static Logger logger = LoggerFactory.getLogger(XxlJobRemotingUtil.class);
|
||||||
|
public static String XXL_RPC_ACCESS_TOKEN = "XXL_RPC_ACCESS_TOKEN";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* post
|
||||||
|
*
|
||||||
|
* @param url
|
||||||
|
* @param accessToken
|
||||||
|
* @param requestObj
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ReturnT<String> postBody(String url, String accessToken, Object requestObj, int timeout) {
|
||||||
|
HttpURLConnection connection = null;
|
||||||
|
BufferedReader bufferedReader = null;
|
||||||
|
try {
|
||||||
|
// connection
|
||||||
|
URL realUrl = new URL(url);
|
||||||
|
connection = (HttpURLConnection) realUrl.openConnection();
|
||||||
|
|
||||||
|
// connection setting
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.setDoOutput(true);
|
||||||
|
connection.setDoInput(true);
|
||||||
|
connection.setUseCaches(false);
|
||||||
|
connection.setReadTimeout(timeout * 1000);
|
||||||
|
connection.setConnectTimeout(3 * 1000);
|
||||||
|
connection.setRequestProperty("connection", "Keep-Alive");
|
||||||
|
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
|
||||||
|
connection.setRequestProperty("Accept-Charset", "application/json;charset=UTF-8");
|
||||||
|
|
||||||
|
if(accessToken!=null && accessToken.trim().length()>0){
|
||||||
|
connection.setRequestProperty(XXL_RPC_ACCESS_TOKEN, accessToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
// do connection
|
||||||
|
connection.connect();
|
||||||
|
|
||||||
|
// write requestBody
|
||||||
|
String requestBody = BasicJson.toJson(requestObj);
|
||||||
|
|
||||||
|
DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream());
|
||||||
|
dataOutputStream.writeBytes(requestBody);
|
||||||
|
dataOutputStream.flush();
|
||||||
|
dataOutputStream.close();
|
||||||
|
|
||||||
|
/*byte[] requestBodyBytes = requestBody.getBytes("UTF-8");
|
||||||
|
connection.setRequestProperty("Content-Length", String.valueOf(requestBodyBytes.length));
|
||||||
|
OutputStream outwritestream = connection.getOutputStream();
|
||||||
|
outwritestream.write(requestBodyBytes);
|
||||||
|
outwritestream.flush();
|
||||||
|
outwritestream.close();*/
|
||||||
|
|
||||||
|
// valid StatusCode
|
||||||
|
int statusCode = connection.getResponseCode();
|
||||||
|
if (statusCode != 200) {
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "xxl-rpc remoting fail, StatusCode("+ statusCode +") invalid. for url : " + url);
|
||||||
|
}
|
||||||
|
|
||||||
|
// result
|
||||||
|
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
String line;
|
||||||
|
while ((line = bufferedReader.readLine()) != null) {
|
||||||
|
result.append(line);
|
||||||
|
}
|
||||||
|
String resultJson = result.toString();
|
||||||
|
|
||||||
|
// parse returnT
|
||||||
|
try {
|
||||||
|
Map<String, Object> resultMap = BasicJson.parseMap(resultJson);
|
||||||
|
|
||||||
|
ReturnT<String> returnT = new ReturnT<String>();
|
||||||
|
if (resultMap==null) {
|
||||||
|
returnT.setCode(ReturnT.FAIL_CODE);
|
||||||
|
returnT.setMsg("AdminBizClient Remoting call fail.");
|
||||||
|
} else {
|
||||||
|
returnT.setCode(Integer.valueOf(String.valueOf(resultMap.get("code"))));
|
||||||
|
returnT.setMsg(String.valueOf(resultMap.get("msg")));
|
||||||
|
returnT.setContent(String.valueOf(resultMap.get("content")));
|
||||||
|
}
|
||||||
|
return returnT;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "xxl-rpc remoting response content invalid("+ resultJson +"), for url : " + url);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (Exception e) {
|
||||||
|
logger.error(e.getMessage(), e);
|
||||||
|
return new ReturnT<String>(ReturnT.FAIL_CODE, "xxl-rpc remoting error("+ e.getMessage() +"), for url : " + url);
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
if (bufferedReader != null) {
|
||||||
|
bufferedReader.close();
|
||||||
|
}
|
||||||
|
if (connection != null) {
|
||||||
|
connection.disconnect();
|
||||||
|
}
|
||||||
|
} catch (Exception e2) {
|
||||||
|
logger.error(e2.getMessage(), e2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue