comparison geo-backend/src/main/java/de/intevation/gnv/geobackend/base/query/cache/CacheCleaner.java @ 895:eb777022b628

Integrated a CacheCleaner that will cleanup the SQL-Cache if necessary geo-backend/trunk@958 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 20 Apr 2010 12:42:17 +0000
parents
children 02cd2935b5fa
comparison
equal deleted inserted replaced
894:d674cef2ca0d 895:eb777022b628
1 /**
2 *
3 */
4 package de.intevation.gnv.geobackend.base.query.cache;
5
6 import java.sql.Date;
7 import java.util.Collection;
8 import java.util.Iterator;
9
10 import org.apache.log4j.Logger;
11
12 import de.intevation.gnv.geobackend.base.Result;
13 import de.intevation.gnv.geobackend.base.query.DefaultQueryExceutor;
14 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
15 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
16 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
17 import de.intevation.gnv.geobackend.util.DateUtils;
18
19 /**
20 * Thread that looks every n - Minutes if an Cache has to be cleanedup.
21 * You can configure the Timeout in Seconds using the Systemproperty
22 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
23 *
24 */
25 public class CacheCleaner extends Thread {
26
27 /**
28 * the logger, used to log exceptions and additonaly information
29 */
30 private static Logger log = Logger.getLogger(CacheCleaner.class);
31
32 /**
33 * The Systemproperty-identifier for the Configuration of the TimeInterval
34 */
35 public static final String CACHE_CLEANER_INTERVAL = "caching.cleaner.interval";
36
37 /**
38 * The Time To wait between two Cleanups.
39 */
40 private long timeout = 1000 * 60; // 1 Minute
41
42 /**
43 * The Border which has to be used to Query the updated Tables.
44 */
45 private long lowerBorderTime = 0;
46
47 /**
48 * The Id to Lookup the SQL-Statement for fetchung the Names of the
49 * updated Tables.
50 */
51 private String queryID = "updated_tables";
52
53 /**
54 * Constructor
55 */
56 public CacheCleaner() {
57 this.setUp();
58 }
59
60 /**
61 * Constructor
62 * @param arg0
63 */
64 public CacheCleaner(Runnable arg0) {
65 super(arg0);
66 this.setUp();
67 }
68
69 /**
70 * Constructor
71 * @param arg0
72 */
73 public CacheCleaner(String arg0) {
74 super(arg0);
75 this.setUp();
76 }
77
78 /**
79 * Constructor
80 * @param arg0
81 * @param arg1
82 */
83 public CacheCleaner(ThreadGroup arg0, Runnable arg1) {
84 super(arg0, arg1);
85 this.setUp();
86 }
87
88 /**
89 * Constructor
90 * @param arg0
91 * @param arg1
92 */
93 public CacheCleaner(ThreadGroup arg0, String arg1) {
94 super(arg0, arg1);
95 this.setUp();
96 }
97
98 /**
99 * Constructor
100 * @param arg0
101 * @param arg1
102 */
103 public CacheCleaner(Runnable arg0, String arg1) {
104 super(arg0, arg1);
105 this.setUp();
106 }
107
108 /**
109 * Constructor
110 * @param arg0
111 * @param arg1
112 * @param arg2
113 */
114 public CacheCleaner(ThreadGroup arg0, Runnable arg1, String arg2) {
115 super(arg0, arg1, arg2);
116 this.setUp();
117 }
118
119 /**
120 * Constructor
121 * @param arg0
122 * @param arg1
123 * @param arg2
124 * @param arg3
125 */
126 public CacheCleaner(ThreadGroup arg0, Runnable arg1, String arg2, long arg3) {
127 super(arg0, arg1, arg2, arg3);
128 this.setUp();
129 }
130
131 /**
132 * Sets up the members of the CacheCleaner.
133 */
134 protected void setUp(){
135 String intervalValue = System.getProperty(CACHE_CLEANER_INTERVAL);
136 if (intervalValue != null){
137 log.info("Set Interval to "+intervalValue+" Seconds");
138 try {
139 this.timeout = Long.parseLong(intervalValue) * 1000;
140 } catch (NumberFormatException e) {
141 log.error(e,e);
142 }
143 }
144 }
145
146 /**
147 * This Method can be used do run the Main-Mechanism of the Thread e.g
148 * from a Unittest.
149 * @return
150 */
151 public boolean test(){
152 log.debug("CacheCleaner.test");
153 this.cleanup();
154 return true;
155 }
156
157 @Override
158 public void run() {
159 log.debug("CacheCleaner.run");
160 long requiredTime = 0;
161 this.lowerBorderTime = System.currentTimeMillis();
162 while (true){
163 long startTime = 0;
164 try {
165 long nextTimeout = timeout-requiredTime;
166 if (nextTimeout > 0){
167 Thread.sleep(nextTimeout);
168 }
169 startTime = System.currentTimeMillis();
170 log.debug("Sleep "+nextTimeout+"ms cleanup Cache now");
171 this.cleanup();
172 } catch (InterruptedException e) {
173 log.error(e,e);
174 } catch (Exception e){
175 log.error(e,e);
176 } catch (Throwable t){
177 log.error(t,t);
178 }finally{
179 requiredTime = System.currentTimeMillis() - startTime;
180 log.debug("CleanUp required "+requiredTime);
181 }
182 }
183 }
184
185 /**
186 * Method that do the Cleanup-Work
187 */
188 protected void cleanup(){
189 log.debug("CacheCleaner.cleanup");
190 try {
191 // We have to go this Way to avoid using the Cache for this Query.
192 String[] tableNames = this.getUpdatedTableNames();
193 if (tableNames != null){
194 QueryExecutorFactory.getInstance()
195 .getQueryExecutor().clearCache(tableNames);
196 }
197 } catch (QueryException e) {
198 log.error(e,e);
199 }
200
201 }
202
203 /**
204 * Returns the Names of the Tables which have been updated.
205 * @return the Names of the Tables which have been updated.
206 * @throws QueryException
207 */
208 protected String[] getUpdatedTableNames()throws QueryException {
209 String[] tableNames = null;
210 QueryExecutor queryExecutor = new DefaultQueryExceutor();
211 Date date = new Date(this.lowerBorderTime);
212 this.lowerBorderTime = System.currentTimeMillis();
213 log.debug("New Lookup at "+DateUtils.getPatternedDateAmer(new Date(lowerBorderTime)));
214 String queryDate = DateUtils.getPatternedDateAmer(date);
215 Collection<Result> result = queryExecutor.
216 executeQuery(queryID,
217 new String[]{queryDate});
218 if (result != null && !result.isEmpty()){
219 tableNames = new String[result.size()];
220 Iterator<Result> it = result.iterator();
221 int i = 0;
222 while (it.hasNext()){
223 tableNames[i++] = it.next().getString(0).toUpperCase();
224 log.debug("Table that was updated: "+tableNames[i-1]);
225 }
226 }
227 return tableNames;
228 }
229 }

http://dive4elements.wald.intevation.org