KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > ui > core > pings > PingQueueProcessor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. The ASF licenses this file to You
4  * under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. For additional information regarding
15  * copyright in this work, please see the NOTICE file in the top level
16  * directory of this distribution.
17  */

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 JavaDoc;
34 import java.util.List JavaDoc;
35
36 /**
37  * Ping Queue Processor. Singleton encapsulating logic for processing the weblog update ping queue.
38  *
39  * @author <a HREF="mailto:anil@busybuddha.org">Anil Gangolli</a>
40  */

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     /**
58      * Initialize the singleton. This is called during <code>RollerContext</code> initialization.
59      *
60      * @throws RollerException
61      */

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     /**
72      * Process the ping queue. Performs one pass through the ping queue, processing every entry once. On ping failure
73      * an entry is requeued for processing on subsequent passes until the configured maximum number of attempts is
74      * reached.
75      */

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 JavaDoc 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         // TODO: Group by ping target and ping all sites for that target?
89
// We're currently not taking advantage of grouping by ping target site and then sending
90
// all of the pings for that target at once. If it becomes an efficiency issue, we should do
91
// that.
92

93         try {
94             if (logger.isDebugEnabled()) logger.debug("Started processing ping queue.");
95             // Get all of the entries
96
List JavaDoc entries = pingQueueMgr.getAllQueueEntries();
97
98             // Process each entry
99
for (Iterator JavaDoc 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 JavaDoc ex) {
105             logger.error("Unexpected exception processing ping queue! Aborting this pass of ping queue processing.", ex);
106         }
107     }
108
109     /**
110      * Process an individual ping queue entry.
111      *
112      * @param pingQueueEntry the ping queue entry
113      * @throws RollerException only if there are problems processing the queue. Exceptions from sending pings are
114      * handled, not thrown.
115      */

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             // Just log the ping and pretend it succeeded.
124
logger.info("Logging simulated ping for ping queue entry " + pingQueueEntry);
125             pingSucceeded = true;
126         } else {
127             // Actually process the ping
128
try {
129                 // Send the ping
130
WeblogUpdatePinger.sendPing(pingTarget, website);
131                 // Consider successful ping transmission if we didn't get an exception. We don't care here
132
// about the result of the ping if it was transmitted.
133
pingSucceeded = true;
134             } catch (Exception JavaDoc ex) {
135                 // Handle the ping error, either removing or requeuing the ping queue entry.
136
handlePingError(pingQueueEntry, ex);
137             }
138         }
139         // We do this outside of the previous try-catch because we don't want an exception here to be considered a ping error.
140
if (pingSucceeded) {
141             if (logger.isDebugEnabled()) logger.debug("Processed ping: " + pingQueueEntry);
142             pingQueueMgr.removeQueueEntry(pingQueueEntry);
143         }
144     }
145
146     /**
147      * Handle any ping error.
148      *
149      * @param pingQueueEntry the ping queue entry
150      * @param ex the exception that occurred on the ping attempt
151      * @throws RollerException
152      */

153     private void handlePingError(PingQueueEntryData pingQueueEntry, Exception JavaDoc ex) throws RollerException {
154         if ((pingQueueEntry.incrementAttempts() < PingConfig.getMaxPingAttempts()) && WeblogUpdatePinger.shouldRetry(ex))
155         {
156             // We have attempts remaining, and it looks like we should retry,
157
// so requeue the entry for processing on subsequent rounds
158
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             // Remove the entry
163
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             // TODO: mark ping target invalid?
167
}
168     }
169
170
171 }
172
Popular Tags