监听器、定时器


1. 建立 监听类 SalesPaymentListener

package com.eas.kingdee.salespayment.listener;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.eas.kingdee.salespayment.runner.SalesPaymentRunnerManager;
/**
 *
 * @author youzhen_qin
 *
 */
public class SalesPaymentListener implements ServletContextListener{
  private static WebApplicationContext webApplicationContext; 
  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);
 }
}

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 javax.servlet.ServletContextEvent;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import com.eas.kingdee.salespayment.listener.ApplicationContextHelper;
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() {
 }
 
}


3. web.xml 配置
 
<listener>
  <listener-class>
   com.eas.kingdee.salespayment.listener.SalesPaymentListener
  </listener-class>
 </listener> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值