File Source: RollerTaskWithLeasing.java
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 package org.apache.roller.weblogger.business.runnable;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.roller.weblogger.WebloggerException;
24 import org.apache.roller.weblogger.business.WebloggerFactory;
25
26
27 /**
28 * An abstract class representing a scheduled task in Roller that will always
29 * attempt to acquire a lease before doing its work.
30 */
/*
P/P * Method: void org.apache.roller.weblogger.business.runnable.RollerTaskWithLeasing()
*/
31 public abstract class RollerTaskWithLeasing extends RollerTask {
32
/*
P/P * Method: org.apache.roller.weblogger.business.runnable.RollerTaskWithLeasing__static_init
*
* Postconditions:
* init'ed(log)
*/
33 private static Log log = LogFactory.getLog(RollerTaskWithLeasing.class);
34
35
36 /**
37 * Run the task.
38 */
39 public abstract void runTask() throws WebloggerException;
40
41
42 /**
43 * The run() method as called by our thread manager.
44 *
45 * This method is purposely defined as "final" so that any tasks that are
46 * defined may not override it and remove any of its functionality. It is
47 * setup to provide some basic functionality to the running of all tasks,
48 * such as lease acquisition and releasing.
49 *
50 * Roller tasks should put their logic in the runTask() method.
51 */
52 public final void run() {
53
/*
P/P * Method: void run()
*
* Preconditions:
* log != null
* org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider != null
* org/apache/roller/weblogger/business/WebloggerFactory.webloggerProvider.webloggerInstance != null
* (soft) org/apache/roller/weblogger/business/WebloggerImpl.log != null
*
* Presumptions:
* getWeblogger(...).autoPingManager != null
* getWeblogger(...).bookmarkManager != null
* getWeblogger(...).fileManager != null
* getWeblogger(...).pingQueueManager != null
* getWeblogger(...).pingTargetManager != null
* ...
*/
54 ThreadManager mgr = WebloggerFactory.getWeblogger().getThreadManager();
55
56 boolean lockAcquired = false;
57 try {
58 log.debug(getName()+": Attempting to acquire lease");
59
60 lockAcquired = mgr.registerLease(this);
61
62 // now if we have a lock then run the task
63 if(lockAcquired) {
64 log.debug(getName()+": Lease acquired, running task");
65 this.runTask();
66 } else {
67 log.debug(getName()+": Lease NOT acquired, cannot continue");
68 return;
69 }
70
71 } catch (Exception ex) {
72 log.error(getName()+": Unexpected exception", ex);
73 } finally {
74
75 if(lockAcquired) {
76
77 log.debug(getName()+": Attempting to release lease");
78
79 boolean lockReleased = mgr.unregisterLease(this);
80
81 if(lockReleased) {
82 log.debug(getName()+": Lease released, task finished");
83 } else {
84 log.debug(getName()+": Lease NOT released, some kind of problem");
85 }
86 }
87
88 // always release Roller session
89 WebloggerFactory.getWeblogger().release();
90 }
91
92 }
93
94 }
SofCheck Inspector Build Version : 2.18479
| RollerTaskWithLeasing.java |
2009-Jan-02 14:25:20 |
| RollerTaskWithLeasing.class |
2009-Sep-04 03:12:31 |