ManualOprUtil.java
6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.sitech.jmx.adaptor;
import com.sitech.ismp.coll.cron.CronConstants;
import com.sitech.ismp.coll.cron.CronSchedule;
import com.sitech.ismp.messageObject.AgentSyncObject;
import com.sitech.ismp.messageObject.ScheduleLog;
import com.sitech.jmx.manage.CollScheduleManager;
import com.sitech.jmx.manage.MBeanManager;
import com.sitech.jmx.mbean.ConfigSync;
import com.sitech.schedule.Scheduler;
import com.sitech.schedule.SchedulerService;
import com.sitech.schedule.bean.Job;
import com.sitech.util.Formater;
import com.sitech.util.mq.MQConstants;
import com.sitech.util.mq.TunnelFactory;
import org.apache.log4j.Logger;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
* User: Linxc
* Date: 14-3-15
* Time: 下午1:17
* To change this template use File | Settings | File Templates.
*/
public class ManualOprUtil {
public static Logger logger = Logger.getLogger(ManualOprUtil.class);
/**
* 执行手动触发的任务
* @param agentSyncObj
*/
public static void doManualOperation(AgentSyncObject agentSyncObj) {
Scheduler scheduler = new Scheduler(agentSyncObj);
// 若MBean未注册,则先创建MBean实例
MBeanManager.getInstance().registCollMBean(scheduler);
if ("com.sitech.ismp.coll.cron.CronSchedule".equals(scheduler.getClassName())) {
execCronSchedule(agentSyncObj);
} else {
sendScheduleLog(agentSyncObj, "agent接收到脚本验证消息!", CronConstants.SEQ_0, "", null);
Job job = new Job();
job.setId(scheduler.getScheduleId());
job.setObjectName(scheduler.getObjectName());
job.setOperation(scheduler.getFunction());
job.setParams(scheduler.getParams());
SchedulerService.getInstance().addJob(job);
}
}
/**
* 是否为手动触发
*/
public static boolean isManualTriger(AgentSyncObject agentSyncObj) {
String trigerType = agentSyncObj.getTriggerType();
if(null != trigerType && "MANUAL".equals(trigerType)){
return true;
}
return false;
}
public static void execCronSchedule(AgentSyncObject agentSyncObj){
String oprType = agentSyncObj.getOperateType();
int type = Integer.valueOf(oprType);
CronSchedule schedule = new CronSchedule();
String endTime = "";
Map<String, Object> extInfo = new HashMap<String, Object>();
try {
switch (type) {
case 1://脚本测试
sendScheduleLog(agentSyncObj, "agent接收到脚本验证消息!", CronConstants.SEQ_0, endTime, extInfo);
schedule.checkScript(agentSyncObj.getParams());
break;
case 2://调度任务同步
extInfo.put("SCHEDULE_FLAG", "Y");
sendScheduleLog(agentSyncObj, "agent接收到调度消息!", CronConstants.SEQ_0, endTime, extInfo);
SchSyncUtil.doSyncOperation(agentSyncObj);
break;
case 3://脚本下发
extInfo.put("SCHEDULE_FLAG", "Y");
sendScheduleLog(agentSyncObj, "agent接收到下发脚本消息", CronConstants.SEQ_0, endTime, extInfo);
schedule.scriptSync2Agent(agentSyncObj.getParams());
updateCronSchedule(agentSyncObj);
break;
case 4://附件下发
sendScheduleLog(agentSyncObj, "agent接收到下发附件消息", CronConstants.SEQ_0, endTime, extInfo);
schedule.ftpHostAttachment(agentSyncObj.getParams());
break;
case 5://附件验证
sendScheduleLog(agentSyncObj, "agent接收到文件验证消息", CronConstants.SEQ_0, endTime, extInfo);
schedule.checkDirFileExist(agentSyncObj.getParams());
break;
default:
logger.error("调度任务[" + agentSyncObj.getScheduleId() + "]未指定操作类型(OperateType=" +
agentSyncObj.getOperateType() + ")!", new RuntimeException());
sendScheduleLog(agentSyncObj, "调度任务[" + agentSyncObj.getScheduleId() +
"]未指定操作类型(OperateType=" + agentSyncObj.getOperateType() + ")!", CronConstants.SEQ_0, endTime, extInfo);
}
} catch (Exception e) {
logger.error("Exception while dispatch CronSchedule:Sch_id[" + agentSyncObj.getScheduleId() + "],Request_id["
+ agentSyncObj.getRequestId() + "],Operate_type[" + agentSyncObj.getOperateType() + "]", e);
}
}
private static void sendScheduleLog(AgentSyncObject agentSyncObj, String logContent, String seq, String endTime, Map<String, Object> extInfo) {
logger.info("[Cron Schedule] LogInfo:" + logContent);
ScheduleLog log = new ScheduleLog();
log.setLogType(CronConstants.SCHEDULE_LOG_MANUAL);
String beginTime = Formater.datetimeToString(new Date(), "yyyy-MM-dd HH:mm:ss");
if (null != endTime && !"".equals(endTime)) {
log.setEndTime(endTime);
}
log.setAgentId(agentSyncObj.getAgentId());
log.setSchId(agentSyncObj.getScheduleId());
log.setRequestId(agentSyncObj.getRequestId());
log.setSeq(seq);
log.setType("std_out");
log.setOperateType(agentSyncObj.getOperateType());
log.setLogInfo(logContent);
log.setBeginTime(beginTime);
extInfo = (null == extInfo ? new HashMap<String, Object>() : extInfo);
log.setExtInfo(extInfo);
log.setTrigerType(agentSyncObj.getTriggerType());
TunnelFactory.getTunnel(MQConstants.Q_ROPORT_FROM_AGENT).writeData(log);
}
public static void updateCronSchedule(AgentSyncObject obj) {
Map<String, Object> extInfo = new HashMap<String, Object>();
String scriptSyncStatus = CronConstants.SCRIPT_EXEC_SUCCESS;
String endTime = Formater.datetimeToString(new Date(), "yyyy-MM-dd HH:mm:ss");
try {
boolean flag = CollScheduleManager.getInstance().scheduleExist(obj.getScheduleId());
if (flag) {
//如果quartz里已经存在该任务,开始更新quartz参数
sendScheduleLog(obj, "agent开始更新调度任务脚本!", CronConstants.SEQ_3, "", extInfo);
ConfigSync cfs = new ConfigSync();
Map<String, Object> datas = cfs.updateCollSchedule(obj);
sendScheduleLog(obj, "agent更新调度任务脚本成功!", CronConstants.SEQ_4, "", extInfo);
}
} catch (Exception e) {
logger.error("Exception while updateCronSchedule", e);
scriptSyncStatus = "0";
}
extInfo.put("SCRIPT_SYNC_STATUS", scriptSyncStatus);
sendScheduleLog(obj, "agent同步脚本结束!", CronConstants.SEQ_5, endTime, extInfo);
}
}