001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. The ASF licenses this file to You
004: * under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License. For additional information regarding
015: * copyright in this work, please see the NOTICE file in the top level
016: * directory of this distribution.
017: */
018:
019: package org.apache.roller.business.pings;
020:
021: import java.util.Calendar;
022: import java.util.Date;
023: import java.util.Properties;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026: import org.apache.roller.RollerException;
027: import org.apache.roller.business.runnable.RollerTask;
028: import org.apache.roller.config.PingConfig;
029: import org.apache.roller.business.Roller;
030: import org.apache.roller.business.RollerFactory;
031:
032: /**
033: * Task for processing the ping queue at fixed intervals. This is set up during context initialization by {@link
034: * RollerContext}. The queue processing interval is currently set from the configuration {@link
035: * org.apache.roller.config.PingConfig} at startup time only.
036: *
037: * @author <a href="mailto:anil@busybuddha.org">Anil Gangolli</a>
038: */
039: public class PingQueueTask extends RollerTask {
040:
041: private static Log log = LogFactory.getLog(PingQueueTask.class);
042:
043: // a String description of when to start this task
044: private String startTimeDesc = "immediate";
045:
046: // interval at which the task is run, default is 5 minutes
047: private int interval = 5;
048:
049: // lease time given to task lock, default is 30 minutes
050: private int leaseTime = 30;
051:
052: public String getName() {
053: return "PingQueueTask";
054: }
055:
056: public Date getStartTime(Date currentTime) {
057: return getAdjustedTime(currentTime, startTimeDesc);
058: }
059:
060: public int getInterval() {
061: return this .interval;
062: }
063:
064: public int getLeaseTime() {
065: return this .leaseTime;
066: }
067:
068: public void init() throws RollerException {
069:
070: // get relevant props
071: Properties props = this .getTaskProperties();
072:
073: // extract start time
074: String startTimeStr = props.getProperty("startTime");
075: if (startTimeStr != null) {
076: this .startTimeDesc = startTimeStr;
077: }
078:
079: // extract interval
080: String intervalStr = props.getProperty("interval");
081: if (intervalStr != null) {
082: try {
083: this .interval = Integer.parseInt(intervalStr);
084: } catch (NumberFormatException ex) {
085: log.warn("Invalid interval: " + intervalStr);
086: }
087: }
088:
089: // extract lease time
090: String leaseTimeStr = props.getProperty("leaseTime");
091: if (leaseTimeStr != null) {
092: try {
093: this .leaseTime = Integer.parseInt(leaseTimeStr);
094: } catch (NumberFormatException ex) {
095: log.warn("Invalid leaseTime: " + leaseTimeStr);
096: }
097: }
098:
099: // initialize queue processor
100: PingQueueProcessor.init();
101: }
102:
103: /**
104: * Run the task once.
105: */
106: public void runTask() {
107:
108: try {
109: log.debug("task started");
110:
111: PingQueueProcessor.getInstance().processQueue();
112: RollerFactory.getRoller().flush();
113:
114: log.debug("task completed");
115:
116: } catch (RollerException e) {
117: log.error("Error while processing ping queue", e);
118: } catch (Exception ee) {
119: log.error("unexpected exception", ee);
120: } finally {
121: // always release
122: RollerFactory.getRoller().release();
123: }
124:
125: }
126:
127: }
|