監聽器參考實例

package com.listener;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Timer;
import java.util.TimerTask;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.jdbc.JdbcUpdateAffectedIncorrectNumberOfRowsException;

import com.util.DateUtil;
import com.util.JdbcUtil;
/**
 * 
 * 工具類,公告定時器
 * 對超過規定時間未審覈的公告進行自動審覈
 * 
 * @author wushengxin
 * 
 */
public class NoticeListener implements ServletContextListener {
    Timer timer = null;

    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("工程銷燬...");
        if (timer != null) {
            System.out.println("取消定時器");
            timer.cancel();
        }
    }
    /**
     * 
     * 
     * 判斷公告是否已超過規定時間未審覈,對未審覈的公告進行審覈
     * 
     * 
     */
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("工程啓動...");

        timer = new Timer();

        timer.schedule(new TimerTask() {

            @Override
            public void run() {
                Connection conn = null;
                Statement stmt = null;
                ResultSet rs = null;
                StringBuffer querySQL = new StringBuffer();
                StringBuffer updateSQL = new StringBuffer();

                querySQL.append("Select notice_id,notice_addtime");
                querySQL.append(" From T_Notice");
                querySQL.append(" where 1 = 1");
                querySQL.append(" and is_approve = 0");
                querySQL.append(" order by notice_id asc");
                String sysTime = DateUtil.getSysDateTime();
                try {
                    conn = JdbcUtil.getConn();
                    stmt = conn.createStatement();
                    rs = stmt.executeQuery(querySQL.toString());
                    while (rs.next()) {
                        int notice_id = rs.getInt("notice_id");
                        String notice_addtime = rs.getString("notice_addtime");

                        long diffdays = DateUtil.diffTimeForDay(sysTime,
                                notice_addtime);
                        if (diffdays >= 3) {
                            updateSQL.setLength(0);
                            updateSQL.append("Update T_Notice set ");
                            updateSQL.append(" is_approve = 1,");
                            updateSQL.append(" approve_user = '系統自動審覈',");
                            updateSQL.append(" approve_time = '" + sysTime
                                    + "'");
                            updateSQL.append(" where 1= 1");
                            updateSQL.append(" and notice_id = '" + notice_id
                                    + "'");
                            stmt.addBatch(updateSQL.toString());
                        }
                    }
                    stmt.executeBatch();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    JdbcUtil.closeResource(rs, stmt, conn);
                }

            }
        }, 5000, 1000 * 20);

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章