Revision 2572
Added by Jing Tao over 19 years ago
src/edu/ucsb/nceas/metacat/MetacatReplication.java | ||
---|---|---|
29 | 29 |
|
30 | 30 |
import edu.ucsb.nceas.dbadapter.AbstractDatabase; |
31 | 31 |
import java.util.*; |
32 |
import java.util.Date; |
|
32 | 33 |
import java.io.*; |
33 | 34 |
import java.sql.*; |
34 | 35 |
import java.net.*; |
... | ... | |
41 | 42 |
|
42 | 43 |
public class MetacatReplication extends HttpServlet implements Runnable |
43 | 44 |
{ |
44 |
private String deltaT; |
|
45 |
private long timeInterval; |
|
46 |
private Date firstTime; |
|
47 |
private boolean timedReplicationIsOn = false; |
|
45 | 48 |
Timer replicationDaemon; |
46 | 49 |
private static MetaCatUtil util = new MetaCatUtil(); |
47 | 50 |
private Vector fileLocks = new Vector(); |
48 | 51 |
private Thread lockThread = null; |
49 | 52 |
private static final AbstractDatabase dbAdapter = MetaCatUtil.dbAdapter; |
50 | 53 |
public static final String FORCEREPLICATEDELETE = "forcereplicatedelete"; |
51 |
|
|
54 |
private static final String TIMEREPLICATION = "timedreplication"; |
|
55 |
private static final String TIMEREPLICATIONINTERVAl = "timedreplicationinterval"; |
|
56 |
private static final String FIRSTTIME = "firsttimedreplication"; |
|
57 |
private static final int TIMEINTERVALLIMIT = 7200000; |
|
58 |
|
|
52 | 59 |
/** |
53 | 60 |
* Initialize the servlet by creating appropriate database connections |
54 | 61 |
*/ |
... | ... | |
56 | 63 |
{ |
57 | 64 |
//initialize db connections to handle any update requests |
58 | 65 |
MetaCatUtil util = new MetaCatUtil(); |
59 |
deltaT = util.getOption("deltaT"); |
|
66 |
//deltaT = util.getOption("deltaT");
|
|
60 | 67 |
//the default deltaT can be set from metacat.properties |
61 | 68 |
//create a thread to do the delta-T check but don't execute it yet |
62 | 69 |
replicationDaemon = new Timer(true); |
63 |
/*java.net.URL.setURLStreamHandlerFactory( |
|
64 |
new java.net.URLStreamHandlerFactory() |
|
70 |
try |
|
71 |
{ |
|
72 |
timedReplicationIsOn = (new Boolean(util.getOption(TIMEREPLICATION ).trim())).booleanValue(); |
|
73 |
MetaCatUtil.debugMessage("The timed replication on is"+timedReplicationIsOn, 30); |
|
74 |
timeInterval = (new Long(util.getOption(TIMEREPLICATIONINTERVAl).trim())).longValue(); |
|
75 |
MetaCatUtil.debugMessage("The timed replication time Inerval is "+ timeInterval, 20); |
|
76 |
String firstTimeStr = util.getOption(FIRSTTIME); |
|
77 |
MetaCatUtil.debugMessage("first replication time form property is "+firstTimeStr, 20); |
|
78 |
firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr); |
|
79 |
MetaCatUtil.debugMessage("After combine current time, the real first time is " |
|
80 |
+firstTime.toString()+" minisec", 20); |
|
81 |
// set up time replication if it is on |
|
82 |
if (timedReplicationIsOn) |
|
65 | 83 |
{ |
66 |
public java.net.URLStreamHandler createURLStreamHandler( |
|
67 |
final String protocol) |
|
68 |
{ |
|
69 |
if ("http".equals(protocol)) |
|
70 |
{ |
|
71 |
try |
|
72 |
{ |
|
73 |
URLStreamHandler urlsh = new HTTPClient.http.Handler(); |
|
74 |
MetaCatUtil.debugMessage("Using HttpClient Protocol Handler for http", 45); |
|
75 |
return urlsh; |
|
76 |
} |
|
77 |
catch (Exception e) |
|
78 |
{ |
|
79 |
MetaCatUtil.debugMessage( |
|
80 |
"Error setting URL StreamHandler!", 30); |
|
81 |
return null; |
|
82 |
} |
|
83 |
}//if |
|
84 |
else if ("https".equals(protocol)) |
|
85 |
{ |
|
86 |
try |
|
87 |
{ |
|
88 |
URLStreamHandler urlsh = new HTTPClient.https.Handler(); |
|
89 |
MetaCatUtil.debugMessage("Using HttpClient Protocol Handler for https", 45); |
|
90 |
return urlsh; |
|
91 |
} |
|
92 |
catch (Exception e) |
|
93 |
{ |
|
94 |
MetaCatUtil.debugMessage( |
|
95 |
"Error setting URL StreamHandler!", 30); |
|
96 |
return null; |
|
97 |
} |
|
98 |
} |
|
99 |
return null; |
|
100 |
}//createURLStreamHandler |
|
101 |
});*/ |
|
102 |
|
|
84 |
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime, timeInterval); |
|
85 |
MetacatReplication.replLog("deltaT handler started with rate=" + |
|
86 |
timeInterval + " mini seconds at " +firstTime.toString()); |
|
87 |
} |
|
88 |
} |
|
89 |
catch (Exception e) |
|
90 |
{ |
|
91 |
// the timed replication in Metacat.properties file has problem |
|
92 |
// so timed replication is setting to false; |
|
93 |
MetaCatUtil.debugMessage("Couldn't set up timed replication "+ |
|
94 |
" in Metacat replication servlet init because " + |
|
95 |
e.getMessage(), 20); |
|
96 |
MetacatReplication.replErrorLog("Couldn't set up timed replication "+ |
|
97 |
" in Metacat replication servlet init because " + |
|
98 |
e.getMessage()); |
|
99 |
timedReplicationIsOn = false; |
|
100 |
} |
|
101 |
|
|
103 | 102 |
} |
104 | 103 |
|
105 | 104 |
public void destroy() |
106 | 105 |
{ |
107 | 106 |
replicationDaemon.cancel(); |
108 |
// System.out.println("Replication daemon cancelled."); |
|
107 |
|
|
109 | 108 |
} |
110 | 109 |
|
111 | 110 |
public void doGet (HttpServletRequest request, HttpServletResponse response) |
... | ... | |
190 | 189 |
//stop the replication server |
191 | 190 |
replicationDaemon.cancel(); |
192 | 191 |
replicationDaemon = new Timer(true); |
192 |
timedReplicationIsOn = false; |
|
193 |
MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString()); |
|
193 | 194 |
out.println("Replication Handler Stopped"); |
194 | 195 |
MetacatReplication.replLog("deltaT handler stopped"); |
195 | 196 |
|
196 | 197 |
|
197 | 198 |
} else if ( action.equals("start") ) { |
198 |
|
|
199 |
String firstTimeStr = ""; |
|
199 | 200 |
//start the replication server |
200 |
int rate;
|
|
201 |
if ( params.containsKey("rate") ) {
|
|
202 |
rate = new Integer(
|
|
203 |
new String(((String[])params.get("rate"))[0])).intValue();
|
|
204 |
if(rate < 30) {
|
|
205 |
out.println("Replication deltaT rate cannot be less than 30!");
|
|
201 |
if ( params.containsKey("rate") ) {
|
|
202 |
timeInterval = new Long(
|
|
203 |
new String(((String[])params.get("rate"))[0])).longValue();
|
|
204 |
if(timeInterval < TIMEINTERVALLIMIT) {
|
|
205 |
out.println("Replication deltaT rate cannot be less than "+
|
|
206 |
TIMEINTERVALLIMIT + " millisecs and system automatically setup the rate to "+TIMEINTERVALLIMIT);
|
|
206 | 207 |
//deltaT<30 is a timing mess! |
207 |
rate = 1000;
|
|
208 |
timeInterval = TIMEINTERVALLIMIT;
|
|
208 | 209 |
} |
209 | 210 |
} else { |
210 |
rate = 1000;
|
|
211 |
timeInterval = TIMEINTERVALLIMIT ;
|
|
211 | 212 |
} |
212 |
out.println("New rate is: " + rate + " seconds."); |
|
213 |
MetaCatUtil.debugMessage("New rate is: " + timeInterval + " mini seconds.", 30); |
|
214 |
if ( params.containsKey("firsttime")) |
|
215 |
{ |
|
216 |
firstTimeStr = ((String[])params.get("firsttime"))[0]; |
|
217 |
try |
|
218 |
{ |
|
219 |
firstTime = ReplicationHandler.combinateCurrentDateAndGivenTime(firstTimeStr); |
|
220 |
MetaCatUtil.debugMessage("The first time setting is "+firstTime.toString(), 30); |
|
221 |
} |
|
222 |
catch (Exception e) |
|
223 |
{ |
|
224 |
throw new ServletException(e.getMessage()); |
|
225 |
} |
|
226 |
MetaCatUtil.debugMessage("After combine current time, the real first time is " |
|
227 |
+firstTime.toString()+" minisec", 20); |
|
228 |
} |
|
229 |
else |
|
230 |
{ |
|
231 |
MetacatReplication.replErrorLog("You should specify the first time " + |
|
232 |
"to start a time replication"); |
|
233 |
MetaCatUtil.debugMessage("You should specify the first time " + |
|
234 |
"to start a time replication", 30); |
|
235 |
return; |
|
236 |
} |
|
237 |
|
|
238 |
timedReplicationIsOn = true; |
|
239 |
// save settings to property file |
|
240 |
MetaCatUtil.setOption(TIMEREPLICATION, (new Boolean(timedReplicationIsOn)).toString()); |
|
241 |
// note we couldn't use firstTime object because it has date info |
|
242 |
// we only need time info such as 10:00 PM |
|
243 |
MetaCatUtil.setOption(FIRSTTIME, firstTimeStr); |
|
244 |
MetaCatUtil.setOption(TIMEREPLICATIONINTERVAl, (new Long(timeInterval)).toString()); |
|
213 | 245 |
replicationDaemon.cancel(); |
214 | 246 |
replicationDaemon = new Timer(true); |
215 |
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(out), 0,
|
|
216 |
rate * 1000);
|
|
247 |
replicationDaemon.scheduleAtFixedRate(new ReplicationHandler(), firstTime,
|
|
248 |
timeInterval);
|
|
217 | 249 |
out.println("Replication Handler Started"); |
218 | 250 |
MetacatReplication.replLog("deltaT handler started with rate=" + |
219 |
rate + " seconds");
|
|
251 |
timeInterval + " milliseconds at " +firstTime.toString());
|
|
220 | 252 |
|
221 | 253 |
|
222 | 254 |
} else if ( action.equals("getall") ) { |
223 | 255 |
//updates this server exactly once |
224 |
replicationDaemon.schedule(new ReplicationHandler(out), 0);
|
|
256 |
replicationDaemon.schedule(new ReplicationHandler(), 0); |
|
225 | 257 |
response.setContentType("text/html"); |
226 | 258 |
out.println("<html><body>\"Get All\" Done</body></html>"); |
227 | 259 |
|
Also available in: Unified diff
Persistant timed replication parameter.