<delete id="removeWxMessageByTime">
delete from wx_message
<dynamic prepend="WHERE">
<isNotNull prepend="and" property="created_at">
<![CDATA[
created_at <= #created_at#
]]>
</isNotNull>
</dynamic>
</delete>
delete from wx_message
<dynamic prepend="WHERE">
<isNotNull prepend="and" property="created_at">
<![CDATA[
created_at <= #created_at#
]]>
</isNotNull>
</dynamic>
</delete>
定时器:
package com.wizincloud.wxManager.tokenManager;
import java.util.Timer;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import com.wizincloud.base.plat.rad.util.SpringBeanLocator;
import com.wizincloud.wxMessage.service.WxMessageService;
/**
* @author liuyanliang
*
*/
public class MessageHandleListener implements ServletContextListener{
private Timer timer = null;
/*
* (non-Javadoc)
*
* @see
* javax.servlet.ServletContextListener#contextInitialized(javax.servlet
* .ServletContextEvent)
*/
@Override
public void contextInitialized(ServletContextEvent sce) {
ServletContext content = sce.getServletContext();
WxMessageService wxMessageService = (WxMessageService) SpringBeanLocator.getBean(content, "wxMessageService");
timer = new Timer(true);
// 设置任务计划,启动和间隔时间(一小时)
timer.schedule(new MessageHandleTask(wxMessageService), 0, 60*60*1000);
}
/*
* (non-Javadoc)
*
* @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
* ServletContextEvent)
*/
@Override
public void contextDestroyed(ServletContextEvent arg0) {
if(timer!=null){
timer.cancel();
}
}
}
定时任务:
package com.wizincloud.wxManager.tokenManager;
import java.util.Calendar;
import java.util.Date;
import java.util.TimerTask;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
import com.wizincloud.wxMessage.model.WxMessage;
import com.wizincloud.wxMessage.service.WxMessageService;
/**
* @author liuyanliang
*
*/
public class MessageHandleTask extends TimerTask {
private static Logger logger = Logger.getLogger(MessageHandleTask.class);
private static Gson g = new Gson();
private WxMessageService wxMessageService;
public MessageHandleTask(WxMessageService wxMessageService) {
this.wxMessageService = wxMessageService;
}
/*
* (non-Javadoc)
*
* @see java.util.TimerTask#run()
*/
@Override
public void run() {
WxMessage wxMessage = new WxMessage();
// 删除N天前的数据
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -3);
Date nDateBefore = cal.getTime();
wxMessage.setCreated_at(nDateBefore);
logger.info("wxMessage:" + g.toJson(wxMessage));
wxMessageService.delWxMessageByTime(wxMessage);
}
}