现在的项目中存在现金券的问题,有效期是一年。但是不知道使用什么去监听其时间已到期,并且废掉已有的现金券。  是在数据库中处理,还是在代码中处理呢?  望路过的大哥大姐给个思路......小弟先谢谢咯

解决方案 »

  1.   

    个人认为应该在代码中处理,使用java的监听器(listener):1、使用监听器需要在web.xml中配置:
    <listener>
            <listener-class>com.css.wam.service.impl.TestListener</listener-class>
    </listener>2、实现ServletContextListener接口:    private Timer timer = null;
        private TimerTask task = null;
        // 月份变量
        private static int month = 1;    // 日期变量
        private static int day = 1;    // 分钟变量
        private static int minute = 1;    // 小时变量
        private static int hour = 1;    // 秒变量
        private static int second = 1;    public void contextInitialized(final ServletContextEvent arg0)
        {
            timer = new Timer();        task = new TimerTask() {            public void run() {                try 
                    {
                        GregorianCalendar date = new GregorianCalendar();
                        month = date.get(Calendar.MONTH);
                        day = date.get(Calendar.DATE);
                        hour = date.get(Calendar.HOUR_OF_DAY);
                        minute = date.get(Calendar.MINUTE);
                        second = date.get(Calendar.SECOND);                    if ((hour == 24) && (minute == 00) && (second == 00)) 
                        {
                         //Do Something
                         //删除过期的现金券。                     }
                        
                    } catch (Exception e) {                    e.printStackTrace();                } finally {
                        closeConnection();
                    }        timer.schedule(task, 10000, 500);
        }public void contextDestroyed(ServletContextEvent arg0) 
    {        timer.cancel();
        }
      

  2.   

    使用的时候 验证  方便,准确。写cron 每天执行一次 将过期的现金券设置为过期再者如果只是根据时间判断的话 在query这些现金券的时候 在一条sql里就可以实现啊 
    case (dateadd(y,1,entrydate) <= getdate()) then 过期 else 未过期 end。sybase代码 其他数据库的 找date操作函数