底层优化

v1.7
xueli.xue 8 years ago
parent 4760187247
commit 7b3d53fa1c

@ -857,6 +857,6 @@ XXL-JOB托管在Github上如有问题可在 [ISSUES](https://github.com/xuxue
更多接入公司欢迎在github [登记](https://github.com/xuxueli/xxl-job/issues/1 )
---
#### 支持的话可以扫一扫,支持 [XXL系列](https://github.com/xuxueli) 的建设
#### 支持的话可以扫一扫,支持 [XXL系列](https://github.com/xuxueli) 的建设
![输入图片说明](http://images2015.cnblogs.com/blog/554415/201605/554415-20160513183306234-1939652116.png "在这里输入图片标题")

@ -20,7 +20,7 @@ package com.xxl.job.admin.core.jobbean;
//import com.xxl.job.client.util.JacksonUtil;
//import com.xxl.job.core.model.XxlJobInfo;
//import com.xxl.job.core.model.XxlJobLog;
//import com.xxl.job.core.thread.JobMonitorHelper;
//import com.xxl.job.core.thread.JobFailMonitorHelper;
//import com.xxl.job.core.util.DynamicSchedulerUtil;
//
///**
@ -84,7 +84,7 @@ package com.xxl.job.admin.core.jobbean;
// // update trigger info
// DynamicSchedulerUtil.xxlJobLogDao.updateTriggerInfo(jobLog);
// DynamicSchedulerUtil.xxlJobLogDao.updateHandleInfo(jobLog);
// JobMonitorHelper.monitor(jobLog.getId());
// JobFailMonitorHelper.monitor(jobLog.getId());
// logger.info(">>>>>>>>>>> xxl-job trigger end, jobLog.id:{}, jobLog:{}", jobLog.getId(), jobLog);
//
// }

@ -6,7 +6,7 @@ import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.model.XxlJobLog;
import com.xxl.job.admin.core.route.ExecutorRouteStrategyEnum;
import com.xxl.job.admin.core.schedule.XxlJobDynamicScheduler;
import com.xxl.job.admin.core.thread.JobMonitorHelper;
import com.xxl.job.admin.core.thread.JobFailMonitorHelper;
import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper;
import com.xxl.job.core.biz.ExecutorBiz;
import com.xxl.job.core.biz.model.ReturnT;
@ -87,7 +87,7 @@ public class RemoteHttpJobBean extends QuartzJobBean {
XxlJobDynamicScheduler.xxlJobLogDao.updateTriggerInfo(jobLog);
// monitor triger
JobMonitorHelper.monitor(jobLog.getId());
JobFailMonitorHelper.monitor(jobLog.getId());
logger.debug(">>>>>>>>>>> xxl-job trigger end, jobId:{}", jobLog.getId());
}

@ -2,7 +2,7 @@ package com.xxl.job.admin.core.schedule;
import com.xxl.job.admin.core.jobbean.RemoteHttpJobBean;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.core.thread.JobMonitorHelper;
import com.xxl.job.admin.core.thread.JobFailMonitorHelper;
import com.xxl.job.admin.core.thread.JobRegistryMonitorHelper;
import com.xxl.job.admin.dao.IXxlJobGroupDao;
import com.xxl.job.admin.dao.IXxlJobInfoDao;
@ -43,7 +43,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
JobRegistryMonitorHelper.getInstance().start();
// admin monitor run
JobMonitorHelper.getInstance().start();
JobFailMonitorHelper.getInstance().start();
}
// destroy
@ -52,7 +52,7 @@ public final class XxlJobDynamicScheduler implements ApplicationContextAware, In
JobRegistryMonitorHelper.getInstance().toStop();
// admin monitor stop
JobMonitorHelper.getInstance().toStop();
JobFailMonitorHelper.getInstance().toStop();
serverFactory.destroy();
}

@ -1,94 +1,94 @@
package com.xxl.job.admin.core.thread;
import com.xxl.job.admin.core.model.XxlJobGroup;
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.core.util.MailUtil;
import com.xxl.job.core.biz.model.ReturnT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;
/**
* job monitor instance
* @author xuxueli 2015-9-1 18:05:56
*/
public class JobMonitorHelper {
private static Logger logger = LoggerFactory.getLogger(JobMonitorHelper.class);
private static JobMonitorHelper instance = new JobMonitorHelper();
public static JobMonitorHelper getInstance(){
return instance;
}
private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(0xfff8);
private Thread monitorThread;
private boolean toStop = false;
public void start(){
monitorThread = new Thread(new Runnable() {
@Override
public void run() {
while (!toStop) {
try {
logger.debug(">>>>>>>>>>> job monitor beat ... ");
Integer jobLogId = JobMonitorHelper.instance.queue.take();
if (jobLogId != null && jobLogId > 0) {
logger.debug(">>>>>>>>>>> job monitor heat success, JobLogId:{}", jobLogId);
XxlJobLog log = XxlJobDynamicScheduler.xxlJobLogDao.load(jobLogId);
if (log!=null) {
if (ReturnT.SUCCESS_CODE==log.getTriggerCode() && log.getHandleCode()==0) {
// running
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
JobMonitorHelper.monitor(jobLogId);
}
if (ReturnT.SUCCESS_CODE==log.getTriggerCode() && ReturnT.SUCCESS_CODE==log.getHandleCode()) {
// pass
}
if (ReturnT.FAIL_CODE == log.getTriggerCode()|| ReturnT.FAIL_CODE==log.getHandleCode()) {
XxlJobInfo info = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(log.getJobId());
if (info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0) {
Set<String> emailSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(",")));
for (String email: emailSet) {
String title = "《调度监控报警》(任务调度中心XXL-JOB)";
XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(Integer.valueOf(info.getJobGroup()));
String content = MessageFormat.format("任务调度失败, 执行器名称:{0}, 任务描述:{1}.", group!=null?group.getTitle():"null", info.getJobDesc());
MailUtil.sendMail(email, title, content, false, null);
}
}
}
}
}
} catch (Exception e) {
logger.error("job monitor error:{}", e);
}
}
}
});
monitorThread.setDaemon(true);
monitorThread.start();
}
public void toStop(){
toStop = true;
//monitorThread.interrupt();
}
// producer
public static void monitor(int jobLogId){
getInstance().queue.offer(jobLogId);
}
}
package com.xxl.job.admin.core.thread;
import com.xxl.job.admin.core.model.XxlJobGroup;
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.core.util.MailUtil;
import com.xxl.job.core.biz.model.ReturnT;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.*;
/**
* job monitor instance
* @author xuxueli 2015-9-1 18:05:56
*/
public class JobFailMonitorHelper {
private static Logger logger = LoggerFactory.getLogger(JobFailMonitorHelper.class);
private static JobFailMonitorHelper instance = new JobFailMonitorHelper();
public static JobFailMonitorHelper getInstance(){
return instance;
}
private LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(0xfff8);
private Thread monitorThread;
private boolean toStop = false;
public void start(){
monitorThread = new Thread(new Runnable() {
@Override
public void run() {
while (!toStop) {
try {
logger.debug(">>>>>>>>>>> job monitor beat ... ");
Integer jobLogId = JobFailMonitorHelper.instance.queue.take();
if (jobLogId != null && jobLogId > 0) {
logger.debug(">>>>>>>>>>> job monitor heat success, JobLogId:{}", jobLogId);
XxlJobLog log = XxlJobDynamicScheduler.xxlJobLogDao.load(jobLogId);
if (log!=null) {
if (ReturnT.SUCCESS_CODE==log.getTriggerCode() && log.getHandleCode()==0) {
// running
try {
TimeUnit.SECONDS.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
JobFailMonitorHelper.monitor(jobLogId);
}
if (ReturnT.SUCCESS_CODE==log.getTriggerCode() && ReturnT.SUCCESS_CODE==log.getHandleCode()) {
// pass
}
if (ReturnT.FAIL_CODE == log.getTriggerCode()|| ReturnT.FAIL_CODE==log.getHandleCode()) {
XxlJobInfo info = XxlJobDynamicScheduler.xxlJobInfoDao.loadById(log.getJobId());
if (info!=null && info.getAlarmEmail()!=null && info.getAlarmEmail().trim().length()>0) {
Set<String> emailSet = new HashSet<String>(Arrays.asList(info.getAlarmEmail().split(",")));
for (String email: emailSet) {
String title = "《调度监控报警》(任务调度中心XXL-JOB)";
XxlJobGroup group = XxlJobDynamicScheduler.xxlJobGroupDao.load(Integer.valueOf(info.getJobGroup()));
String content = MessageFormat.format("任务调度失败, 执行器名称:{0}, 任务描述:{1}.", group!=null?group.getTitle():"null", info.getJobDesc());
MailUtil.sendMail(email, title, content, false, null);
}
}
}
}
}
} catch (Exception e) {
logger.error("job monitor error:{}", e);
}
}
}
});
monitorThread.setDaemon(true);
monitorThread.start();
}
public void toStop(){
toStop = true;
//monitorThread.interrupt();
}
// producer
public static void monitor(int jobLogId){
getInstance().queue.offer(jobLogId);
}
}

@ -1,16 +0,0 @@
//package com.xxl.job.core.glue.loader;
//
///**
// * code source loader
// * @author xuxueli 2016-1-2 20:01:39
// */
//public interface GlueLoader {
//
// /**
// * load code source by name, ensure every load is the latest.
// * @param jobId
// * @return code source
// */
// public String load(int jobId);
//
//}

@ -1,30 +0,0 @@
//package com.xxl.job.core.glue.loader.impl;
//
//import com.xxl.job.core.glue.loader.GlueLoader;
//import com.xxl.job.core.util.DBUtil;
//
//import javax.sql.DataSource;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by xuxueli on 16/9/30.
// */
//public class DbGlueLoader implements GlueLoader {
//
// private DataSource dataSource;
// public void setDataSource(DataSource dataSource) {
// this.dataSource = dataSource;
// }
//
// @Override
// public String load(int jobId) {
// String sql = "SELECT glue_source FROM XXL_JOB_QRTZ_TRIGGER_INFO WHERE id = ?";
// List<Map<String, Object>> result = DBUtil.query(dataSource, sql, new Object[]{jobId});
// if (result!=null && result.size()==1 && result.get(0)!=null && result.get(0).get("glue_source")!=null ) {
// return (String) result.get(0).get("glue_source");
// }
// return null;
// }
//
//}

@ -20,6 +20,12 @@ public class XxlJobLogger {
*/
public static void log(String appendLog) {
// logFileName
String logFileName = XxlJobFileAppender.contextHolder.get();
if (logFileName==null || logFileName.trim().length()==0) {
return;
}
// "yyyy-MM-dd HH:mm:ss [ClassName]-[MethodName]-[LineNumber]-[ThreadName] log";
StackTraceElement[] stackTraceElements = new Throwable().getStackTrace();
StackTraceElement callInfo = stackTraceElements[1];
@ -34,7 +40,6 @@ public class XxlJobLogger {
String formatAppendLog = stringBuffer.toString();
// appendlog
String logFileName = XxlJobFileAppender.contextHolder.get();
XxlJobFileAppender.appendLog(logFileName, formatAppendLog);
logger.warn("[{}]: {}", logFileName, formatAppendLog);

@ -106,27 +106,29 @@ public class JobThread extends Thread{
String logFileName = XxlJobFileAppender.makeLogFileName(new Date(triggerParam.getLogDateTim()), triggerParam.getLogId());
XxlJobFileAppender.contextHolder.set(logFileName);
XxlJobLogger.log("----------- xxl-job job execute start -----------");
XxlJobLogger.log("<br>----------- xxl-job job execute start -----------<br>----------- Params:" + handlerParams);
executeResult = handler.execute(handlerParams);
if (executeResult == null) {
executeResult = ReturnT.FAIL;
}
XxlJobLogger.log("<br>----------- xxl-job job execute end(finish) -----------<br>----------- ReturnT:" + executeResult);
} catch (Exception e) {
if (toStop) {
XxlJobLogger.log("<br>----------- xxl-job toStop, stopReason:" + stopReason);
XxlJobLogger.log("<br>----------- JobThread toStop, stopReason:" + stopReason);
}
StringWriter stringWriter = new StringWriter();
e.printStackTrace(new PrintWriter(stringWriter));
String errorMsg = stringWriter.toString();
XxlJobLogger.log("JobThread Exception:" + errorMsg);
executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, errorMsg);
executeResult = new ReturnT<String>(ReturnT.FAIL_CODE, stringWriter.toString());
XxlJobLogger.log("<br>----------- JobThread Exception:" + errorMsg + "<br>----------- xxl-job job execute end(error) -----------");
}
XxlJobLogger.log("----------- xxl-job job execute end ----------- <br> " +
"Look : ExecutorParams:"+ handlerParams +", Code:"+ executeResult.getCode() +", Msg:" + executeResult.getMsg());
// callback handler info
if (!toStop) {

Loading…
Cancel
Save