1 8 9 package org.roller.presentation.pings; 10 11 import org.apache.commons.logging.Log; 12 import org.apache.commons.logging.LogFactory; 13 import org.roller.RollerException; 14 import org.roller.config.PingConfig; 15 import org.roller.model.PingQueueManager; 16 import org.roller.model.RollerFactory; 17 import org.roller.pojos.PingQueueEntryData; 18 import org.roller.pojos.PingTargetData; 19 import org.roller.pojos.WebsiteData; 20 import org.roller.presentation.RollerContext; 21 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 28 public class PingQueueProcessor 29 { 30 private static final Log logger = LogFactory.getLog(PingQueueProcessor.class); 31 32 private static PingQueueProcessor theInstance; 33 34 35 private RollerContext rollerContext; 36 private PingQueueManager pingQueueMgr; 37 38 public static PingQueueProcessor getInstance() 39 { 40 return theInstance; 41 } 42 43 private PingQueueProcessor(RollerContext rc) throws RollerException 44 { 45 rollerContext = rc; 46 pingQueueMgr = RollerFactory.getRoller().getPingQueueManager(); 47 } 48 49 55 public static synchronized void init(RollerContext rc) throws RollerException 56 { 57 if (theInstance != null) 58 { 59 logger.warn("Ignoring duplicate initialization of PingQueueProcessor!"); 60 return; 61 } 62 theInstance = new PingQueueProcessor(rc); 63 if (logger.isDebugEnabled()) logger.debug("Ping queue processor initialized."); 64 } 65 66 71 public synchronized void processQueue() 72 { 73 if (PingConfig.getSuspendPingProcessing()) { 74 logger.info("Ping processing has been suspended. Skipping current round of ping queue processing."); 75 return; 76 } 77 78 String absoluteContextUrl = rollerContext.getAbsoluteContextUrl(); 79 if (absoluteContextUrl == null) 80 { 81 logger.warn("WARNING: Skipping current ping queue processing round because we cannot yet determine the site's absolute context url."); 82 return; 83 } 84 85 90 try 91 { 92 if (logger.isDebugEnabled()) logger.debug("Started processing ping queue."); 93 List entries = pingQueueMgr.getAllQueueEntries(); 95 96 for (Iterator i = entries.iterator(); i.hasNext();) 98 { 99 PingQueueEntryData pingQueueEntry = (PingQueueEntryData) i.next(); 100 processQueueEntry(absoluteContextUrl, pingQueueEntry); 101 } 102 if (logger.isDebugEnabled()) logger.debug("Finished processing ping queue."); 103 } 104 catch (Exception ex) 105 { 106 logger.error("Unexpected exception processing ping queue! Aborting this pass of ping queue processing.", ex); 107 } 108 } 109 110 118 private void processQueueEntry(String absoluteContextUrl, PingQueueEntryData pingQueueEntry) 119 throws RollerException 120 { 121 if (logger.isDebugEnabled()) logger.debug("Processing ping queue entry: " + pingQueueEntry); 122 123 PingTargetData pingTarget = pingQueueEntry.getPingTarget(); 124 WebsiteData website = pingQueueEntry.getWebsite(); 125 boolean pingSucceeded = false; 126 if (PingConfig.getLogPingsOnly()) 127 { 128 logger.info("Logging simulated ping for ping queue entry " + pingQueueEntry); 130 pingSucceeded = true; 131 } 132 else 133 { 134 try 136 { 137 WeblogUpdatePinger.sendPing(absoluteContextUrl, pingTarget, website); 139 pingSucceeded = true; 142 } 143 catch (Exception ex) 144 { 145 handlePingError(pingQueueEntry, ex); 147 } 148 } 149 if (pingSucceeded) 151 { 152 if (logger.isDebugEnabled()) logger.debug("Processed ping: " + pingQueueEntry); 153 pingQueueMgr.removeQueueEntry(pingQueueEntry); 154 } 155 } 156 157 164 private void handlePingError(PingQueueEntryData pingQueueEntry, Exception ex) 165 throws RollerException 166 { 167 if ((pingQueueEntry.incrementAttempts() < PingConfig.getMaxPingAttempts()) && 168 WeblogUpdatePinger.shouldRetry(ex)) 169 { 170 logger.warn("Error on ping attempt (" + pingQueueEntry.getAttempts() + ") for " + pingQueueEntry + 173 ": [" + ex.getMessage() + "]. Will re-queue for later attempts."); 174 if (logger.isDebugEnabled()) logger.debug("Error on last ping attempt was: ", ex); 175 pingQueueMgr.storeQueueEntry(pingQueueEntry); 176 } 177 else 178 { 179 logger.warn("Error on ping attempt (" + pingQueueEntry.getAttempts() + ") for " + pingQueueEntry + 181 ": [" + ex.getMessage() + "]. Entry will be REMOVED from ping queue."); 182 if (logger.isDebugEnabled()) logger.debug("Error on last ping attempt was: ", ex); 183 pingQueueMgr.removeQueueEntry(pingQueueEntry); 184 } 186 } 187 188 189 } 190 | Popular Tags |