annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 143:7e20702a90ed

Implemented an abstract class for the output of artifacts and collections. Added a CollectionOutResource. artifacts/trunk@1368 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 02 Mar 2011 16:02:47 +0000
parents 933bbc9fc11f
children b2115f484edb
rev   line source
100
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
1 /*
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
3 *
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
5 * Read the file LGPL.txt coming with the software for details
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
7 */
933bbc9fc11f Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 93
diff changeset
8
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 package de.intevation.artifactdatabase;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import de.intevation.artifacts.Artifact;
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 java.sql.Connection;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import java.sql.PreparedStatement;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import java.sql.ResultSet;
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
16 import java.sql.SQLException;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
17
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
18 import java.util.ArrayList;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
19 import java.util.List;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21 import javax.sql.DataSource;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 import org.apache.log4j.Logger;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 /**
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
26 * 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
27 * 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
28 * 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
29 * 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
30 * 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
31 * 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
32 * is called.<br>
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
33 * 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
34 * resources form the system.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
35 *
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
36 * @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
37 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
38 public class DatabaseCleaner
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
39 extends Thread
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
40 {
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
41 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
42 * Implementors of this interface are able to create a
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
43 * living artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
44 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
45 public interface ArtifactReviver {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
46
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
47 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
48 * Called to revive an artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
49 * @param factoryName The name of the factory which
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
50 * created this artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
51 * @param bytes The bytes of the serialized artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
52 * @return The revived artfiact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
53 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
54 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
55
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
56 } // interface ArtifactReviver
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
57
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 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
59
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
60 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
61 * Number of artifacts to be loaded at once. Used to
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
62 * mitigate the problem of a massive denial of service
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
63 * if too many artifacts have died since last cleanup.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
64 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 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
66
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
67 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
68 * The SQL statement to select the outdated artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
69 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 public static final String SQL_OUTDATED =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 SQL.get("artifacts.outdated");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
73 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
74 * The SQL statement to delete some artifacts from the database.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
75 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 public static final String SQL_DELETE =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 SQL.get("artifacts.delete");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
79 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
80 * 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
81 * cleanups. This is stored in the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
82 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 public static final String SLEEP_XPATH =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 "/artifact-database/cleaner/sleep-time/text()";
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
86 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
87 * Default nap time between cleanups: 5 minutes.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
88 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 public static final long SLEEP_DEFAULT =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 5 * 60 * 1000L; // 5 minutes
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
92 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
93 * The configured nap time.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
94 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95 protected long sleepTime;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
96
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
97 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
98 * Internal locking mechanism to prevent some race conditions.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
99 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 protected Object sleepLock = new Object();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
101
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
102 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
103 * A reference to the global context.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
104 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 protected Object context;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
107 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
108 * A specialized Id filter which only delete some artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
109 * This is used to prevent deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
110 */
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
111 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
112
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
113 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
114 * The reviver used to bring the dead artifact on last
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
115 * time back to live to call endOfLife() on them.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
116 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
117 protected ArtifactReviver reviver;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
118
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
119 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
120 * Default constructor.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
121 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 public DatabaseCleaner() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
124
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
125 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
126 * 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
127 * and a given reviver.
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
128 * @param context The global context of the artifact database
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
129 * @param reviver The reviver to awake artifact one last time.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
130 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
131 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
132 setDaemon(true);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 sleepTime = getSleepTime();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134 this.context = context;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
135 this.reviver = reviver;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
136 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
137
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
138 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
139 * Sets the filter that prevents deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
140 * Living artifacts are artifacts which are currently active
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
141 * inside the artifact database. Deleting them in this state
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
142 * would create severe internal problems.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
143 * @param filter
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
144 */
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
145 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
146 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
147 }
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
148
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
149 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
150 * 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
151 * 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
152 * database finds an artifact which is already outdated.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
153 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
154 public void wakeup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
155 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
156 sleepLock.notify();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
157 }
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
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
160 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
161 * Fetches the sleep time from the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
162 * @return the time to sleep between database cleanups in ms.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
163 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 protected static long getSleepTime() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 String sleepTimeString = Config.getStringXPath(SLEEP_XPATH);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 if (sleepTimeString == null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 // sleep at least one second
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172 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
173 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 catch (NumberFormatException nfe) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 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
176 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179
47
4ae4dc99127d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 41
diff changeset
180 private static final class IdData
4ae4dc99127d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 41
diff changeset
181 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
182 {
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183 byte [] data;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
184 String factoryName;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
186 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
187 super(id);
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
188 this.factoryName = factoryName;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
189 this.data = data;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191 } // class IdData
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 /**
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 * 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
195 * 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
196 * 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
197 * 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
198 * 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
199 * 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
200 * 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
201 * is repeated.
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 protected void cleanup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 logger.info("database cleanup");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
205
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
206 Connection connection = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
207 PreparedStatement fetchIds = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
208 PreparedStatement deleteId = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
209 ResultSet result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
210
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
211 int removedArtifacts = 0;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
212
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
213 DataSource dataSource = DBConnection.getDataSource();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
215 connection = dataSource.getConnection();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
216 connection.setAutoCommit(false);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217 fetchIds = connection.prepareStatement(SQL_OUTDATED);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 deleteId = connection.prepareStatement(SQL_DELETE);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
220 // 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
221 // in SQL statements.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222 fetchIds.setMaxRows(MAX_ROWS);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
224 for (;;) {
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
225 List ids = new ArrayList();
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 result = fetchIds.executeQuery();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
228
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
229 while (result.next()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
230 ids.add(new IdData(
47
4ae4dc99127d Removed trailing whitespace.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 41
diff changeset
231 result.getInt(1),
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
232 result.getString(2),
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
233 result.getBytes(3)));
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
234 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
235
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
236 result.close(); result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
237
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
238 if (ids.isEmpty()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
239 break;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
240 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
241
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
242 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
243 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
244 }
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
245
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
246 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
247 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
248 Artifact artifact = reviver.reviveArtifact(
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
249 idData.factoryName, idData.data);
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
250 idData.data = null;
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 deleteId.setInt(1, idData.id);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
253 deleteId.execute();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
254 connection.commit();
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 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
257 if (artifact != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
258 artifact.endOfLife(context);
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 (Exception e) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
262 logger.error(e.getLocalizedMessage(), e);
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 } // for all fetched data
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
265
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
266 removedArtifacts += ids.size();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
267 }
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 catch (SQLException sqle) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
270 logger.error(sqle.getLocalizedMessage(), sqle);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
272 finally {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
273 if (result != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
274 try { result.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 (fetchIds != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
278 try { fetchIds.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 if (deleteId != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
282 try { deleteId.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
283 catch (SQLException sqle) {}
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 if (connection != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286 try { connection.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
287 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
288 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
289 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
290
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
291 logger.info("artifacts removed: " + removedArtifacts);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
292 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
293
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
294 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
295 * 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
296 * 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
297 */
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
298 @Override
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
299 public void run() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
300 logger.info("sleep time: " + sleepTime + "ms");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
301 for (;;) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
302 cleanup();
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
303 long startTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
304
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
305 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
306 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
307 sleepLock.wait(sleepTime);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
308 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
309 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
310 catch (InterruptedException ie) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
311 }
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
312
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
313 long stopTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
314
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
315 if (logger.isDebugEnabled()) {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
316 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
317 }
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
318 } // for (;;)
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
319 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
320 }
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
321 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org