annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DatabaseCleaner.java @ 231:1ea35226a6de

Database cleaner: Ignoring locked artifacts is now done on database level. artifacts/trunk@1635 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 31 Mar 2011 15:56:50 +0000
parents fbd57d2eeaef
children 202b6ae2fcad
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
207
b2115f484edb Move Config and XMLUtils totally to commons package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 100
diff changeset
11 import de.intevation.artifacts.common.utils.Config;
b2115f484edb Move Config and XMLUtils totally to commons package.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 100
diff changeset
12
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 import de.intevation.artifacts.Artifact;
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 java.sql.Connection;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16 import java.sql.PreparedStatement;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import java.sql.ResultSet;
93
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
18 import java.sql.SQLException;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
19
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
20 import java.util.ArrayList;
e27cf9c84eb8 Unified imports.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 91
diff changeset
21 import java.util.List;
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
22 import java.util.Set;
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
23 import java.util.Collections;
30
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 import javax.sql.DataSource;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 import org.apache.log4j.Logger;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 /**
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
30 * 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
31 * 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
32 * 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
33 * 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
34 * 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
35 * 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
36 * is called.<br>
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
37 * 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
38 * resources form the system.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
39 *
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
40 * @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
41 */
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 public class DatabaseCleaner
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43 extends Thread
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 {
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
45 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
46 * Implementors of this interface are able to create a
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
47 * living artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
48 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
49 public interface ArtifactReviver {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
50
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
51 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
52 * Called to revive an artifact from a given byte array.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
53 * @param factoryName The name of the factory which
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
54 * created this artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
55 * @param bytes The bytes of the serialized artifact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
56 * @return The revived artfiact.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
57 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
58 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
59
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
60 } // interface ArtifactReviver
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
61
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
62 public interface LockedIdsProvider {
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
63 Set<Integer> getLockedIds();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
64 } // interface LockedIdsProvider
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
65
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 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
67
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
68 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
69 * Number of artifacts to be loaded at once. Used to
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
70 * mitigate the problem of a massive denial of service
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
71 * if too many artifacts have died since last cleanup.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
72 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 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
74
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
75 public static final Set<Integer> EMPTY_IDS = Collections.emptySet();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
76
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
77 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
78 * The SQL statement to select the outdated artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
79 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 public static final String SQL_OUTDATED =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 SQL.get("artifacts.outdated");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
83 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
84 * The SQL statement to delete some artifacts from the database.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
85 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 public static final String SQL_DELETE =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87 SQL.get("artifacts.delete");
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 * 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
91 * cleanups. This is stored in the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
92 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 public static final String SLEEP_XPATH =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 "/artifact-database/cleaner/sleep-time/text()";
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
96 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
97 * Default nap time between cleanups: 5 minutes.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
98 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
99 public static final long SLEEP_DEFAULT =
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
100 5 * 60 * 1000L; // 5 minutes
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 * The configured nap time.
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 long sleepTime;
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 * Internal locking mechanism to prevent some race conditions.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
109 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
110 protected Object sleepLock = new Object();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
111
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
112 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
113 * A reference to the global context.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
114 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
115 protected Object context;
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 * A specialized Id filter which only delete some artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
119 * This is used to prevent deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
120 */
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
121 protected LockedIdsProvider lockedIdsProvider;
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
122
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
123 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
124 * The reviver used to bring the dead artifact on last
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
125 * time back to live to call endOfLife() on them.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
126 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
127 protected ArtifactReviver reviver;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
128
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
129 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
130 * Default constructor.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
131 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
132 public DatabaseCleaner() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
133 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
134
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
135 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
136 * 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
137 * and a given reviver.
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
138 * @param context The global context of the artifact database
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
139 * @param reviver The reviver to awake artifact one last time.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
140 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
141 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
142 setDaemon(true);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
143 sleepTime = getSleepTime();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
144 this.context = context;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
145 this.reviver = reviver;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
146 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
147
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
148 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
149 * Sets the filter that prevents deletion of living artifacts.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
150 * Living artifacts are artifacts which are currently active
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
151 * inside the artifact database. Deleting them in this state
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
152 * would create severe internal problems.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
153 * @param filter
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
154 */
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
155 public void setLockedIdsProvider(LockedIdsProvider lockedIdsProvider) {
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
156 this.lockedIdsProvider = lockedIdsProvider;
32
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
157 }
c2d53bd30ab8 Re-factored artifact API for better integration of background processing.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 30
diff changeset
158
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
159 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
160 * 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
161 * 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
162 * database finds an artifact which is already outdated.
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 public void wakeup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 sleepLock.notify();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }
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
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
170 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
171 * Fetches the sleep time from the global configuration.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
172 * @return the time to sleep between database cleanups in ms.
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
173 */
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174 protected static long getSleepTime() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 String sleepTimeString = Config.getStringXPath(SLEEP_XPATH);
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 if (sleepTimeString == null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181 // sleep at least one second
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
182 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
183 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 catch (NumberFormatException nfe) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
185 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
186 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 return SLEEP_DEFAULT;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
190 private static final class IdData {
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
191
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
192 int id;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 byte [] data;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
194 String factoryName;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
196 public IdData(int id, String factoryName, byte [] data) {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
197 this.id = id;
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
198 this.factoryName = factoryName;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
199 this.data = data;
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
200 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
201 } // class IdData
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 /**
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
204 * 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
205 * 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
206 * 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
207 * 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
208 * 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
209 * 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
210 * 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
211 * is repeated.
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 protected void cleanup() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
214 logger.info("database cleanup");
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 Connection connection = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
217 PreparedStatement fetchIds = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
218 PreparedStatement deleteId = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
219 ResultSet result = null;
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 int removedArtifacts = 0;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
222
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
223 DataSource dataSource = DBConnection.getDataSource();
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
224
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
225 Set<Integer> lockedIds = lockedIdsProvider != null
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
226 ? lockedIdsProvider.getLockedIds()
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
227 : EMPTY_IDS;
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
228
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
229 String questionMarks = StringUtils.repeat('?', lockedIds.size(), ',');
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
230
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
231 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
232 connection = dataSource.getConnection();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
233 connection.setAutoCommit(false);
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
234
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
235 fetchIds = connection.prepareStatement(
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
236 SQL_OUTDATED.replace("$LOCKED_IDS$", questionMarks));
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
237
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
238 int idx = 1;
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
239 for (Integer id: lockedIds) {
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
240 fetchIds.setInt(idx++, id);
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
241 }
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
242
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
243 deleteId = connection.prepareStatement(SQL_DELETE);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
244
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
245 // 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
246 // in SQL statements.
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
247 fetchIds.setMaxRows(MAX_ROWS);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
248
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
249 for (;;) {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
250 List<IdData> ids = new ArrayList<IdData>();
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
251
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
252 result = fetchIds.executeQuery();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
253
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
254 while (result.next()) {
231
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
255 ids.add(new IdData(
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
256 result.getInt(1),
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
257 result.getString(2),
1ea35226a6de Database cleaner: Ignoring locked artifacts is now done on database level.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 230
diff changeset
258 result.getBytes(3)));
30
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 result.close(); result = null;
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
262
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
263 if (ids.isEmpty()) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
264 break;
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
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
267 for (int i = ids.size()-1; i >= 0; --i) {
230
fbd57d2eeaef Changed semantics of locked artifact ids.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 207
diff changeset
268 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
269 Artifact artifact = reviver.reviveArtifact(
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 32
diff changeset
270 idData.factoryName, idData.data);
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
271 idData.data = null;
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 deleteId.setInt(1, idData.id);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
274 deleteId.execute();
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
275 connection.commit();
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 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
278 if (artifact != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
279 artifact.endOfLife(context);
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 catch (Exception e) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
283 logger.error(e.getLocalizedMessage(), e);
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 } // for all fetched data
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
286
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
287 removedArtifacts += ids.size();
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 catch (SQLException sqle) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
291 logger.error(sqle.getLocalizedMessage(), sqle);
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 finally {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
294 if (result != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
295 try { result.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
296 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
297 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
298 if (fetchIds != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
299 try { fetchIds.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
300 catch (SQLException sqle) {}
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 if (deleteId != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
303 try { deleteId.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
304 catch (SQLException sqle) {}
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
305 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
306 if (connection != null) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
307 try { connection.close(); }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
308 catch (SQLException sqle) {}
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 }
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 logger.info("artifacts removed: " + removedArtifacts);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
313 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
314
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
315 /**
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
316 * 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
317 * 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
318 */
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
319 @Override
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
320 public void run() {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
321 logger.info("sleep time: " + sleepTime + "ms");
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
322 for (;;) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
323 cleanup();
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
324 long startTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
325
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
326 try {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
327 synchronized (sleepLock) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
328 sleepLock.wait(sleepTime);
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
329 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
330 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
331 catch (InterruptedException ie) {
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
332 }
48
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
333
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
334 long stopTime = System.currentTimeMillis();
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
335
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
336 if (logger.isDebugEnabled()) {
41c225c8bd41 Add i18n support via "Accept-Language" HTTP headers.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 47
diff changeset
337 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
338 }
30
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
339 } // for (;;)
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
340 }
88972c6daa4f Added a cleanup thread which periodically removes
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
341 }
90
68285f7bc476 More javadoc.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 80
diff changeset
342 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org