Mercurial > dive4elements > framework
annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 95:0edcaf5b5c78
The path to database can be configured with a path relative to the configuration directory.
artifacts/trunk@1007 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 27 Apr 2010 16:12:37 +0000 |
parents | e27cf9c84eb8 |
children | 933bbc9fc11f |
rev | line source |
---|---|
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.artifactdatabase; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
2 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
3 import de.intevation.artifacts.Artifact; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
4 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
5 import java.sql.Connection; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
6 import java.sql.PreparedStatement; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
7 import java.sql.ResultSet; |
93
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
91
diff
changeset
|
8 import java.sql.SQLException; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
91
diff
changeset
|
9 |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
91
diff
changeset
|
10 import java.util.ArrayList; |
e27cf9c84eb8
Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
91
diff
changeset
|
11 import java.util.List; |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
12 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
13 import javax.sql.DataSource; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
14 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
15 import org.apache.log4j.Logger; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
16 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
17 /** |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
18 * The database cleaner runs in background. It sleep for a configurable |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
19 * while and when it wakes up it removes outdated artifacts from the |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
20 * database. Outdated means that the the last access to the artifact |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
21 * is longer aga then the time to live of this artifact.<br> |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
22 * Before the artifact is finally removed from the system it is |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
23 * revived one last time an the #endOfLife() method of the artifact |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
24 * is called.<br> |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
25 * The artifact implementations may e.g. use this to remove some extrenal |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
26 * resources form the system. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
27 * |
80
8447467cef86
Implementation to import artifacts from incoming xml documents (applied patch from issue208 by SLT).
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
48
diff
changeset
|
28 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a> |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
29 */ |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
30 public class DatabaseCleaner |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
31 extends Thread |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
32 { |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
33 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
34 * Implementors of this interface are able to create a |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
35 * living artifact from a given byte array. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
36 */ |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
37 public interface ArtifactReviver { |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
38 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
39 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
40 * Called to revive an artifact from a given byte array. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
41 * @param factoryName The name of the factory which |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
42 * created this artifact. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
43 * @param bytes The bytes of the serialized artifact. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
44 * @return The revived artfiact. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
45 */ |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
46 Artifact reviveArtifact(String factoryName, byte [] bytes); |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
47 |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
48 } // interface ArtifactReviver |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
49 |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
50 private static Logger logger = Logger.getLogger(DatabaseCleaner.class); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
51 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
52 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
53 * Number of artifacts to be loaded at once. Used to |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
54 * mitigate the problem of a massive denial of service |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
55 * if too many artifacts have died since last cleanup. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
56 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
57 public static final int MAX_ROWS = 50; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
58 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
59 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
60 * The SQL statement to select the outdated artifacts. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
61 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
62 public static final String SQL_OUTDATED = |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
63 SQL.get("artifacts.outdated"); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
64 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
65 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
66 * The SQL statement to delete some artifacts from the database. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
67 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
68 public static final String SQL_DELETE = |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
69 SQL.get("artifacts.delete"); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
70 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
71 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
72 * XPath to figure out how long the cleaner should sleep between |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
73 * cleanups. This is stored in the global configuration. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
74 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
75 public static final String SLEEP_XPATH = |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
76 "/artifact-database/cleaner/sleep-time/text()"; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
77 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
78 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
79 * Default nap time between cleanups: 5 minutes. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
80 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
81 public static final long SLEEP_DEFAULT = |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
82 5 * 60 * 1000L; // 5 minutes |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
83 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
84 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
85 * The configured nap time. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
86 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
87 protected long sleepTime; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
88 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
89 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
90 * Internal locking mechanism to prevent some race conditions. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
91 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
92 protected Object sleepLock = new Object(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
93 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
94 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
95 * A reference to the global context. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
96 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
97 protected Object context; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
98 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
99 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
100 * A specialized Id filter which only delete some artifacts. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
101 * This is used to prevent deletion of living artifacts. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
102 */ |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
103 protected Id.Filter filter; |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
104 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
105 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
106 * The reviver used to bring the dead artifact on last |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
107 * time back to live to call endOfLife() on them. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
108 */ |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
109 protected ArtifactReviver reviver; |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
110 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
111 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
112 * Default constructor. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
113 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
114 public DatabaseCleaner() { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
115 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
116 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
117 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
118 * Constructor to create a cleaner with a given global context |
91
730ff077a58c
More javadoc, some code formatting.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
90
diff
changeset
|
119 * and a given reviver. |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
120 * @param context The global context of the artifact database |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
121 * @param reviver The reviver to awake artifact one last time. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
122 */ |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
123 public DatabaseCleaner(Object context, ArtifactReviver reviver) { |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
124 setDaemon(true); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
125 sleepTime = getSleepTime(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
126 this.context = context; |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
127 this.reviver = reviver; |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
128 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
129 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
130 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
131 * Sets the filter that prevents deletion of living artifacts. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
132 * Living artifacts are artifacts which are currently active |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
133 * inside the artifact database. Deleting them in this state |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
134 * would create severe internal problems. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
135 * @param filter |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
136 */ |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
137 public void setFilter(Id.Filter filter) { |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
138 this.filter = filter; |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
139 } |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
140 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
141 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
142 * External hook to tell the cleaner to wake up before its |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
143 * regular nap time is over. This is the case when the artifact |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
144 * database finds an artifact which is already outdated. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
145 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
146 public void wakeup() { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
147 synchronized (sleepLock) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
148 sleepLock.notify(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
149 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
150 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
151 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
152 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
153 * Fetches the sleep time from the global configuration. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
154 * @return the time to sleep between database cleanups in ms. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
155 */ |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
156 protected static long getSleepTime() { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
157 String sleepTimeString = Config.getStringXPath(SLEEP_XPATH); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
158 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
159 if (sleepTimeString == null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
160 return SLEEP_DEFAULT; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
161 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
162 try { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
163 // sleep at least one second |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
164 return Math.max(Long.parseLong(sleepTimeString), 1000L); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
165 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
166 catch (NumberFormatException nfe) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
167 logger.warn("Cleaner sleep time defaults to " + SLEEP_DEFAULT); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
168 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
169 return SLEEP_DEFAULT; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
170 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
171 |
47
4ae4dc99127d
Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
41
diff
changeset
|
172 private static final class IdData |
4ae4dc99127d
Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
41
diff
changeset
|
173 extends Id |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
174 { |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
175 byte [] data; |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
176 String factoryName; |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
177 |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
178 public IdData(int id, String factoryName, byte [] data) { |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
179 super(id); |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
180 this.factoryName = factoryName; |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
181 this.data = data; |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
182 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
183 } // class IdData |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
184 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
185 /** |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
186 * Cleaning is done in two phases. First we fetch a list of ids |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
187 * of artifacts. If there are artifacts the cleaning is done. |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
188 * Second we load the artifacts one by one one and call there |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
189 * endOfLife() method. In this loop we remove them from database, too. |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
190 * Each deletion is commited to ensure that a sudden failure |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
191 * of the artifact database server does delete artifacts twice |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
192 * or does not delete them at all. After this the first step |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
193 * is repeated. |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
194 */ |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
195 protected void cleanup() { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
196 logger.info("database cleanup"); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
197 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
198 Connection connection = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
199 PreparedStatement fetchIds = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
200 PreparedStatement deleteId = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
201 ResultSet result = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
202 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
203 int removedArtifacts = 0; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
204 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
205 DataSource dataSource = DBConnection.getDataSource(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
206 try { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
207 connection = dataSource.getConnection(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
208 connection.setAutoCommit(false); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
209 fetchIds = connection.prepareStatement(SQL_OUTDATED); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
210 deleteId = connection.prepareStatement(SQL_DELETE); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
211 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
212 // some dbms like derby do not support LIMIT |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
213 // in SQL statements. |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
214 fetchIds.setMaxRows(MAX_ROWS); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
215 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
216 for (;;) { |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
217 List ids = new ArrayList(); |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
218 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
219 result = fetchIds.executeQuery(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
220 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
221 while (result.next()) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
222 ids.add(new IdData( |
47
4ae4dc99127d
Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
41
diff
changeset
|
223 result.getInt(1), |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
224 result.getString(2), |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
225 result.getBytes(3))); |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
226 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
227 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
228 result.close(); result = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
229 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
230 if (ids.isEmpty()) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
231 break; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
232 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
233 |
32
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
234 if (filter != null) { |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
235 ids = filter.filterIds(ids); |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
236 } |
c2d53bd30ab8
Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
30
diff
changeset
|
237 |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
238 for (int i = ids.size()-1; i >= 0; --i) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
239 IdData idData = (IdData)ids.get(i); |
41
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
240 Artifact artifact = reviver.reviveArtifact( |
5e4bc24ea438
Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
32
diff
changeset
|
241 idData.factoryName, idData.data); |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
242 idData.data = null; |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
243 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
244 deleteId.setInt(1, idData.id); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
245 deleteId.execute(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
246 connection.commit(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
247 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
248 try { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
249 if (artifact != null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
250 artifact.endOfLife(context); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
251 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
252 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
253 catch (Exception e) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
254 logger.error(e.getLocalizedMessage(), e); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
255 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
256 } // for all fetched data |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
257 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
258 removedArtifacts += ids.size(); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
259 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
260 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
261 catch (SQLException sqle) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
262 logger.error(sqle.getLocalizedMessage(), sqle); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
263 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
264 finally { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
265 if (result != null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
266 try { result.close(); } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
267 catch (SQLException sqle) {} |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
268 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
269 if (fetchIds != null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
270 try { fetchIds.close(); } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
271 catch (SQLException sqle) {} |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
272 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
273 if (deleteId != null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
274 try { deleteId.close(); } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
275 catch (SQLException sqle) {} |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
276 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
277 if (connection != null) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
278 try { connection.close(); } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
279 catch (SQLException sqle) {} |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
280 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
281 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
282 |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
283 logger.info("artifacts removed: " + removedArtifacts); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
284 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
285 |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
286 /** |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
287 * The main code of the cleaner. It sleeps for the configured |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
288 * nap time, cleans up the database, sleeps again and so on. |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
289 */ |
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
290 @Override |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
291 public void run() { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
292 logger.info("sleep time: " + sleepTime + "ms"); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
293 for (;;) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
294 cleanup(); |
48
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
295 long startTime = System.currentTimeMillis(); |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
296 |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
297 try { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
298 synchronized (sleepLock) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
299 sleepLock.wait(sleepTime); |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
300 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
301 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
302 catch (InterruptedException ie) { |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
303 } |
48
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
304 |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
305 long stopTime = System.currentTimeMillis(); |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
306 |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
307 if (logger.isDebugEnabled()) { |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
308 logger.debug("Cleaner slept " + (stopTime - startTime) + "ms"); |
41c225c8bd41
Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
47
diff
changeset
|
309 } |
30
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
310 } // for (;;) |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
311 } |
88972c6daa4f
Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff
changeset
|
312 } |
90
68285f7bc476
More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
80
diff
changeset
|
313 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |