1 41 package com.mvnforum.user; 42 43 import java.util.Date ; 44 import java.util.TimerTask ; 45 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import net.myvietnam.mvncore.util.TimerUtil; 49 50 class DeleteOrphanPmAttachmentTask extends TimerTask { 51 52 private static Log log = LogFactory.getLog(DeleteOrphanPmAttachmentTask.class); 54 55 private static DeleteOrphanPmAttachmentTask instance = null; 57 58 private boolean scheduled = false; 60 private int count = 0; 61 private PmAttachmentWebHandler pmAttachmentWebHandler = new PmAttachmentWebHandler(); 62 63 private DeleteOrphanPmAttachmentTask() { 65 } 66 67 71 public static synchronized DeleteOrphanPmAttachmentTask getInstance() { 72 if (instance == null) { 73 instance = new DeleteOrphanPmAttachmentTask(); 74 } 75 return instance; 76 } 77 78 public boolean cancel() { 79 return super.cancel(); 80 } 81 82 public synchronized void schedule(Date firstTime, long period) { 83 if (scheduled == false) { 84 scheduled = true; 85 TimerUtil.getInstance().schedule(this, firstTime, period); 86 } 87 } 88 89 public synchronized void schedule(Date time) { 90 if (scheduled == false) { 91 scheduled = true; 92 TimerUtil.getInstance().schedule(this, time); 93 } 94 } 95 96 public synchronized void schedule(long delay) { 97 if (scheduled == false) { 98 scheduled = true; 99 TimerUtil.getInstance().schedule(this, delay); 100 } 101 } 102 103 public synchronized void schedule(long delay, long period) { 104 if (scheduled == false) { 105 scheduled = true; 106 TimerUtil.getInstance().schedule(this, delay, period); 107 } 108 } 109 110 public synchronized void scheduleAtFixedRate(Date firstTime, long period) { 111 if (scheduled == false) { 112 scheduled = true; 113 TimerUtil.getInstance().schedule(this, firstTime, period); 114 } 115 } 116 117 public synchronized void scheduleAtFixedRate(long delay, long period) { 118 if (scheduled == false) { 119 scheduled = true; 120 TimerUtil.getInstance().schedule(this, delay, period); 121 } 122 } 123 124 public void run() { 125 count++; 126 long start = System.currentTimeMillis(); 127 try { 128 log.debug("Begin calling deleteOrphanPmAttachment in DeleteOrphanPmAttachmentTask"); 129 pmAttachmentWebHandler.deleteOrphanPmAttachment(); 130 } catch (Exception ex) { 131 log.error("Cannot process deleteOrphanPmAttachment in DeleteOrphanPmAttachmentTask", ex); 132 } finally{ 133 long duration = System.currentTimeMillis() - start; 134 log.debug("DeleteOrphanPmAttachmentTask:deleteOrphanPmAttachment process " + count + " times, took " + duration + " ms"); 135 } 136 } 137 } 138 | Popular Tags |