1 18 19 20 package org.apache.roller.ui.core.pings; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.roller.RollerException; 25 import org.apache.roller.config.PingConfig; 26 import org.apache.roller.config.RollerRuntimeConfig; 27 import org.apache.roller.model.PingQueueManager; 28 import org.apache.roller.model.RollerFactory; 29 import org.apache.roller.pojos.PingQueueEntryData; 30 import org.apache.roller.pojos.PingTargetData; 31 import org.apache.roller.pojos.WebsiteData; 32 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 41 public class PingQueueProcessor { 42 private static final Log logger = LogFactory.getLog(PingQueueProcessor.class); 43 44 private static PingQueueProcessor theInstance; 45 46 47 private PingQueueManager pingQueueMgr; 48 49 public static PingQueueProcessor getInstance() { 50 return theInstance; 51 } 52 53 private PingQueueProcessor() throws RollerException { 54 pingQueueMgr = RollerFactory.getRoller().getPingQueueManager(); 55 } 56 57 62 public static synchronized void init() throws RollerException { 63 if (theInstance != null) { 64 logger.warn("Ignoring duplicate initialization of PingQueueProcessor!"); 65 return; 66 } 67 theInstance = new PingQueueProcessor(); 68 if (logger.isDebugEnabled()) logger.debug("Ping queue processor initialized."); 69 } 70 71 76 public synchronized void processQueue() { 77 if (PingConfig.getSuspendPingProcessing()) { 78 logger.info("Ping processing has been suspended. Skipping current round of ping queue processing."); 79 return; 80 } 81 82 String absoluteContextUrl = RollerRuntimeConfig.getAbsoluteContextURL(); 83 if (absoluteContextUrl == null) { 84 logger.warn("WARNING: Skipping current ping queue processing round because we cannot yet determine the site's absolute context url."); 85 return; 86 } 87 88 93 try { 94 if (logger.isDebugEnabled()) logger.debug("Started processing ping queue."); 95 List entries = pingQueueMgr.getAllQueueEntries(); 97 98 for (Iterator i = entries.iterator(); i.hasNext();) { 100 PingQueueEntryData pingQueueEntry = (PingQueueEntryData) i.next(); 101 processQueueEntry(pingQueueEntry); 102 } 103 if (logger.isDebugEnabled()) logger.debug("Finished processing ping queue."); 104 } catch (Exception ex) { 105 logger.error("Unexpected exception processing ping queue! Aborting this pass of ping queue processing.", ex); 106 } 107 } 108 109 116 private void processQueueEntry(PingQueueEntryData pingQueueEntry) throws RollerException { 117 if (logger.isDebugEnabled()) logger.debug("Processing ping queue entry: " + pingQueueEntry); 118 119 PingTargetData pingTarget = pingQueueEntry.getPingTarget(); 120 WebsiteData website = pingQueueEntry.getWebsite(); 121 boolean pingSucceeded = false; 122 if (PingConfig.getLogPingsOnly()) { 123 logger.info("Logging simulated ping for ping queue entry " + pingQueueEntry); 125 pingSucceeded = true; 126 } else { 127 try { 129 WeblogUpdatePinger.sendPing(pingTarget, website); 131 pingSucceeded = true; 134 } catch (Exception ex) { 135 handlePingError(pingQueueEntry, ex); 137 } 138 } 139 if (pingSucceeded) { 141 if (logger.isDebugEnabled()) logger.debug("Processed ping: " + pingQueueEntry); 142 pingQueueMgr.removeQueueEntry(pingQueueEntry); 143 } 144 } 145 146 153 private void handlePingError(PingQueueEntryData pingQueueEntry, Exception ex) throws RollerException { 154 if ((pingQueueEntry.incrementAttempts() < PingConfig.getMaxPingAttempts()) && WeblogUpdatePinger.shouldRetry(ex)) 155 { 156 logger.warn("Error on ping attempt (" + pingQueueEntry.getAttempts() + ") for " + pingQueueEntry + ": [" + ex.getMessage() + "]. Will re-queue for later attempts."); 159 if (logger.isDebugEnabled()) logger.debug("Error on last ping attempt was: ", ex); 160 pingQueueMgr.saveQueueEntry(pingQueueEntry); 161 } else { 162 logger.warn("Error on ping attempt (" + pingQueueEntry.getAttempts() + ") for " + pingQueueEntry + ": [" + ex.getMessage() + "]. Entry will be REMOVED from ping queue."); 164 if (logger.isDebugEnabled()) logger.debug("Error on last ping attempt was: ", ex); 165 pingQueueMgr.removeQueueEntry(pingQueueEntry); 166 } 168 } 169 170 171 } 172 | Popular Tags |