摘要:Spring+XML+Scheduler
.請在Spring的Confige檔中(寫在bean-config.xml或是mvc-confige.xml都可以)加入底下的設定
(1) 設定要把那一支程式作為排程使用 qix310WJobBean為自己定義的名稱
gov.fdc.qix.scheduler.Qix310WScheduler 這支就是要自己客製的排程
<bean name="qix310WJobBean" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="gov.fdc.qix.scheduler.Qix310WScheduler" />
</bean>
(2) 該排程要執行的時間週期, 底下為每天晚上十一點執行一次 qix310WSchedulerTrigger為自己定義的名稱
<bean id="qix310WSchedulerTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="qix310WJobBean" />
<property name="cronExpression">
<value>0 0 23 * * ? </value>
</property>
</bean>
(3) 將排程assign給spring的SchedulerFactoryBean
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="triggers"> <list> <ref bean="qix310WSchedulerTrigger" /> </list> </property> <property name="applicationContextSchedulerContextKey" value="applicationContext" /> </bean>
2.程式請參考附件Qix310WScheduler.java
需覆寫 executeInternal method
其中底下一定要寫, 如果你要call像managerImpl的bean物件來存取db時會用到
private ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
Qix310WScheduler.java
==============================================================================================
package gov.fdc.qix.scheduler;
import gov.fdc.qix.service.Qix310WJobManager;
import gov.fdc.qix.util.EipRtnMsgTagName;
import gov.fdc.qix.util.QixDateUtil;
import gov.fdc.qix.util.QixSqlUtil;
import gov.fdc.qix.util.QixXmlParseUtil;
import gov.fdc.qix.util.WSUtil;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.ApplicationContext;
import org.springframework.scheduling.quartz.QuartzJobBean;
/**
* 程式資訊摘要:Qix310WScheduler.java
* 類別名稱 :Qix310WScheduler.java
* 程式內容說明:Qix310WScheduler.java
* 程式修改記錄:2012/10/9
* @author EdwardChen
* @version 2.0
* @since 1.0
*/
public class Qix310WScheduler extends QuartzJobBean implements Serializable {
private static final long serialVersionUID = 3182593910668545699L;
private ApplicationContext applicationContext;
@Override
protected void executeInternal(JobExecutionContext arg0) throws JobExecutionException {
if (applicationContext != null) {
sendEipMsg(1);
sendEipMsg(2);
}
}
/*
* type = 1 By SendMsgDate type = 2 By Start Date
*/
private void sendEipMsg(int type) {
String[] countryId = QixSqlUtil.schedulerCountryId;
Qix310WJobManager manager = (Qix310WJobManager) this.applicationContext.getBean(Qix310WJobManager.class);
List<Map<String, Object>> result = null;
if (countryId != null && countryId.length > 0) {
for (int i = 0; i < countryId.length; i++) {
String cId = countryId[i];
if (type == 1) {
result = manager.doQuerySendEipMsgBySendMsgDate(cId, null);
} else if (type == 2) {
result = manager.doQuerySendEipMsgByStartDate(cId, null);
}
if (result != null && result.size() > 0) {
for (int k = 0; k < result.size(); k++) {
Map<String, Object> m = (Map<String, Object>) result.get(k);
BigDecimal qixt312Id = (BigDecimal) m.get("QIXT312_ID");
String electName = (String) m.get("ELECT_NAME");
String sDate = (String) m.get("ELECT_START_DATE");
String eDate = (String) m.get("ELECT_END_DATE");
sDate = QixDateUtil.centDateToCht(sDate);
eDate = QixDateUtil.centDateToCht(eDate);
String sTime = (String) m.get("ELECT_START_TIME");
String eTime = (String) m.get("ELECT_END_TIME");
String toDoEmpId = (String) m.get("ELECT_USER_ID");
String applyEmpId = (String) m.get("CREATE_USER_ID");
String toDoUserId = QixSqlUtil.getEipUserId(toDoEmpId, cId);
String applyUserId = QixSqlUtil.getEipUserId(applyEmpId, cId);
String reXmlStr = null;
try {
// send message to eip
reXmlStr = WSUtil.doEipService(toDoUserId, "QIX", applyUserId,
this.getMsgStrBySendMsgDate(electName, sDate, eDate, sTime, eTime, type));
String eipReCode = QixXmlParseUtil.queryXmlTagValue(reXmlStr, EipRtnMsgTagName.EIP_CODE);
String eipReDesc = QixXmlParseUtil.queryXmlTagValue(reXmlStr, EipRtnMsgTagName.EIP_DESC);
// update QIXT312 related column
manager.doUpdate(qixt312Id, eipReCode, eipReDesc, cId);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
} // for(int k=0; k<result.size(); k++)
} // if (result != null && result.size() > 0)
} // for (int i = 0; i < countryId.length; i++)
} // if (countryId != null && countryId.length > 0)
}
private String getMsgStrBySendMsgDate(String electName, String sDate, String eDate, String sTime, String eTime,
int type) {
String msg = null;
String result = "";
if (type == 1) {
msg = "「{0}」投票時間:{1} {2}至{3} {4}," + "請至「候選人資料查詢 (QIX360W)」查詢候選人名單";
result = MessageFormat.format(msg, electName, sDate, sTime, eDate, eTime);
} else if (type == 2) {
msg = "「{0}」投票進行中!請至「投票作業(QIX350W)」功能,進行投票。";
result = MessageFormat.format(msg, electName);
}
return result;
}
/**
* @param applicationContext ApplicationContext
*/
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}