parent
1651c63d80
commit
ee07e0b794
@ -0,0 +1,121 @@
|
||||
package com.xxl.job.admin.service.impl;
|
||||
|
||||
import com.xxl.job.admin.controller.JobApiController;
|
||||
import com.xxl.job.admin.core.model.XxlJobInfo;
|
||||
import com.xxl.job.admin.core.model.XxlJobLog;
|
||||
import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler;
|
||||
import com.xxl.job.admin.dao.XxlJobInfoDao;
|
||||
import com.xxl.job.admin.dao.XxlJobLogDao;
|
||||
import com.xxl.job.admin.dao.XxlJobRegistryDao;
|
||||
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 org.apache.commons.lang.StringUtils;
|
||||
import org.quartz.SchedulerException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author xuxueli 2017-07-27 21:54:20
|
||||
*/
|
||||
@Service
|
||||
public class AdminBizImpl implements AdminBiz {
|
||||
private static Logger logger = LoggerFactory.getLogger(AdminBizImpl.class);
|
||||
|
||||
@Resource
|
||||
public XxlJobLogDao xxlJobLogDao;
|
||||
@Resource
|
||||
private XxlJobInfoDao xxlJobInfoDao;
|
||||
@Resource
|
||||
private XxlJobRegistryDao xxlJobRegistryDao;
|
||||
|
||||
@Override
|
||||
public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList) {
|
||||
for (HandleCallbackParam handleCallbackParam: callbackParamList) {
|
||||
ReturnT<String> callbackResult = callback(handleCallbackParam);
|
||||
logger.info("JobApiController.callback {}, handleCallbackParam={}, callbackResult={}",
|
||||
(callbackResult.getCode()==ReturnT.SUCCESS_CODE?"success":"fail"), handleCallbackParam, callbackResult);
|
||||
}
|
||||
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
private ReturnT<String> callback(HandleCallbackParam handleCallbackParam) {
|
||||
// valid log item
|
||||
XxlJobLog log = xxlJobLogDao.load(handleCallbackParam.getLogId());
|
||||
if (log == null) {
|
||||
return new ReturnT<String>(ReturnT.FAIL_CODE, "log item not found.");
|
||||
}
|
||||
|
||||
// trigger success, to trigger child job, and avoid repeat trigger child job
|
||||
String childTriggerMsg = null;
|
||||
if (ReturnT.SUCCESS_CODE==handleCallbackParam.getExecuteResult().getCode() && ReturnT.SUCCESS_CODE!=log.getHandleCode()) {
|
||||
XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(log.getJobId());
|
||||
if (xxlJobInfo!=null && StringUtils.isNotBlank(xxlJobInfo.getChildJobKey())) {
|
||||
childTriggerMsg = "<hr>";
|
||||
String[] childJobKeys = xxlJobInfo.getChildJobKey().split(",");
|
||||
for (int i = 0; i < childJobKeys.length; i++) {
|
||||
String[] jobKeyArr = childJobKeys[i].split("_");
|
||||
if (jobKeyArr!=null && jobKeyArr.length==2) {
|
||||
XxlJobInfo childJobInfo = xxlJobInfoDao.loadById(Integer.valueOf(jobKeyArr[1]));
|
||||
if (childJobInfo!=null) {
|
||||
try {
|
||||
boolean ret = XxlJobDynamicScheduler.triggerJob(String.valueOf(childJobInfo.getId()), String.valueOf(childJobInfo.getJobGroup()));
|
||||
|
||||
// add msg
|
||||
childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务成功, 子任务Key: {2}, status: {3}, 子任务描述: {4}",
|
||||
(i+1), childJobKeys.length, childJobKeys[i], ret, childJobInfo.getJobDesc());
|
||||
} catch (SchedulerException e) {
|
||||
logger.error("", e);
|
||||
}
|
||||
} else {
|
||||
childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务失败, 子任务xxlJobInfo不存在, 子任务Key: {2}",
|
||||
(i+1), childJobKeys.length, childJobKeys[i]);
|
||||
}
|
||||
} else {
|
||||
childTriggerMsg += MessageFormat.format("<br> {0}/{1} 触发子任务失败, 子任务Key格式错误, 子任务Key: {2}",
|
||||
(i+1), childJobKeys.length, childJobKeys[i]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// handle msg
|
||||
StringBuffer handleMsg = new StringBuffer();
|
||||
if (log.getHandleMsg()!=null) {
|
||||
handleMsg.append(log.getHandleMsg()).append("<br>");
|
||||
}
|
||||
if (handleCallbackParam.getExecuteResult().getMsg() != null) {
|
||||
handleMsg.append(handleCallbackParam.getExecuteResult().getMsg());
|
||||
}
|
||||
if (childTriggerMsg !=null) {
|
||||
handleMsg.append("<br>子任务触发备注:").append(childTriggerMsg);
|
||||
}
|
||||
|
||||
// success, save log
|
||||
log.setHandleTime(new Date());
|
||||
log.setHandleCode(handleCallbackParam.getExecuteResult().getCode());
|
||||
log.setHandleMsg(handleMsg.toString());
|
||||
xxlJobLogDao.updateHandleInfo(log);
|
||||
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReturnT<String> registry(RegistryParam registryParam) {
|
||||
int ret = xxlJobRegistryDao.registryUpdate(registryParam.getRegistGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue());
|
||||
if (ret < 1) {
|
||||
xxlJobRegistryDao.registrySave(registryParam.getRegistGroup(), registryParam.getRegistryKey(), registryParam.getRegistryValue());
|
||||
}
|
||||
return ReturnT.SUCCESS;
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
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");
|
||||
|
||||
AdminApiUtil.init("http://localhost:8080/xxl-job-admin");
|
||||
ReturnT<String> registryResult = AdminApiUtil.callApiFailover(AdminApiUtil.REGISTRY, registryParam);
|
||||
System.out.println(registryResult);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package com.xxl.job.core.biz;
|
||||
|
||||
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 java.util.List;
|
||||
|
||||
/**
|
||||
* @author xuxueli 2017-07-27 21:52:49
|
||||
*/
|
||||
public interface AdminBiz {
|
||||
|
||||
/**
|
||||
* callback
|
||||
*
|
||||
* @param callbackParamList
|
||||
* @return
|
||||
*/
|
||||
public ReturnT<String> callback(List<HandleCallbackParam> callbackParamList);
|
||||
|
||||
/**
|
||||
* registry
|
||||
*
|
||||
* @param registryParam
|
||||
* @return
|
||||
*/
|
||||
public ReturnT<String> registry(RegistryParam registryParam);
|
||||
|
||||
}
|
@ -1,128 +0,0 @@
|
||||
package com.xxl.job.core.util;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* Created by xuxueli on 16/9/30.
|
||||
*/
|
||||
public class DBUtil {
|
||||
|
||||
private static Connection getConn(DataSource dataSource) {
|
||||
try {
|
||||
return dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* update
|
||||
*
|
||||
* @param dataSource
|
||||
* @param sql
|
||||
* @param params
|
||||
*/
|
||||
public static int update(DataSource dataSource, String sql, Object params[]) {
|
||||
Connection connection = getConn(dataSource);
|
||||
PreparedStatement preparedStatement = null;
|
||||
int ret = 0;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
if (params != null) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
preparedStatement.setObject(i + 1, params[i]);
|
||||
}
|
||||
}
|
||||
ret = preparedStatement.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
release(connection, preparedStatement, null);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* query
|
||||
*
|
||||
* @param dataSource
|
||||
* @param sql
|
||||
* @param params
|
||||
* @return
|
||||
*/
|
||||
public static List<Map<String, Object>> query(DataSource dataSource, String sql, Object[] params) {
|
||||
Connection connection = getConn(dataSource);
|
||||
PreparedStatement preparedStatement = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
preparedStatement = connection.prepareStatement(sql);
|
||||
if (params != null) {
|
||||
for (int i = 0; i < params.length; i++) {
|
||||
preparedStatement.setObject(i + 1, params[i]);
|
||||
}
|
||||
}
|
||||
resultSet = preparedStatement.executeQuery();
|
||||
|
||||
List<Map<String, Object>> ret = resultSetToList(resultSet);
|
||||
return ret;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
release(connection, preparedStatement, resultSet);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static List<Map<String, Object>> resultSetToList(ResultSet resultSet) throws SQLException {
|
||||
if (resultSet == null) {
|
||||
return new ArrayList<Map<String, Object>>();
|
||||
}
|
||||
|
||||
ResultSetMetaData resultSetMetaData = resultSet.getMetaData(); // 得到结果集(rs)的结构信息,比如字段数、字段名等
|
||||
int columnCount = resultSetMetaData.getColumnCount(); // 返回此 ResultSet 对象中的列数
|
||||
|
||||
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
|
||||
while (resultSet.next()) {
|
||||
Map<String, Object> rowData = new HashMap<String, Object>(columnCount);
|
||||
for (int i = 1; i <= columnCount; i++) {
|
||||
rowData.put(resultSetMetaData.getColumnName(i), resultSet.getObject(i));
|
||||
}
|
||||
list.add(rowData);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
* release
|
||||
* @param connection
|
||||
* @param preparedStatement
|
||||
* @param resultSet
|
||||
*/
|
||||
public static void release(Connection connection, PreparedStatement preparedStatement, ResultSet resultSet) {
|
||||
if (resultSet != null) {
|
||||
try {
|
||||
resultSet.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (preparedStatement != null) {
|
||||
try {
|
||||
preparedStatement.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
if (connection != null) {
|
||||
try {
|
||||
connection.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue