cn.dceast.platform.task.quartz.data.OutputJobToDataBaseService Maven / Gradle / Ivy
The newest version!
package cn.dceast.platform.task.quartz.data;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import cn.dceast.platform.task.config.TaskConfig;
import cn.dceast.platform.task.db.DBUtil;
import cn.dceast.platform.task.quartz.JobEntity;
import cn.dceast.platform.task.quartz.JobExecutedEntity;
import cn.dceast.platform.task.quartz.exception.WriteToDataBaseException;
import cn.dceast.platform.task.quartz.listener.platformJobListener;
import cn.dceast.platform.task.quartz.vo.JobLifecycleHistory;
import cn.dceast.platform.task.quartz.vo.Pagination;
import cn.dceast.platform.task.util.ObjectDBObjectUtil;
import cn.dceast.platform.task.util.StringUtil;
import com.mongodb.BasicDBList;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.QueryOperators;
import com.mongodb.WriteResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OutputJobToDataBaseService {
private static Logger logger= LoggerFactory.getLogger(OutputJobToDataBaseService.class);
public enum jobAction {ADD,UPDATE,DELETE,PAUSE,RESUME,UNSECHEDULER,EXECUTING,COMPLETE}
public static String COLL_JOB="job";
public static String COLL_JOB_EXEC_HISTORY="job_exec_history";
public static String COLL_JOB_LIFECYCLE_HISTORY="job_lifecycle_history";
public static boolean isExistsOfJob(String taskId,String jobGroup,String jobName){
BasicDBObject query=new BasicDBObject();
query.append("taskId", taskId)
.append("jobGroup", jobGroup)
.append("jobName", jobName);
long count=DBUtil.getColl(COLL_JOB).count(query);
return count>0?true:false;
}
public static JobExecutedEntity getJob(String jobId){
BasicDBObject query=new BasicDBObject();
query.append("_id", jobId);
BasicDBObject basicDBObject=(BasicDBObject)DBUtil.getColl(COLL_JOB).findOne(query);
if(basicDBObject==null){
return null;
}
JobExecutedEntity jee=new JobExecutedEntity();
jee.setAppName(basicDBObject.getString("appName"));
jee.setJobGroup(basicDBObject.getString("jobGroup"));
jee.setJobName(basicDBObject.getString("jobName"));
jee.setCronExpression(basicDBObject.getString("cronExpression"));
jee.setJobParam(basicDBObject.getString("jobParam"));
jee.setJobClass(basicDBObject.getString("jobClass"));
jee.setJobDescription(basicDBObject.getString("jobDescription"));
jee.setPriority(basicDBObject.getInt("priority"));
jee.setStopExecutingWhenException(basicDBObject.getBoolean("stopExecutingWhenException"));
jee.setFireTime(basicDBObject.getDate("fireTime"));
jee.setFinalFireTime(basicDBObject.getDate("finalFireTime"));
jee.setNextFireTime(basicDBObject.getDate("nextFireTime"));
jee.setRuntime(basicDBObject.getLong("runtime"));
jee.setStatus(basicDBObject.getString("status"));
jee.setStartTime(basicDBObject.getDate("startTime"));
jee.setEndTime(basicDBObject.getDate("endTime"));
jee.setErrorMessage(basicDBObject.getString("errorMessage"));
jee.setCreateTime(basicDBObject.getDate("createTime"));
jee.setUpdateTime(basicDBObject.getDate("updateTime"));
jee.setJobId(basicDBObject.getString("_id"));
jee.setTaskId(basicDBObject.getString("taskId"));
return jee;
}
public static JobExecutedEntity getJob(String groupName, String jobName) {
BasicDBObject query = new BasicDBObject();
query.append("appName", TaskConfig.appName)
.append("jobGroup", groupName)
.append("jobName", jobName);
BasicDBObject basicDBObject = (BasicDBObject) DBUtil.getColl(COLL_JOB).findOne(query);
if (basicDBObject == null) {
return null;
}
JobExecutedEntity jee = new JobExecutedEntity();
jee.setAppName(basicDBObject.getString("appName"));
jee.setJobGroup(basicDBObject.getString("jobGroup"));
jee.setJobName(basicDBObject.getString("jobName"));
jee.setCronExpression(basicDBObject.getString("cronExpression"));
jee.setJobParam(basicDBObject.getString("jobParam"));
jee.setJobClass(basicDBObject.getString("jobClass"));
jee.setJobDescription(basicDBObject.getString("jobDescription"));
jee.setPriority(basicDBObject.getInt("priority"));
jee.setStopExecutingWhenException(basicDBObject.getBoolean("stopExecutingWhenException"));
jee.setFireTime(basicDBObject.getDate("fireTime"));
jee.setFinalFireTime(basicDBObject.getDate("finalFireTime"));
jee.setNextFireTime(basicDBObject.getDate("nextFireTime"));
jee.setRuntime(basicDBObject.getLong("runtime"));
jee.setStatus(basicDBObject.getString("status"));
jee.setStartTime(basicDBObject.getDate("startTime"));
jee.setEndTime(basicDBObject.getDate("endTime"));
jee.setErrorMessage(basicDBObject.getString("errorMessage"));
jee.setCreateTime(basicDBObject.getDate("createTime"));
jee.setUpdateTime(basicDBObject.getDate("updateTime"));
jee.setJobId(basicDBObject.getString("_id"));
jee.setTaskId(basicDBObject.getString("taskId"));
return jee;
}
public static List getJobs(String appName){
BasicDBObject query=new BasicDBObject();
query.append("appName", appName);
DBCursor dbCursor=null;
try{
List result=new ArrayList();
dbCursor=DBUtil.getColl(COLL_JOB).find(query);
while(dbCursor.hasNext()){
JobExecutedEntity jee=new JobExecutedEntity();
BasicDBObject basicDBObject=(BasicDBObject)dbCursor.next();
jee.setAppName(basicDBObject.getString("appName"));
jee.setJobGroup(basicDBObject.getString("jobGroup"));
jee.setJobName(basicDBObject.getString("jobName"));
jee.setCronExpression(basicDBObject.getString("cronExpression"));
jee.setJobParam(basicDBObject.getString("jobParam"));
jee.setJobClass(basicDBObject.getString("jobClass"));
jee.setJobDescription(basicDBObject.getString("jobDescription"));
jee.setPriority(basicDBObject.getInt("priority"));
jee.setStopExecutingWhenException(basicDBObject.getBoolean("stopExecutingWhenException"));
jee.setFireTime(basicDBObject.getDate("fireTime"));
jee.setFinalFireTime(basicDBObject.getDate("finalFireTime"));
jee.setNextFireTime(basicDBObject.getDate("nextFireTime"));
jee.setRuntime(basicDBObject.getLong("runtime"));
jee.setStatus(basicDBObject.getString("status"));
jee.setStartTime(basicDBObject.getDate("startTime"));
jee.setEndTime(basicDBObject.getDate("endTime"));
jee.setErrorMessage(basicDBObject.getString("errorMessage"));
jee.setCreateTime(basicDBObject.getDate("createTime"));
jee.setUpdateTime(basicDBObject.getDate("updateTime"));
jee.setJobId(basicDBObject.getString("_id"));
jee.setTaskId(basicDBObject.getString("taskId"));
result.add(jee);
}
return result;
}catch(Exception e){
e.printStackTrace();
throw new WriteToDataBaseException(e.getMessage());
}finally{
DBUtil.closeDBCursor(dbCursor);
}
}
public static int saveJob(JobExecutedEntity jee){
BasicDBObject field=new BasicDBObject();
//String _id="JOB"+String.format("%10d",new Object[]{DBUtil.nextValue("jobId")});
field.append("_id", jee.getJobId());
field.append("taskId",jee.getTaskId());
field.append("appName", jee.getAppName());
field.append("jobGroup", jee.getJobGroup());
field.append("jobName", jee.getJobName());
field.append("cronExpression", jee.getCronExpression());
field.append("jobParam", jee.getJobParam());
field.append("jobClass", jee.getJobClass());
field.append("jobDescription", jee.getJobDescription());
field.append("priority", jee.getPriority());
field.append("stopExecutingWhenException", jee.isStopExecutingWhenException());
field.append("fireTime", jee.getFireTime());
field.append("finalFireTime", jee.getFinalFireTime());
field.append("nextFireTime", jee.getNextFireTime());
field.append("runtime", jee.getRuntime());
field.append("status", jee.getStatus());
field.append("startTime", jee.getStartTime());
field.append("endTime", jee.getEndTime());
field.append("errorMessage", jee.getErrorMessage());
field.append("createTime", new Date());
WriteResult writeResult=DBUtil.getColl(COLL_JOB).save(field);
return writeResult.getN();
}
public static int deleteJob(String jobId){
BasicDBObject query=new BasicDBObject();
query.append("_id", jobId);
WriteResult writeResult=DBUtil.getColl(COLL_JOB).remove(query);
return writeResult.getN();
}
public static int updateJob(JobExecutedEntity jee){
BasicDBObject query=new BasicDBObject();
query.append("_id", jee.getJobId());
BasicDBObject field=new BasicDBObject();
field.append("cronExpression", jee.getCronExpression());
field.append("jobParam", jee.getJobParam());
field.append("jobClass", jee.getJobClass());
field.append("jobDescription", jee.getJobDescription());
field.append("priority", jee.getPriority());
field.append("stopExecutingWhenException", jee.isStopExecutingWhenException());
field.append("fireTime", jee.getFireTime());
field.append("finalFireTime", jee.getFinalFireTime());
field.append("nextFireTime", jee.getNextFireTime());
field.append("runtime", jee.getRuntime());
field.append("status", jee.getStatus());
field.append("startTime", jee.getStartTime());
field.append("endTime", jee.getEndTime());
field.append("errorMessage", jee.getErrorMessage());
field.append("updateTime", new Date());
WriteResult writeResult=DBUtil.getColl(COLL_JOB).update(query, new BasicDBObject("$set",field));
logger.debug("DataBaseService do update======="+jee.getJobParam());
return writeResult.getN();
}
public static int saveJobExecHistory(JobExecutedEntity jee){
BasicDBObject field=new BasicDBObject();
field.append("appName", jee.getAppName());
field.append("jobGroup", jee.getJobGroup());
field.append("jobName", jee.getJobName());
field.append("cronExpression", jee.getCronExpression());
field.append("jobParam", jee.getJobParam());
field.append("jobClass", jee.getJobClass());
field.append("jobDescription", jee.getJobDescription());
field.append("priority", jee.getPriority());
field.append("stopExecutingWhenException", jee.isStopExecutingWhenException());
field.append("fireTime", jee.getFireTime());
field.append("finalFireTime", jee.getFinalFireTime());
field.append("nextFireTime", jee.getNextFireTime());
field.append("runtime", jee.getRuntime());
field.append("status", jee.getStatus());
field.append("startTime", jee.getStartTime());
field.append("endTime", jee.getEndTime());
field.append("errorMessage", jee.getErrorMessage());
field.append("createTime", new Date());
field.append("jobId", jee.getJobId());
WriteResult writeResult=DBUtil.getColl(COLL_JOB_EXEC_HISTORY).save(field);
return writeResult.getN();
}
public static int deleteJobExecHistory(String jobId){
BasicDBObject query=new BasicDBObject();
query.append("jobId", jobId);
WriteResult writeResult=DBUtil.getColl(COLL_JOB_EXEC_HISTORY).remove(query);
return writeResult.getN();
}
public static int saveJobLifecycleHistory(JobEntity jee,String action){
BasicDBObject field=new BasicDBObject();
field.append("appName", jee.getAppName());
field.append("jobGroup", jee.getJobGroup());
field.append("jobName", jee.getJobName());
field.append("cronExpression", jee.getCronExpression());
field.append("jobParam", jee.getJobParam());
field.append("jobClass", jee.getJobClass());
field.append("jobDescription", jee.getJobDescription());
field.append("priority", jee.getPriority());
field.append("stopExecutingWhenException", jee.isStopExecutingWhenException());
field.append("action", action);
field.append("createTime", new Date());
field.append("jobId", jee.getJobId());
WriteResult writeResult=DBUtil.getColl(COLL_JOB_LIFECYCLE_HISTORY).save(field);
return writeResult.getN();
}
public static int deleteJobLifecycleHistory(String jobId){
BasicDBObject query=new BasicDBObject();
query.append("jobId", jobId);
WriteResult writeResult=DBUtil.getColl(COLL_JOB_LIFECYCLE_HISTORY).remove(query);
return writeResult.getN();
}
public static Map getJobLifecycleHistory(int pageNo, int pageSize,
String jobGroup, String jobName, Date beginTime, Date endtime,String action,String jobId) {
Pagination pagination=new Pagination();
pagination.setPageNo(pageNo);
pagination.setPageSize(pageSize);
BasicDBList andList=new BasicDBList();
if(!StringUtil.isEmpty(jobGroup)){
andList.add(new BasicDBObject("jobGroup", jobGroup));
}
if(!StringUtil.isEmpty(jobName)){
andList.add(new BasicDBObject("jobName", jobName));
}
if(!StringUtil.isEmpty(jobId)){
andList.add(new BasicDBObject("jobId", jobId));
}
if(!StringUtil.isEmpty(action)){
andList.add(new BasicDBObject("action", action));
}
if(beginTime!=null){
andList.add(new BasicDBObject("createTime", new BasicDBObject("$gte",beginTime)));
}
if(endtime!=null){
andList.add(new BasicDBObject("createTime", new BasicDBObject("$lte",endtime)));
}
BasicDBObject query=new BasicDBObject();
query.append(QueryOperators.AND, andList);
long count=DBUtil.getColl(COLL_JOB_EXEC_HISTORY).count(query);
pagination.setTotal(Long.valueOf(count).intValue());
BasicDBObject sort = new BasicDBObject("createTime",-1);
List data=new ArrayList();
DBCursor dbCursor=null;
try{
dbCursor=DBUtil.getColl(COLL_JOB_LIFECYCLE_HISTORY).find(query).sort(sort).skip((pageNo-1)*pageSize).limit(pageSize);
while(dbCursor.hasNext()){
BasicDBObject history=(BasicDBObject)dbCursor.next();
JobLifecycleHistory jee=ObjectDBObjectUtil.convertToObject(JobLifecycleHistory.class, history);
data.add(jee);
}
}finally{
if(dbCursor!=null){dbCursor.close();}
}
Map map=new HashMap();
map.put("page", pagination);
map.put("list", data);
return map;
}
public static Map getJobExecutedHistory(int pageNo, int pageSize,
String jobGroup, String jobName, Date beginTime, Date endTime,String jobId) {
Pagination pagination=new Pagination();
pagination.setPageNo(pageNo);
pagination.setPageSize(pageSize);
BasicDBList andList=new BasicDBList();
if(!StringUtil.isEmpty(jobGroup)){
andList.add(new BasicDBObject("jobGroup", jobGroup));
}
if(!StringUtil.isEmpty(jobName)){
andList.add(new BasicDBObject("jobName", jobName));
}
if(!StringUtil.isEmpty(jobId)){
andList.add(new BasicDBObject("jobId", jobId));
}
if(beginTime!=null){
andList.add(new BasicDBObject("finalFireTime", new BasicDBObject("$gte",beginTime)));
}
if(endTime!=null){
andList.add(new BasicDBObject("finalFireTime", new BasicDBObject("$lte",endTime)));
}
BasicDBObject query=new BasicDBObject();
query.append(QueryOperators.AND, andList);
long count=DBUtil.getColl(COLL_JOB_EXEC_HISTORY).count(query);
pagination.setTotal(Long.valueOf(count).intValue());
BasicDBObject sort = new BasicDBObject("finalFireTime",-1);
List data=new ArrayList();
DBCursor dbCursor=null;
try{
dbCursor=DBUtil.getColl(COLL_JOB_EXEC_HISTORY).find(query).sort(sort).skip((pageNo-1)*pageSize).limit(pageSize);
while(dbCursor.hasNext()){
BasicDBObject history=(BasicDBObject)dbCursor.next();
JobExecutedEntity jee=ObjectDBObjectUtil.convertToObject(JobExecutedEntity.class, history);
data.add(jee);
}
}finally{
if(dbCursor!=null){dbCursor.close();}
}
Map map=new HashMap();
map.put("page", pagination);
map.put("list", data);
return map;
}
}