View Javadoc

1   /*$Id: CachingFileJobFactory.java,v 1.3 2006/01/04 09:52:31 clq2 Exp $
2    * Created on 11-Apr-2005
3    *
4    * Copyright (C) AstroGrid. All rights reserved.
5    *
6    * This software is published under the terms of the AstroGrid 
7    * Software License version 1.2, a copy of which has been included 
8    * with this distribution in the LICENSE.txt file.  
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          //cache.stuff(wf); not safe to stuff in the cache if it's a cache miss on read - as is being done from the read thread.
84          return wf;
85      }
86  
87   /*can't easily cache this one - pity, as it's the one used most by the client - needs a redesign.
88      public Iterator findUserJobs(Account acc) throws JobException {
89          
90          return .findUserJobs(acc);
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 $Log: CachingFileJobFactory.java,v $
114 Revision 1.3  2006/01/04 09:52:31  clq2
115 jes-gtr-1462
116 
117 Revision 1.2.42.1  2005/12/09 23:11:55  gtr
118 I refactored the base-directory feature out of its inner class and interface in FileJobFactory and into org.aastrogrid.jes.util. This addresses part, but not all, of BZ1487.
119 
120 Revision 1.2  2005/04/25 12:13:54  clq2
121 jes-nww-776-again
122 
123 Revision 1.1.2.2  2005/04/12 17:07:51  nw
124 added cache to filestore
125 
126 Revision 1.1.2.1  2005/04/11 16:31:14  nw
127 updated version of xstream.
128 added caching to job factory
129 
130 Revision 1.1.2.1  2005/04/11 13:55:56  nw
131 started using common-namegen
132  
133 */