⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 forumschedulerunit.java

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA
字号:
package com.redmoon.forum;

import cn.js.fan.kernel.BaseSchedulerUnit;
import cn.js.fan.db.Conn;
import cn.js.fan.web.Global;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import java.util.Date;
import java.sql.PreparedStatement;
import com.redmoon.forum.message.MessageDb;
import cn.js.fan.util.ResKeyException;
import cn.js.fan.util.DateUtil;
import cn.js.fan.util.StrUtil;


/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ForumSchedulerUnit extends BaseSchedulerUnit {
    static Logger logger = Logger.getLogger(ForumSchedulerUnit.class.getName());

    public static int refreshOnlineInterval = 5; // 5分钟
    public static long lastRefreshOnlineIntervalTime;

    public static long lastClearUserMessageInerval = System.currentTimeMillis();
    public static long lastClearTmpAttachmentTime = System.currentTimeMillis();

    public static long clearUserMessageInterval = 24 * 60 * 60 * 1000; // 每隔24小时清除用户超出容量的短信息
    public static long clearTmpAttachmentInterval = 4 * 60 * 60 * 1000; // 每隔24小时删除临时图片
    public static int message_expire_day = 150; // 天

    static {
        initParam();
    }

    public ForumSchedulerUnit() {
        lastTime = System.currentTimeMillis();
        interval = 600000; // 每隔10分钟刷新一次
        lastRefreshOnlineIntervalTime = System.currentTimeMillis();
        lastClearUserMessageInerval = System.currentTimeMillis();
        name = "Forum Scheduler";
    }

    public static void initParam() {
        Config cfg = new Config();
        refreshOnlineInterval = cfg.getIntProperty("forum.refreshOnlineInterval") * 60 * 1000;
        message_expire_day = cfg.getIntProperty("forum.message_expire_day");
        clearUserMessageInterval = cfg.getIntProperty("interval_clear_message") * 60 * 60 * 1000;
    }

    /**
     * OnTimer
     *
     * @param currentTime long
     * @todo Implement this cn.js.fan.kernal.ISchedulerUnit method
     */
    public void OnTimer(long curTime) {
        // logger.info("curTime=" + curTime);
        try {
            if (curTime - lastTime >= interval) {
                action();
                lastTime = curTime;
            }
            if (curTime - lastRefreshOnlineIntervalTime >=
                refreshOnlineInterval) {
                OnlineUserDb oud = new OnlineUserDb();
                oud.refreshOnlineUser();
                lastRefreshOnlineIntervalTime = curTime;
            }

            if (curTime - lastClearUserMessageInerval >=
                clearUserMessageInterval) {
                clearMessageExpired(message_expire_day);
                lastClearUserMessageInerval = curTime;
            }

            if (curTime - lastClearTmpAttachmentTime >=
                clearTmpAttachmentInterval) {
                clearTmpAttachment();
                lastClearTmpAttachmentTime = curTime;
            }
        }
        catch (Throwable e) {
            // 防止运行有异常,导致线程退出
            logger.error("OnTimer:" + StrUtil.trace(e));
        }
    }

    public void clearMessageExpired(int expireDay) {
        MessageDb md = new MessageDb();
        md.clearMessageExpired(expireDay);
    }

    public synchronized void action() {
        refreshColor();
        refreshBold();
    }

    /**
     * 清除两天前至前十天的临时图片文件
     */
    public void clearTmpAttachment() {
        java.util.Date today = new Date();
        Date d2 = DateUtil.addDate(today, -2);
        Date d10 = DateUtil.addDate(today, -10);
        // logger.info("d2=" + DateUtil.format(d2, "yyyy-MM-dd") + " d10=" + DateUtil.format(d10, "yyyy-MM-dd"));
        String sql = "select id from sq_message_attach where msgId=-1 and UPLOAD_DATE>? and UPLOAD_DATE<?";
        // 如果加粗显示已到期
        Conn conn = new Conn(Global.defaultDB);
        ResultSet rs = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, "" + d10.getTime());
            ps.setString(2, "" + d2.getTime());
            rs = conn.executePreQuery();
            // logger.info("rows=" + conn.getRows() + " d10=" + d10.getTime());
            while (rs.next()) {
                long id = rs.getLong(1);
                logger.info("clearTmpAttachment: Delete temp attchment id=" + id);
                Attachment att = new Attachment(id);
                att.delTmpAttach();
            }
        } catch (SQLException e) {
            logger.error("clearTmpAttachment:" + e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public void refreshBold() {
        MsgDb md = new MsgDb();
        String sql = "select id from sq_message where isBold=1 and boldExpire<?";
        // 如果加粗显示已到期
        Conn conn = new Conn(Global.defaultDB);
        ResultSet rs = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, "" + System.currentTimeMillis());
            rs = conn.executePreQuery();
            while (rs.next()) {
                long id = rs.getLong(1);
                // logger.info("msgRootId=" + msgRootId);
                md = md.getMsgDb(id);
                try {
                    md.ChangeBold(id, 0, new Date());
                } catch (ResKeyException e) {
                    logger.error("action:" + e.getMessage());
                }
            }
        } catch (SQLException e) {
            logger.error("refreshBold:" + e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

    public void refreshColor() {
        MsgDb md = new MsgDb();
        String sql = "select id from sq_message where color<>'' and colorExpire<?";
        // 如果颜色值已到期
        Conn conn = new Conn(Global.defaultDB);
        ResultSet rs = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1, "" + System.currentTimeMillis());
            rs = conn.executePreQuery();
            // logger.info("rs=" + rs);
            while (rs.next()) {
                long id = rs.getLong(1);
                // logger.info("msgRootId=" + msgRootId);
                md = md.getMsgDb(id);
                try {
                    md.ChangeColor(id, "", new Date());
                } catch (ResKeyException e) {
                    logger.error("refreshColor:" + e.getMessage());
                }
            }
        } catch (SQLException e) {
            logger.error("refreshColor1:" + e.getMessage());
        } finally {
            if (conn != null) {
                conn.close();
                conn = null;
            }
        }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -