Commit fa1bc0948d97b5969918a6de0efe6bdc9c68682b
1 parent
b5e5f53b6d
Exists in
master
remove classes moved into inside the product
Showing
2 changed files
with
0 additions
and
287 deletions
Show diff stats
src/main/java/org/alfresco/schedule/AbstractScheduledLockedJob.java
... | ... | @@ -1,92 +0,0 @@ |
1 | -/* | |
2 | - * Copyright (C) 2005-2013 Alfresco Software Limited. | |
3 | - * | |
4 | - * This program is free software; you can redistribute it and/or | |
5 | - * modify it under the terms of the GNU General Public License | |
6 | - * as published by the Free Software Foundation; either version 2 | |
7 | - * of the License, or (at your option) any later version. | |
8 | - | |
9 | - * This program is distributed in the hope that it will be useful, | |
10 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | - * GNU General Public License for more details. | |
13 | - | |
14 | - * You should have received a copy of the GNU General Public License | |
15 | - * along with this program; if not, write to the Free Software | |
16 | - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | - | |
18 | - * As a special exception to the terms and conditions of version 2.0 of | |
19 | - * the GPL, you may redistribute this Program in connection with Free/Libre | |
20 | - * and Open Source Software ("FLOSS") applications as described in Alfresco's | |
21 | - * FLOSS exception. You should have recieved a copy of the text describing | |
22 | - * the FLOSS exception, and it is also available here: | |
23 | - * http://www.alfresco.com/legal/licensing" | |
24 | - */ | |
25 | -package org.alfresco.schedule; | |
26 | - | |
27 | -import org.alfresco.repo.lock.JobLockService; | |
28 | -import org.quartz.JobExecutionContext; | |
29 | -import org.quartz.JobExecutionException; | |
30 | -import org.springframework.scheduling.quartz.QuartzJobBean; | |
31 | - | |
32 | -/** | |
33 | - * | |
34 | - * This class should be extended any time a scheduled job needs to be | |
35 | - * implemented to be executed using | |
36 | - * {@link org.alfresco.repo.lock.JobLockService JobLockService}. It makes the | |
37 | - * cluster aware locking of the job transparent to the implementation. On the | |
38 | - * job's spring {@link org.quartz.JobExecutionContext JobExecutionContext} it | |
39 | - * will still always have to be passed as parameter the | |
40 | - * {@link org.alfresco.repo.lock.JobLockService jobLockService}. The name to be | |
41 | - * used for locking of the job is optional, if none is passed a name will be | |
42 | - * composed using the simple name of the implementation class. In general if it | |
43 | - * may make sense to have more than one job setup using the same class you | |
44 | - * should always use a different name on each | |
45 | - * {@link org.quartz.JobExecutionContext JobExecutionContext} to differentiate | |
46 | - * the jobs, unless you want the lock to be shared between the different | |
47 | - * instances. | |
48 | - * | |
49 | - * The only method to be implemented when extending this class is the executeJob | |
50 | - * method. | |
51 | - * | |
52 | - * @author Rui Fernandes | |
53 | - * | |
54 | - */ | |
55 | -public abstract class AbstractScheduledLockedJob extends QuartzJobBean | |
56 | -{ | |
57 | - | |
58 | - private ScheduledJobLockExecuter locker; | |
59 | - | |
60 | - @Override | |
61 | - protected final void executeInternal(final JobExecutionContext jobContext) | |
62 | - throws JobExecutionException | |
63 | - { | |
64 | - if (locker == null) | |
65 | - { | |
66 | - JobLockService jobLockServiceBean = (JobLockService) jobContext | |
67 | - .getJobDetail().getJobDataMap().get("jobLockService"); | |
68 | - if (jobLockServiceBean == null) | |
69 | - throw new JobExecutionException( | |
70 | - "Missing setting for bean jobLockService"); | |
71 | - String name = (String) jobContext.getJobDetail().getJobDataMap() | |
72 | - .get("name"); | |
73 | - String jobName = name == null ? this.getClass().getSimpleName() | |
74 | - : name; | |
75 | - locker = new ScheduledJobLockExecuter(jobLockServiceBean, jobName, | |
76 | - this); | |
77 | - } | |
78 | - locker.execute(jobContext); | |
79 | - } | |
80 | - | |
81 | - /** | |
82 | - * | |
83 | - * This is the method that should be implemented by any extension of the | |
84 | - * abstract class. It won't need to worry about any lockings of the job and | |
85 | - * can focus only on its specific task. | |
86 | - * | |
87 | - * @param jobContext | |
88 | - * @throws JobExecutionException | |
89 | - */ | |
90 | - public abstract void executeJob(JobExecutionContext jobContext) | |
91 | - throws JobExecutionException; | |
92 | -} |
src/main/java/org/alfresco/schedule/ScheduledJobLockExecuter.java
... | ... | @@ -1,195 +0,0 @@ |
1 | -/* | |
2 | - * Copyright (C) 2005-2013 Alfresco Software Limited. | |
3 | - * | |
4 | - * This program is free software; you can redistribute it and/or | |
5 | - * modify it under the terms of the GNU General Public License | |
6 | - * as published by the Free Software Foundation; either version 2 | |
7 | - * of the License, or (at your option) any later version. | |
8 | - | |
9 | - * This program is distributed in the hope that it will be useful, | |
10 | - * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | - * GNU General Public License for more details. | |
13 | - | |
14 | - * You should have received a copy of the GNU General Public License | |
15 | - * along with this program; if not, write to the Free Software | |
16 | - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | - | |
18 | - * As a special exception to the terms and conditions of version 2.0 of | |
19 | - * the GPL, you may redistribute this Program in connection with Free/Libre | |
20 | - * and Open Source Software ("FLOSS") applications as described in Alfresco's | |
21 | - * FLOSS exception. You should have recieved a copy of the text describing | |
22 | - * the FLOSS exception, and it is also available here: | |
23 | - * http://www.alfresco.com/legal/licensing" | |
24 | - */ | |
25 | -package org.alfresco.schedule; | |
26 | - | |
27 | - | |
28 | -import org.alfresco.repo.lock.JobLockService; | |
29 | -import org.alfresco.repo.lock.LockAcquisitionException; | |
30 | -import org.alfresco.service.namespace.NamespaceService; | |
31 | -import org.alfresco.service.namespace.QName; | |
32 | -import org.alfresco.util.Pair; | |
33 | -import org.alfresco.util.VmShutdownListener.VmShutdownException; | |
34 | -import org.apache.commons.logging.Log; | |
35 | -import org.apache.commons.logging.LogFactory; | |
36 | -import org.quartz.JobExecutionContext; | |
37 | -import org.quartz.JobExecutionException; | |
38 | - | |
39 | -/** | |
40 | - * | |
41 | - * This class encapsulates the {@link org.alfresco.repo.lock.JobLockService | |
42 | - * JobLockService} usage in order to guarantee that a job is not executed | |
43 | - * simultaneously in more than one node in a cluster. After instantiated passing | |
44 | - * in constructor {@link org.alfresco.schedule.AbstractScheduledLockedJob job} | |
45 | - * to be executed, as well as the name of the to be locked job and the | |
46 | - * {@link org.alfresco.repo.lock.JobLockService JobLockService}, the execute | |
47 | - * method of this class will execute the job taking care of all cluster aware | |
48 | - * lockings. | |
49 | - * | |
50 | - * This code is based on original code by Derek Hurley on | |
51 | - * {@link org.alfresco.repo.content.cleanup.ContentStoreCleaner | |
52 | - * ContentStoreCleaner}. Extracting the generic locking code in order to be | |
53 | - * reused and avoid code duplication. | |
54 | - * | |
55 | - * @author Rui Fernandes | |
56 | - * | |
57 | - */ | |
58 | - | |
59 | -public class ScheduledJobLockExecuter | |
60 | -{ | |
61 | - | |
62 | - private static Log logger = LogFactory | |
63 | - .getLog(ScheduledJobLockExecuter.class.getName()); | |
64 | - | |
65 | - private static final long LOCK_TTL = 30000L; | |
66 | - private static ThreadLocal<Pair<Long, String>> lockThreadLocal = new ThreadLocal<Pair<Long, String>>(); | |
67 | - private JobLockService jobLockService; | |
68 | - private QName lockQName; | |
69 | - private AbstractScheduledLockedJob job; | |
70 | - | |
71 | - /** | |
72 | - * | |
73 | - * @param jobLockService | |
74 | - * the {@link org.alfresco.repo.lock.JobLockService | |
75 | - * JobLockService} | |
76 | - * @param name | |
77 | - * the name of the job to be used for the lock registry | |
78 | - * @param job | |
79 | - * the {@link org.alfresco.schedule.AbstractScheduledLockedJob | |
80 | - * job} to be executed | |
81 | - */ | |
82 | - public ScheduledJobLockExecuter(JobLockService jobLockService, String name, | |
83 | - AbstractScheduledLockedJob job) | |
84 | - { | |
85 | - this.jobLockService = jobLockService; | |
86 | - lockQName = QName.createQName(NamespaceService.SYSTEM_MODEL_1_0_URI, | |
87 | - name); | |
88 | - this.job = job; | |
89 | - } | |
90 | - | |
91 | - /** | |
92 | - * | |
93 | - * It will execute the | |
94 | - * {@link org.alfresco.schedule.AbstractScheduledLockedJob job} passed on | |
95 | - * instantiation taking care of all cluster aware lockings. | |
96 | - * | |
97 | - * @param jobContext | |
98 | - * the usual quartz job context | |
99 | - * @throws JobExecutionException | |
100 | - */ | |
101 | - public void execute(JobExecutionContext jobContext) | |
102 | - throws JobExecutionException | |
103 | - { | |
104 | - try | |
105 | - { | |
106 | - if (logger.isDebugEnabled()) | |
107 | - { | |
108 | - logger.debug(String.format(" Job %s started.", | |
109 | - lockQName.getLocalName())); | |
110 | - } | |
111 | - refreshLock(); | |
112 | - job.executeJob(jobContext); | |
113 | - if (logger.isDebugEnabled()) | |
114 | - { | |
115 | - logger.debug(String.format(" Job %s completed.", | |
116 | - lockQName.getLocalName())); | |
117 | - } | |
118 | - } catch (LockAcquisitionException e) | |
119 | - { | |
120 | - // Job being done by another process | |
121 | - if (logger.isDebugEnabled()) | |
122 | - { | |
123 | - logger.debug(String.format(" Job %s already underway.", | |
124 | - lockQName.getLocalName())); | |
125 | - } | |
126 | - } catch (VmShutdownException e) | |
127 | - { | |
128 | - // Aborted | |
129 | - if (logger.isDebugEnabled()) | |
130 | - { | |
131 | - logger.debug(String.format(" Job %s aborted.", | |
132 | - lockQName.getLocalName())); | |
133 | - } | |
134 | - } finally | |
135 | - { | |
136 | - releaseLock(); | |
137 | - } | |
138 | - } | |
139 | - | |
140 | - /** | |
141 | - * Lazily update the job lock | |
142 | - * | |
143 | - */ | |
144 | - private void refreshLock() | |
145 | - { | |
146 | - Pair<Long, String> lockPair = lockThreadLocal.get(); | |
147 | - if (lockPair == null) | |
148 | - { | |
149 | - String lockToken = jobLockService.getLock(lockQName, LOCK_TTL); | |
150 | - Long lastLock = new Long(System.currentTimeMillis()); | |
151 | - // We have not locked before | |
152 | - lockPair = new Pair<Long, String>(lastLock, lockToken); | |
153 | - lockThreadLocal.set(lockPair); | |
154 | - } else | |
155 | - { | |
156 | - long now = System.currentTimeMillis(); | |
157 | - long lastLock = lockPair.getFirst().longValue(); | |
158 | - String lockToken = lockPair.getSecond(); | |
159 | - // Only refresh the lock if we are past a threshold | |
160 | - if (now - lastLock > (long) (LOCK_TTL / 2L)) | |
161 | - { | |
162 | - jobLockService.refreshLock(lockToken, lockQName, LOCK_TTL); | |
163 | - lastLock = System.currentTimeMillis(); | |
164 | - lockPair = new Pair<Long, String>(lastLock, lockToken); | |
165 | - // I think it was missing the following code line, on the | |
166 | - // original | |
167 | - // org.alfresco.repo.content.cleanup.ContentStoreCleaner locking | |
168 | - // code - Rui Fernandes | |
169 | - lockThreadLocal.set(lockPair); | |
170 | - } | |
171 | - } | |
172 | - } | |
173 | - | |
174 | - /** | |
175 | - * Release the lock after the job completes | |
176 | - */ | |
177 | - private void releaseLock() | |
178 | - { | |
179 | - Pair<Long, String> lockPair = lockThreadLocal.get(); | |
180 | - if (lockPair != null) | |
181 | - { | |
182 | - // We can't release without a token | |
183 | - try | |
184 | - { | |
185 | - jobLockService.releaseLock(lockPair.getSecond(), lockQName); | |
186 | - } finally | |
187 | - { | |
188 | - // Reset | |
189 | - lockThreadLocal.set(null); | |
190 | - } | |
191 | - } | |
192 | - // else: We can't release without a token | |
193 | - } | |
194 | - | |
195 | -} |