001: /*
002: * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/common/DeleteOrphanPmAttachmentTask.java,v 1.2 2007/10/09 11:09:16 lexuanttkhtn Exp $
003: * $Author: lexuanttkhtn $
004: * $Revision: 1.2 $
005: * $Date: 2007/10/09 11:09:16 $
006: *
007: * ====================================================================
008: *
009: * Copyright (C) 2002-2007 by MyVietnam.net
010: *
011: * All copyright notices regarding mvnForum MUST remain
012: * intact in the scripts and in the outputted HTML.
013: * The "powered by" text/logo with a link back to
014: * http://www.mvnForum.com and http://www.MyVietnam.net in
015: * the footer of the pages MUST remain visible when the pages
016: * are viewed on the internet or intranet.
017: *
018: * This program is free software; you can redistribute it and/or modify
019: * it under the terms of the GNU General Public License as published by
020: * the Free Software Foundation; either version 2 of the License, or
021: * any later version.
022: *
023: * This program is distributed in the hope that it will be useful,
024: * but WITHOUT ANY WARRANTY; without even the implied warranty of
025: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
026: * GNU General Public License for more details.
027: *
028: * You should have received a copy of the GNU General Public License
029: * along with this program; if not, write to the Free Software
030: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
031: *
032: * Support can be obtained from support forums at:
033: * http://www.mvnForum.com/mvnforum/index
034: *
035: * Correspondence and Marketing Questions can be sent to:
036: * info at MyVietnam net
037: *
038: * @author: Minh Nguyen
039: * @author: Mai Nguyen
040: */
041: package com.mvnforum.common;
042:
043: import java.util.Date;
044: import java.util.TimerTask;
045:
046: import org.apache.commons.logging.Log;
047: import org.apache.commons.logging.LogFactory;
048:
049: import com.mvnforum.user.PmAttachmentWebHandler;
050:
051: import net.myvietnam.mvncore.util.TimerUtil;
052:
053: public class DeleteOrphanPmAttachmentTask extends TimerTask {
054:
055: // static variable
056: private static Log log = LogFactory
057: .getLog(DeleteOrphanPmAttachmentTask.class);
058:
059: // static variable
060: private static DeleteOrphanPmAttachmentTask instance = null;
061:
062: // instance variables
063: private boolean scheduled = false;
064: private int count = 0;
065: private PmAttachmentWebHandler pmAttachmentWebHandler = new PmAttachmentWebHandler();
066:
067: // private constructor will prevent any instatiation
068: private DeleteOrphanPmAttachmentTask() {
069: }
070:
071: /**
072: * This static method is used to get the Singleton instance of DeleteOrphanPmAttachmentTask
073: * @return the singleton instance of DeleteOrphanPmAttachmentTask
074: */
075: public static synchronized DeleteOrphanPmAttachmentTask getInstance() {
076: if (instance == null) {
077: instance = new DeleteOrphanPmAttachmentTask();
078: }
079: return instance;
080: }
081:
082: public boolean cancel() {
083: return super .cancel();
084: }
085:
086: public synchronized void schedule(Date firstTime, long period) {
087: if (scheduled == false) {
088: scheduled = true;
089: TimerUtil.getInstance().schedule(this , firstTime, period);
090: }
091: }
092:
093: public synchronized void schedule(Date time) {
094: if (scheduled == false) {
095: scheduled = true;
096: TimerUtil.getInstance().schedule(this , time);
097: }
098: }
099:
100: public synchronized void schedule(long delay) {
101: if (scheduled == false) {
102: scheduled = true;
103: TimerUtil.getInstance().schedule(this , delay);
104: }
105: }
106:
107: public synchronized void schedule(long delay, long period) {
108: if (scheduled == false) {
109: scheduled = true;
110: TimerUtil.getInstance().schedule(this , delay, period);
111: }
112: }
113:
114: public synchronized void scheduleAtFixedRate(Date firstTime,
115: long period) {
116: if (scheduled == false) {
117: scheduled = true;
118: TimerUtil.getInstance().schedule(this , firstTime, period);
119: }
120: }
121:
122: public synchronized void scheduleAtFixedRate(long delay, long period) {
123: if (scheduled == false) {
124: scheduled = true;
125: TimerUtil.getInstance().schedule(this , delay, period);
126: }
127: }
128:
129: public void run() {
130: count++;
131: long start = System.currentTimeMillis();
132: try {
133: log
134: .debug("Begin calling deleteOrphanPmAttachment in DeleteOrphanPmAttachmentTask");
135: pmAttachmentWebHandler.deleteOrphanPmAttachment();
136: } catch (Exception ex) {
137: log
138: .error(
139: "Cannot process deleteOrphanPmAttachment in DeleteOrphanPmAttachmentTask",
140: ex);
141: } finally {
142: long duration = System.currentTimeMillis() - start;
143: log
144: .debug("DeleteOrphanPmAttachmentTask:deleteOrphanPmAttachment process "
145: + count
146: + " times, took "
147: + duration
148: + " ms");
149: }
150: }
151: }
|