Spring與quarz結合處理定時任務需求
quarz是功能強大的作業排程框架,與spring結合能夠方便的處理定時任務。
程式碼如下:
beans.xml配置:
<bean id="clearExceptStatisticsTask"
class="com.sf.module.deptManage.clearCustomExceptionManage.thread.ClearExceptStatisticsTask">
<property name="exceptionClearInfoBiz" ref="exceptionClearInfoBiz" />
<property name="customDeptDao" ref="customDeptDao" />
</bean>
<!-- Spring Quartz Begin -->
<bean id="clearExceptStatisticsJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="clearExceptStatisticsTask" />
<property name="targetMethod" value="exec" />
<property name="concurrent" value="false" />
</bean>
<bean id="clearExceptStatisticsCronTrigger1" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="clearExceptStatisticsJob" />
<property name="cronExpression" value="0 0 4 * * ?" /><!-- CRON表示式,每天凌晨四點執行 -->
<!--<property name="cronExpression" value="0/90 * * * * ?" />--><!-- 每隔90秒執行一次 -->
</bean>
<bean id="clearExceptStatisticsCronTrigger2" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="clearExceptStatisticsJob" />
<property name="cronExpression" value="0 0 16 * * ?" /><!-- CRON表示式,每天下午16點執行 -->
</bean>
<!-- 入口 -->
<bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="clearExceptStatisticsCronTrigger1" />
<ref bean="clearExceptStatisticsCronTrigger2" />
</list>
</property>
</bean>
class中程式碼:
private static ExecutorService service = Executors.newCachedThreadPool();
Runnable mailSendRun=new Runnable() {
@Override
public void run() {
boolean flag=false;
String subject = getSubject();//郵件主題
String email[] = {map.get("EMP_MAIL").toString()};//傳送人
StringBuffer stringBuffer = new StringBuffer();
for(int i=0; i<email.length; i++){
if(!(i == email.length-1)){
stringBuffer.append(email[i]).append(";");
}else{
stringBuffer.append(email[i]);
}
}
String accessId = null;
String accessToken = null;
try {
accessId = PropritesUtils.getValue("mail.new.id");
accessToken = PropritesUtils.getValue("mail.new.token");
} catch (IOException e) {
logger.error("讀取配置檔案錯誤:"+e);
}
String body = getHtml(map,flag);//傳送內容
JSONObject params = new JSONObject();
params.put("userId", stringBuffer.toString());//收件人郵箱,支援多收件人,分號隔離;如下面的抄送人格式
params.put("templateCode", "cmsp_clear_thread");// 業務模板code
JSONObject templateJson = new JSONObject();
templateJson.put("html", body); //對應渠道模板中{{html}}中引數
params.put("templateParam", templateJson); //如果對應渠道模板中沒有類{{}}格式的引數,可以遮蔽該語句
params.put("subject", subject);// 郵件主題,預設是渠道模板名稱
params.put("msgType", "txt");// 訊息型別,目前只支援txt
params.put("accessId", accessId);// 通知平臺接入Id
params.put("accessToken", accessToken);// 通知平臺接入Token
if(StringUtil.isNotNull(body)&&isSelectServer()){
// MailSendFactory.getMailSendFactory().sendMail(email, true,
// subject, body);
SendEmail.send(params);
}
logger.info("傳送成功!!!!!!!"+"傳送內容:"+body);
}
};
service.execute(mailSendRun);