1. 建立 监听类 SalesPaymentListener
package com.eas.kingdee.salespayment.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.eas.kingdee.salespayment.runner.SalesPaymentRunnerManager;
/**
*
* @author youzhen_qin
*
*/
public class SalesPaymentListener implements ServletContextListener{
*
* @author youzhen_qin
*
*/
public class SalesPaymentListener implements ServletContextListener{
private static WebApplicationContext webApplicationContext;
private static ApplicationContextHelper helper = new ApplicationContextHelper();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
private static ApplicationContextHelper helper = new ApplicationContextHelper();
@Override
public void contextDestroyed(ServletContextEvent arg0) {
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
helper.setApplicationContext(webApplicationContext);
//run 定时器
SalesPaymentRunnerManager.initRunner(arg0);
}
public void contextInitialized(ServletContextEvent arg0) {
webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(arg0.getServletContext());
helper.setApplicationContext(webApplicationContext);
//run 定时器
SalesPaymentRunnerManager.initRunner(arg0);
}
}
2. 定时器类
package com.eas.kingdee.salespayment.runner;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContext;
import com.eas.kingdee.salespayment.listener.ApplicationContextHelper;
import com.eas.kingdee.salespayment.task.SalesPaymentTask;
import com.eas.kingdee.salespayment.task.SalesPaymentTask;
public class SalesPaymentRunnerManager {
private static final Logger logger = Logger.getLogger(SalesPaymentRunnerManager.class);
public static final void initRunner(ServletContextEvent applicationContext) {
logger.info("initRunner.");
ApplicationContextHelper helper = new ApplicationContextHelper();
final ApplicationContext context = helper.getContext();
new Thread(){
public void run() {
logger.info("saledaily xt push thread started.");
while(true){
try {
long inteval = 1*60L * 60L * 1000L; //1 小时跑一次
sleep(inteval);
// if(betweenTime("19:00:00","23:50:00")){
if(betweenTime("18:00:00","18:02:00")){
SalesPaymentTask task = new SalesPaymentTask();
boolean pushSuccess = task.pushMsg(context); //具体执行的业务操作
logger.info("pushSuccess-----:"+pushSuccess);
}
} catch (Exception e1) {
logger.error("定时推送通知任务失败", e1);
return ;
}
}
}
}.start();
}
public static boolean betweenTime(String beginStr,String endStr) throws ParseException{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//今天
Date thisTime = cal.getTime();
String thisStr = format.format(thisTime); //当前时间
beginStr = thisStr.substring(0,11)+beginStr; //开始时间
endStr = thisStr.substring(0,11)+endStr; //
//第二天
Date beginDate = format.parse(beginStr);
Date endDate = format.parse(endStr);
if(thisTime.after(beginDate) && thisTime.before(endDate)){
return true;
}else{
return false;
}
}
public static final void stopRunner() {
private static final Logger logger = Logger.getLogger(SalesPaymentRunnerManager.class);
public static final void initRunner(ServletContextEvent applicationContext) {
logger.info("initRunner.");
ApplicationContextHelper helper = new ApplicationContextHelper();
final ApplicationContext context = helper.getContext();
new Thread(){
public void run() {
logger.info("saledaily xt push thread started.");
while(true){
try {
long inteval = 1*60L * 60L * 1000L; //1 小时跑一次
sleep(inteval);
// if(betweenTime("19:00:00","23:50:00")){
if(betweenTime("18:00:00","18:02:00")){
SalesPaymentTask task = new SalesPaymentTask();
boolean pushSuccess = task.pushMsg(context); //具体执行的业务操作
logger.info("pushSuccess-----:"+pushSuccess);
}
} catch (Exception e1) {
logger.error("定时推送通知任务失败", e1);
return ;
}
}
}
}.start();
}
public static boolean betweenTime(String beginStr,String endStr) throws ParseException{
DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
//今天
Date thisTime = cal.getTime();
String thisStr = format.format(thisTime); //当前时间
beginStr = thisStr.substring(0,11)+beginStr; //开始时间
endStr = thisStr.substring(0,11)+endStr; //
//第二天
Date beginDate = format.parse(beginStr);
Date endDate = format.parse(endStr);
if(thisTime.after(beginDate) && thisTime.before(endDate)){
return true;
}else{
return false;
}
}
public static final void stopRunner() {
}
}
}
3. web.xml 配置
<listener>
<listener-class>
com.eas.kingdee.salespayment.listener.SalesPaymentListener
</listener-class>
</listener>