Revision 2062
Added by Duane Costa almost 21 years ago
src/edu/ucsb/nceas/metacat/harvesterClient/Harvester.java | ||
---|---|---|
36 | 36 |
/* |
37 | 37 |
* Class fields |
38 | 38 |
*/ |
39 |
private static final String marker = |
|
40 |
"*****************************************************************************"; |
|
39 | 41 |
|
40 | 42 |
|
41 | 43 |
/* |
... | ... | |
51 | 53 |
|
52 | 54 |
|
53 | 55 |
/** |
54 |
* Determines whether Harvester is running on a Win32 platform. Used |
|
55 |
* during development. |
|
56 |
* |
|
57 |
* @return true if this in Win32, false otherwise |
|
56 |
* Loads Harvester properties from a configuration file. |
|
58 | 57 |
*/ |
59 |
public static boolean isWin32 () { |
|
60 |
boolean isWin32; |
|
61 |
String osName = System.getProperty("os.name"); |
|
58 |
private static Properties loadProperties() { |
|
59 |
File configfile = new File("../../lib/harvester", "harvester.properties"); |
|
60 |
Properties properties = new Properties(); |
|
61 |
|
|
62 |
try { |
|
63 |
properties.load(new FileInputStream(configfile)); |
|
64 |
} |
|
65 |
catch (IOException e) { |
|
66 |
System.out.println("IOException: " + e.getMessage()); |
|
67 |
System.exit(1); |
|
68 |
} |
|
62 | 69 |
|
63 |
isWin32 = (osName.startsWith("Windows")); |
|
64 |
return isWin32; |
|
70 |
return properties; |
|
65 | 71 |
} |
66 | 72 |
|
67 |
|
|
73 |
|
|
68 | 74 |
/** |
69 | 75 |
* Harvester main method. |
70 | 76 |
* |
... | ... | |
74 | 80 |
* @throws ParserConfigurationException |
75 | 81 |
*/ |
76 | 82 |
public static void main(String[] args) { |
77 |
Harvester harvester = new Harvester(); |
|
83 |
Integer delayDefault = new Integer(0); // Default number of hours delay |
|
84 |
int delay = delayDefault.intValue(); // Delay in hours before first harvest |
|
85 |
Integer d; // Used for determining delay |
|
86 |
long delta; // endTime - startTime |
|
87 |
long endTime; // time that a harvest completes |
|
88 |
Harvester harvester; // object for a single harvest run |
|
89 |
Integer maxHarvestsDefault = new Integer(30); // Default max harvests |
|
90 |
int maxHarvests = maxHarvestsDefault.intValue(); // Max number of harvests |
|
91 |
Integer mh; // used in determining max harvests |
|
92 |
int nHarvests = 0; // counts the number of harvest runs |
|
93 |
final long oneHour = (60 * 60 * 1000); |
|
94 |
final long oneSecond = 1000; |
|
95 |
Integer periodDefault = new Integer(24); // Default hours between harvests |
|
96 |
int period = periodDefault.intValue(); // Hours between harvests |
|
97 |
Integer p; // Used in determining the period |
|
98 |
Properties properties; |
|
99 |
long startTime; // time that a harvest starts |
|
100 |
|
|
101 |
System.out.println(marker); |
|
102 |
System.out.println("Starting Harvester"); |
|
103 |
properties = Harvester.loadProperties(); |
|
104 |
properties.list(System.out); |
|
105 |
|
|
106 |
// Parse the delay property. Use default if necessary. |
|
107 |
try { |
|
108 |
d = Integer.valueOf(properties.getProperty( |
|
109 |
"delay", |
|
110 |
delayDefault.toString() |
|
111 |
)); |
|
112 |
delay = d.intValue(); |
|
113 |
} |
|
114 |
catch (NumberFormatException e) { |
|
115 |
System.out.println("NumberFormatException: Error parsing delay: " + |
|
116 |
e.getMessage()); |
|
117 |
System.out.println("Defaulting to delay=" + delayDefault); |
|
118 |
delay = delayDefault.intValue(); |
|
119 |
} |
|
120 |
|
|
121 |
// Parse the maxHarvests property. Use default if necessary. |
|
122 |
try { |
|
123 |
mh = Integer.valueOf(properties.getProperty( |
|
124 |
"maxHarvests", |
|
125 |
maxHarvestsDefault.toString() |
|
126 |
)); |
|
127 |
maxHarvests = mh.intValue(); |
|
128 |
} |
|
129 |
catch (NumberFormatException e) { |
|
130 |
System.out.println("NumberFormatException: Error parsing maxHarvests: " + |
|
131 |
e.getMessage()); |
|
132 |
System.out.println("Defaulting to maxHarvests=" + maxHarvestsDefault); |
|
133 |
maxHarvests = maxHarvestsDefault.intValue(); |
|
134 |
} |
|
135 |
|
|
136 |
// Parse the period property. Use default if necessary. |
|
137 |
try { |
|
138 |
p = Integer.valueOf(properties.getProperty("period", |
|
139 |
periodDefault.toString() |
|
140 |
)); |
|
141 |
period = p.intValue(); |
|
142 |
} |
|
143 |
catch (NumberFormatException e) { |
|
144 |
System.out.println("NumberFormatException: Error parsing period: " + |
|
145 |
e.getMessage()); |
|
146 |
System.out.println("Defaulting to period=" + periodDefault); |
|
147 |
period = periodDefault.intValue(); |
|
148 |
} |
|
78 | 149 |
|
79 |
harvester.startup(); // Start up Harvester |
|
80 |
harvester.readHarvestSiteSchedule(); // Read the database table |
|
81 |
harvester.harvest(); // Harvest the documents |
|
82 |
harvester.shutdown(); // Shut down Harvester |
|
150 |
// Sleep for delay number of hours prior to starting first harvest |
|
151 |
if (delay > 0) { |
|
152 |
try { |
|
153 |
System.out.print("First harvest will begin in " + delay); |
|
154 |
if (delay == 1) { |
|
155 |
System.out.println(" hour."); |
|
156 |
} |
|
157 |
else { |
|
158 |
System.out.println(" hours."); |
|
159 |
} |
|
160 |
//Thread.sleep(delay * oneHour); |
|
161 |
Thread.sleep(delay * oneSecond); |
|
162 |
} |
|
163 |
catch (InterruptedException e) { |
|
164 |
System.err.println("InterruptedException: " + e.getMessage()); |
|
165 |
System.exit(1); |
|
166 |
} |
|
167 |
} |
|
168 |
|
|
169 |
// Repeat a new harvest once every period number of hours, until we reach |
|
170 |
// the maximum number of harvests. Subtract delta from the time period so |
|
171 |
// that each harvest will start at a fixed interval. |
|
172 |
// |
|
173 |
while (nHarvests < maxHarvests) { |
|
174 |
nHarvests++; |
|
175 |
startTime = System.currentTimeMillis(); |
|
176 |
harvester = new Harvester(); // New object for this harvest |
|
177 |
harvester.startup(nHarvests, maxHarvests); // Start up Harvester |
|
178 |
harvester.readHarvestSiteSchedule(); // Read the database table |
|
179 |
harvester.harvest(); // Harvest the documents |
|
180 |
harvester.shutdown(); // Shut down Harvester |
|
181 |
endTime = System.currentTimeMillis(); |
|
182 |
delta = endTime - startTime; |
|
183 |
|
|
184 |
if (nHarvests < maxHarvests) { |
|
185 |
try { |
|
186 |
// Thread.sleep((period * oneHour) - delta); |
|
187 |
Thread.sleep((period * oneSecond) - delta); |
|
188 |
} |
|
189 |
catch (InterruptedException e) { |
|
190 |
System.err.println("InterruptedException: " + e.getMessage()); |
|
191 |
System.exit(1); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
83 | 195 |
} |
84 | 196 |
|
85 | 197 |
|
... | ... | |
90 | 202 |
/** Database connection */ |
91 | 203 |
Connection conn; |
92 | 204 |
|
93 |
/** Used during development to determine whether to connect to metacat */ |
|
205 |
/** Used during development to determine whether to connect to metacat |
|
206 |
* Sometimes it's useful to test parts of the code without actually |
|
207 |
* connecting to Metacat. |
|
208 |
*/ |
|
94 | 209 |
private boolean connectToMetacat; |
95 | 210 |
|
96 | 211 |
/** Highest DETAIL_LOG_ID primary key in the HARVEST_DETAIL_LOG table */ |
... | ... | |
120 | 235 |
/** Metacat client object */ |
121 | 236 |
Metacat metacat; |
122 | 237 |
|
123 |
/** Loads harvester properties from configuration file */ |
|
124 |
Properties properties; |
|
125 |
|
|
126 | 238 |
|
127 | 239 |
/* |
128 | 240 |
* Object methods |
129 | 241 |
*/ |
130 | 242 |
|
131 |
|
|
132 |
/* |
|
133 |
Harvester harvester, |
|
134 |
Date harvestDate, |
|
135 |
int status, |
|
136 |
String message, |
|
137 |
String harvestOperationCode, |
|
138 |
int siteScheduleID, |
|
139 |
HarvestDocument harvestDocument, |
|
140 |
String errorMessage |
|
141 |
*/ |
|
142 | 243 |
/** |
143 | 244 |
* Creates a new HarvestLog object and adds it to the harvestLogList. |
144 | 245 |
* |
... | ... | |
347 | 448 |
harvestLogID = getMaxValue("HARVEST_LOG", "HARVEST_LOG_ID") + 1; |
348 | 449 |
} |
349 | 450 |
|
350 |
|
|
451 |
|
|
351 | 452 |
/** |
352 |
* Loads Harvester properties from a configuration file. |
|
453 |
* Prints the site schedule data for a given site. |
|
454 |
* |
|
455 |
* @param siteScheduleID the primary key in the HARVEST_SITE_SCHEDULE table |
|
353 | 456 |
*/ |
354 |
private void loadProperties() { |
|
355 |
File configfile = new File("../../lib/harvester", "harvester.properties"); |
|
356 |
|
|
357 |
properties = new Properties(); |
|
358 |
|
|
359 |
try { |
|
360 |
properties.load(new FileInputStream(configfile)); |
|
361 |
properties.list(System.out); |
|
362 |
} |
|
363 |
catch (IOException e) { |
|
364 |
System.out.println("IOException: " + e.getMessage()); |
|
365 |
System.exit(1); |
|
366 |
} |
|
367 |
} |
|
368 |
|
|
369 |
|
|
370 | 457 |
void printHarvestSiteSchedule(int siteScheduleID) { |
371 | 458 |
HarvestSiteSchedule harvestSiteSchedule; |
372 | 459 |
|
... | ... | |
569 | 656 |
|
570 | 657 |
/** |
571 | 658 |
* Initializes Harvester at startup. Connects to the database and to Metacat. |
659 |
* |
|
660 |
* @param nHarvests the nth harvest |
|
661 |
* @param maxHarvests the maximum number of harvests that this process |
|
662 |
* can run |
|
572 | 663 |
*/ |
573 |
private void startup() { |
|
664 |
private void startup(int nHarvests, int maxHarvests) {
|
|
574 | 665 |
Boolean ctm; |
575 | 666 |
String dbDriver; |
576 | 667 |
Integer lp; |
577 | 668 |
String metacatURL; |
578 |
String osName = Harvester.isWin32() ? "Windows" : "Unix";
|
|
669 |
Date now;
|
|
579 | 670 |
String password; |
671 |
Properties properties; |
|
580 | 672 |
//String response; |
581 | 673 |
String sessionId; |
674 |
String timestamp; |
|
582 | 675 |
String url; |
583 | 676 |
String user; |
584 | 677 |
String userName = System.getProperty("user.name"); |
585 | 678 |
SQLWarning warn; |
586 | 679 |
|
587 | 680 |
// Log startup operation |
588 |
System.out.println("*****************************************************"); |
|
589 |
System.out.println("Starting Up Harvester"); |
|
590 |
loadProperties(); |
|
681 |
System.out.println(Harvester.marker); |
|
682 |
now = new Date(); |
|
683 |
timestamp = now.toString() + ": "; |
|
684 |
System.out.println(timestamp + "Starting Next Harvest (" + |
|
685 |
nHarvests + "/" + maxHarvests + ")"); |
|
686 |
properties = loadProperties(); |
|
591 | 687 |
ctm = Boolean.valueOf(properties.getProperty("connectToMetacat", "true")); |
592 | 688 |
connectToMetacat = ctm.booleanValue(); |
593 | 689 |
dbDriver = properties.getProperty("dbDriver"); |
594 | 690 |
harvesterAdministrator = properties.getProperty("harvesterAdministrator"); |
691 |
|
|
595 | 692 |
try { |
596 | 693 |
lp = Integer.valueOf(properties.getProperty("logPeriod", "90")); |
597 | 694 |
logPeriod = lp.intValue(); |
... | ... | |
602 | 699 |
System.err.println("Defaulting to logPeriod of 90 days"); |
603 | 700 |
logPeriod = 90; |
604 | 701 |
} |
702 |
|
|
605 | 703 |
metacatURL = properties.getProperty("metacatURL"); |
606 | 704 |
password = properties.getProperty("password"); |
607 | 705 |
url = properties.getProperty("url"); |
Also available in: Unified diff
Additional scheduling logic