1
2
3
4
5
6
7
8
9
10
11 package org.astrogrid.jes.impl.workflow;
12
13 import org.apache.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15
16 import org.astrogrid.community.beans.v1.Account;
17 import org.astrogrid.jes.job.DuplicateFoundException;
18 import org.astrogrid.jes.job.JobException;
19 import org.astrogrid.jes.job.JobFactory;
20 import org.astrogrid.jes.job.NotFoundException;
21 import org.astrogrid.jes.util.BaseDirectory;
22 import org.astrogrid.jes.util.Cache;
23 import org.astrogrid.workflow.beans.v1.Workflow;
24 import org.astrogrid.workflow.beans.v1.execution.JobURN;
25
26 import org.apache.commons.collections.map.LRUMap;
27 import org.apache.commons.collections.map.ReferenceMap;
28 import org.apache.commons.transaction.file.ResourceManagerException;
29
30 import java.io.IOException;
31 import java.lang.ref.Reference;
32 import java.lang.ref.SoftReference;
33 import java.util.Iterator;
34 import java.util.Map;
35
36 /*** Decorator for another job factory that maintains a LRU cache of workflow documents - hopefully we can see some performance gain
37 * by reducing the amount of xml parsing and marshalling required.
38 *
39 * idea is that cache never fails / disrupts the system. any update gets written back to store and cache. however, reads may stop at cache, if match is found.
40 * assumes a single-thread write design - which is what we've got.
41 * @author Noel Winstanley nw@jb.man.ac.uk 11-Apr-2005
42 *
43 */
44 public class CachingFileJobFactory extends FileJobFactoryImpl {
45 /***
46 * Commons Logger for this class
47 */
48 private static final Log logger = LogFactory.getLog(CachingFileJobFactory.class);
49
50 /*** Construct a new CachingJobFactoryDecorator
51 * @throws IOException
52 * @throws ResourceManagerException
53 * @throws IllegalArgumentException
54 *
55 */
56 public CachingFileJobFactory(BaseDirectory bd) throws IllegalArgumentException,
57 IOException,
58 ResourceManagerException{
59 super(bd);
60 }
61 protected static final int WORKFLOW_CACHE_SIZE = 20;
62 protected final Cache cache = new Cache(WORKFLOW_CACHE_SIZE);
63
64 /***
65 * @see org.astrogrid.jes.job.JobFactory#initializeJob(org.astrogrid.workflow.beans.v1.Workflow)
66 */
67 public Workflow initializeJob(Workflow req) throws JobException {
68 Workflow wf = super.initializeJob(req);
69 cache.stuff(wf.getJobExecutionRecord().getJobId(),wf);
70 return wf;
71 }
72
73 /***
74 * @see org.astrogrid.jes.job.JobFactory#findJob(org.astrogrid.workflow.beans.v1.execution.JobURN)
75 */
76 public Workflow findJob(JobURN urn) throws JobException, NotFoundException,
77 DuplicateFoundException {
78 Workflow wf = (Workflow)cache.check(urn);
79 if (wf != null) {
80 return wf;
81 }
82 wf = super.findJob(urn);
83
84 return wf;
85 }
86
87
88
89
90
91
92
93 /***
94 * @see org.astrogrid.jes.job.JobFactory#deleteJob(org.astrogrid.workflow.beans.v1.Workflow)
95 */
96 public void deleteJob(Workflow job) throws JobException {
97 super.deleteJob(job);
98 cache.delete(job.getJobExecutionRecord().getJobId());
99 }
100
101 /***
102 * @see org.astrogrid.jes.job.JobFactory#updateJob(org.astrogrid.workflow.beans.v1.Workflow)
103 */
104 public void updateJob(Workflow job) throws JobException {
105 super.updateJob(job);
106 cache.stuff(job.getJobExecutionRecord().getJobId(),job);
107 }
108
109 }
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133