396 lines
12 KiB
Java
396 lines
12 KiB
Java
package com.tenwa.message;
|
||
|
||
import java.util.Date;
|
||
import java.util.HashMap;
|
||
import java.util.Map;
|
||
|
||
import org.quartz.CronExpression;
|
||
import org.quartz.CronScheduleBuilder;
|
||
import org.quartz.JobBuilder;
|
||
import org.quartz.JobDataMap;
|
||
import org.quartz.JobDetail;
|
||
import org.quartz.JobKey;
|
||
import org.quartz.ListenerManager;
|
||
import org.quartz.Scheduler;
|
||
import org.quartz.SchedulerException;
|
||
import org.quartz.Trigger;
|
||
import org.quartz.TriggerBuilder;
|
||
import org.quartz.TriggerKey;
|
||
import org.quartz.impl.StdSchedulerFactory;
|
||
import org.quartz.impl.matchers.KeyMatcher;
|
||
|
||
import jbo.sys.MSG_CONFIG;
|
||
import jbo.sys.QRTZ_TRIGGERS;
|
||
|
||
import com.amarsoft.are.jbo.BizObject;
|
||
import com.amarsoft.are.jbo.JBOException;
|
||
import com.amarsoft.are.jbo.JBOFactory;
|
||
import com.amarsoft.are.jbo.JBOTransaction;
|
||
import com.tenwa.quartz.DateUtil;
|
||
import com.tenwa.quartz.JobListenerImpl;
|
||
import com.tenwa.quartz.TriggerListenerImpl;
|
||
import com.tenwa.reckon.util.UUIDUtil;
|
||
|
||
public class QuartzMessageManager {
|
||
private String msgType;
|
||
private String type;
|
||
private String id;
|
||
private String jobName;
|
||
private String triggerName;
|
||
private String msgtypename;
|
||
private String cronExpression;
|
||
private String durabledays;
|
||
private String isoverdateexecute;
|
||
private String triggerState;
|
||
private String msgSql;
|
||
private String triggermode;
|
||
private String msgcontent;
|
||
private String msgsqlparam;
|
||
private String msgsendtype;
|
||
private String msgsendtime;
|
||
private String delayday;
|
||
private String isrepeatbydate;
|
||
private String userID;
|
||
private Scheduler scheduler;
|
||
|
||
public QuartzMessageManager(){
|
||
try {
|
||
this.scheduler = StdSchedulerFactory.getDefaultScheduler();
|
||
} catch (SchedulerException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}
|
||
|
||
public String getId() {
|
||
return id;
|
||
}
|
||
|
||
public void setId(String id) {
|
||
this.id = id;
|
||
}
|
||
|
||
public String getType() {
|
||
return type;
|
||
}
|
||
|
||
public void setType(String type) {
|
||
this.type = type;
|
||
}
|
||
|
||
public String getMsgType() {
|
||
return msgType;
|
||
}
|
||
|
||
public void setMsgType(String msgType) {
|
||
this.msgType = msgType;
|
||
}
|
||
|
||
public String getJobName() {
|
||
return jobName;
|
||
}
|
||
|
||
public void setJobName(String jobName) {
|
||
this.jobName = jobName;
|
||
}
|
||
|
||
public String getMsgtypename() {
|
||
return msgtypename;
|
||
}
|
||
|
||
public void setMsgtypename(String msgtypename) {
|
||
this.msgtypename = msgtypename;
|
||
}
|
||
|
||
public String getIsoverdateexecute() {
|
||
return isoverdateexecute;
|
||
}
|
||
|
||
public void setIsoverdateexecute(String isoverdateexecute) {
|
||
this.isoverdateexecute = isoverdateexecute;
|
||
}
|
||
|
||
public Scheduler getScheduler() {
|
||
return scheduler;
|
||
}
|
||
|
||
public void setScheduler(Scheduler scheduler) {
|
||
this.scheduler = scheduler;
|
||
}
|
||
|
||
public String getTriggerName() {
|
||
return triggerName;
|
||
}
|
||
|
||
public void setTriggerName(String triggerName) {
|
||
this.triggerName = triggerName;
|
||
}
|
||
|
||
public String getCronExpression() {
|
||
return cronExpression;
|
||
}
|
||
|
||
public void setCronExpression(String cronExpression) {
|
||
this.cronExpression = cronExpression;
|
||
}
|
||
|
||
public String getDurabledays() {
|
||
return durabledays;
|
||
}
|
||
|
||
public void setDurabledays(String durabledays) {
|
||
this.durabledays = durabledays;
|
||
}
|
||
|
||
public String getTriggerState() {
|
||
return triggerState;
|
||
}
|
||
|
||
public void setTriggerState(String triggerState) {
|
||
this.triggerState = triggerState;
|
||
}
|
||
|
||
public String getMsgSql() {
|
||
return msgSql;
|
||
}
|
||
|
||
public void setMsgSql(String msgSql) {
|
||
this.msgSql = msgSql;
|
||
}
|
||
|
||
public String getTriggermode() {
|
||
return triggermode;
|
||
}
|
||
|
||
public void setTriggermode(String triggermode) {
|
||
this.triggermode = triggermode;
|
||
}
|
||
|
||
public String getMsgcontent() {
|
||
return msgcontent;
|
||
}
|
||
|
||
public void setMsgcontent(String msgcontent) {
|
||
this.msgcontent = msgcontent;
|
||
}
|
||
|
||
public String getMsgsqlparam() {
|
||
return msgsqlparam;
|
||
}
|
||
|
||
public void setMsgsqlparam(String msgsqlparam) {
|
||
this.msgsqlparam = msgsqlparam;
|
||
}
|
||
|
||
public String getMsgsendtype() {
|
||
return msgsendtype;
|
||
}
|
||
|
||
public void setMsgsendtype(String msgsendtype) {
|
||
this.msgsendtype = msgsendtype;
|
||
}
|
||
|
||
public String getMsgsendtime() {
|
||
return msgsendtime;
|
||
}
|
||
|
||
public void setMsgsendtime(String msgsendtime) {
|
||
this.msgsendtime = msgsendtime;
|
||
}
|
||
|
||
public String getDelayday() {
|
||
return delayday;
|
||
}
|
||
|
||
public void setDelayday(String delayday) {
|
||
this.delayday = delayday;
|
||
}
|
||
|
||
public String getIsrepeatbydate() {
|
||
return isrepeatbydate;
|
||
}
|
||
|
||
public void setIsrepeatbydate(String isrepeatbydate) {
|
||
this.isrepeatbydate = isrepeatbydate;
|
||
}
|
||
|
||
public String getUserID() {
|
||
return userID;
|
||
}
|
||
|
||
public void setUserID(String userID) {
|
||
this.userID = userID;
|
||
}
|
||
|
||
public String checkMsgType(JBOTransaction tx) throws Exception{
|
||
BizObject boMC = null;
|
||
if("add".equals(type)){
|
||
boMC = JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME)
|
||
.createQuery("select O.MSG_TYPE from jbo.sys.MSG_CONFIG O where O.MSG_TYPE='"+msgType+"'").getSingleResult(false);
|
||
}else if("edit".equals(type)){
|
||
boMC = JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME)
|
||
.createQuery("select O.MSG_TYPE from O where O.MSG_TYPE not in(select mc.msg_type from jbo.sys.MSG_CONFIG mc where mc.id='"+id+"') and O.MSG_TYPE='"+msgType+"'").getSingleResult(false);
|
||
}
|
||
if(boMC != null){
|
||
return "FAILED";
|
||
}else{
|
||
return "SUCCESS";
|
||
}
|
||
}
|
||
|
||
public String saveOrUpdateJobAndTrigger(JBOTransaction tx) throws JBOException{
|
||
BizObject boMC = JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("id=:id").setParameter("id", id).getSingleResult(false);
|
||
msgSql = boMC.getAttribute("msg_sql").getString();
|
||
Map<String,String> data = new HashMap<String,String>();
|
||
data.put("id", id);
|
||
data.put("msgSql", msgSql);
|
||
data.put("msgsqlparam", msgsqlparam);
|
||
data.put("msgcontent", msgcontent);
|
||
data.put("triggermode", triggermode);
|
||
data.put("msgsendtype", msgsendtype);
|
||
data.put("msgsendtime", msgsendtime);
|
||
data.put("isoverdateexecute", isoverdateexecute);
|
||
data.put("msgType", msgType);
|
||
data.put("isrepeatbydate", isrepeatbydate);
|
||
data.put("userID", userID);
|
||
JobDataMap jdm = new JobDataMap(data);
|
||
try {
|
||
JobDetail jobDetail = null;
|
||
if(jobName != null){
|
||
jobDetail = scheduler.getJobDetail(JobKey.jobKey(this.jobName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
if(jobDetail != null){
|
||
scheduler.deleteJob(JobKey.jobKey(this.jobName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
}
|
||
jobName = UUIDUtil.getUUID();
|
||
}else{
|
||
jobName = UUIDUtil.getUUID();
|
||
}
|
||
|
||
Class jobClass = Class.forName("com.tenwa.message.QuartzJobMsg");
|
||
jobDetail = JobBuilder.newJob(jobClass).withIdentity(jobName, QuartzJobMsg.MSG_GROUP_NAME)
|
||
.withDescription(msgtypename)
|
||
.requestRecovery("1".equals(isoverdateexecute)?true:false)
|
||
.storeDurably(true)
|
||
.usingJobData(jdm)
|
||
.build();
|
||
|
||
if(triggerName != null){
|
||
Trigger trigger = scheduler.getTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
if(trigger != null){
|
||
scheduler.unscheduleJob(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
}
|
||
triggerName = UUIDUtil.getUUID();
|
||
}else{
|
||
triggerName = UUIDUtil.getUUID();
|
||
}
|
||
if(msgType.startsWith("holiday") || msgType.startsWith("birthday")){//<2F>ڼ<EFBFBD><DABC>պ<EFBFBD><D5BA><EFBFBD><EFBFBD><EFBFBD>ף<EFBFBD><D7A3><EFBFBD>Ķ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡϵͳ<CFB5>Ľڼ<C4BD><DABC><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
|
||
cronExpression = "0 0 0 * * ? *";
|
||
}
|
||
CronExpression con = new CronExpression(cronExpression);
|
||
Date startTime = new Date();
|
||
startTime = new Date(startTime.getTime()+1000);
|
||
int days = Integer.parseInt(durabledays);
|
||
Date endTime = DateUtil.getEndDateByDays(startTime, days);
|
||
Trigger trigger = TriggerBuilder.newTrigger().withDescription(msgtypename)
|
||
.withIdentity(triggerName, QuartzJobMsg.MSG_GROUP_NAME)
|
||
.withSchedule(CronScheduleBuilder.cronSchedule(con).withMisfireHandlingInstructionDoNothing())
|
||
.startAt(startTime).endAt(endTime)
|
||
.forJob(jobName, QuartzJobMsg.MSG_GROUP_NAME)
|
||
.build();
|
||
|
||
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("update O set cron_expression='"+cronExpression+"',start_date='"+DateUtil.getDateTime(startTime)+"',end_date='"+DateUtil.getDateTime(endTime)+"',jobname='"+jobName+"',triggername='"+triggerName+"' where id='"+id+"'").executeUpdate();
|
||
scheduler.scheduleJob(jobDetail,trigger);
|
||
ListenerManager lm = scheduler.getListenerManager();
|
||
KeyMatcher<JobKey> keyMetcher = KeyMatcher.keyEquals(JobKey.jobKey(jobName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
lm.addJobListener(new JobListenerImpl(), keyMetcher);
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
if(!"ACQUIRED".equals(triggerState)){
|
||
try {
|
||
scheduler.pauseTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
} catch (SchedulerException e) {
|
||
e.printStackTrace();
|
||
}
|
||
}else{
|
||
try {
|
||
scheduler.resumeTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
} catch (SchedulerException e) {
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
|
||
public String resumeTrigger(JBOTransaction tx){
|
||
try {
|
||
scheduler.resumeTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
JBOFactory.getBizObjectManager(QRTZ_TRIGGERS.CLASS_NAME,tx).createQuery("UPDATE O SET NEXT_FIRE_TIME = :Date WHERE TRIGGER_NAME = :triggerName AND TRIGGER_GROUP = :triggerGroup AND JOB_NAME = :jobName AND JOB_GROUP = :jobGroup")
|
||
.setParameter("Date", new Date().getTime()-1000).setParameter("triggerName", triggerName).setParameter("triggerGroup", QuartzJobMsg.MSG_GROUP_NAME).setParameter("jobName", jobName).setParameter("jobGroup", QuartzJobMsg.MSG_GROUP_NAME).executeUpdate();
|
||
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("update O set NEXT_FIRE_TIME =:Date,MSG_STATUS='"+triggerState+"' where id=:id").setParameter("Date", new Date().getTime()-1000).setParameter("id", id).executeUpdate();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
|
||
public String pauseTrigger(JBOTransaction tx){
|
||
try {
|
||
scheduler.pauseTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("update O set MSG_STATUS='"+triggerState+"' where id=:id").setParameter("id", id).executeUpdate();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
|
||
public String triggerJob(JBOTransaction tx){
|
||
try {
|
||
scheduler.triggerJob(JobKey.jobKey(jobName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
JBOFactory.getBizObjectManager(QRTZ_TRIGGERS.CLASS_NAME).createQuery("UPDATE O SET PREV_FIRE_TIME = :Date,NEXT_FIRE_TIME = null WHERE TRIGGER_NAME = :triggerName AND TRIGGER_GROUP = :triggerGroup AND JOB_NAME = :jobName AND JOB_GROUP = :jobGroup")
|
||
.setParameter("Date", new Date().getTime()).setParameter("triggerName", triggerName).setParameter("triggerGroup", QuartzJobMsg.MSG_GROUP_NAME).setParameter("jobName", jobName).setParameter("jobGroup", QuartzJobMsg.MSG_GROUP_NAME).executeUpdate();
|
||
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("update O set PREVIOUS_FIRE_TIME = :Date,NEXT_FIRE_TIME = null where id=:id").setParameter("Date", new Date().getTime()).setParameter("id", id).executeUpdate();
|
||
} catch (Exception e) {
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
|
||
public String updateCronExpress(JBOTransaction tx){
|
||
try{
|
||
if(triggerName != null){
|
||
Trigger trigger = scheduler.getTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
if(trigger != null){
|
||
scheduler.unscheduleJob(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
}
|
||
triggerName = UUIDUtil.getUUID();
|
||
}else{
|
||
triggerName = UUIDUtil.getUUID();
|
||
}
|
||
CronExpression con = new CronExpression(cronExpression);
|
||
Date startTime = new Date();
|
||
startTime = new Date(startTime.getTime()+1000);
|
||
int days = Integer.parseInt(durabledays);
|
||
Date endTime = DateUtil.getEndDateByDays(startTime, days);
|
||
Trigger trigger = TriggerBuilder.newTrigger().withDescription(msgtypename)
|
||
.withIdentity(triggerName, QuartzJobMsg.MSG_GROUP_NAME)
|
||
.withSchedule(CronScheduleBuilder.cronSchedule(con).withMisfireHandlingInstructionDoNothing())
|
||
.startAt(startTime).endAt(endTime)
|
||
.forJob(jobName, QuartzJobMsg.MSG_GROUP_NAME)
|
||
.build();
|
||
|
||
JBOFactory.getBizObjectManager(MSG_CONFIG.CLASS_NAME,tx).createQuery("update O set cron_expression='"+cronExpression+"',start_date='"+DateUtil.getDateTime(startTime)+"',end_date='"+DateUtil.getDateTime(endTime)+"',jobname='"+jobName+"',triggername='"+triggerName+"' where id='"+id+"'").executeUpdate();
|
||
scheduler.scheduleJob(trigger);
|
||
scheduler.pauseTrigger(TriggerKey.triggerKey(triggerName, QuartzJobMsg.MSG_GROUP_NAME));
|
||
}catch(Exception e){
|
||
e.printStackTrace();
|
||
return "ERROR";
|
||
}
|
||
return "SUCCESS";
|
||
}
|
||
}
|