1 | package gov.hhs.fha.nhinc.gateway.aggregator.persistence;
|
---|
2 |
|
---|
3 | import java.util.Date;
|
---|
4 |
|
---|
5 | import org.apache.commons.logging.Log;
|
---|
6 | import org.apache.commons.logging.LogFactory;
|
---|
7 |
|
---|
8 | import gov.hhs.fha.nhinc.properties.PropertyAccessor;
|
---|
9 | import java.util.Calendar;
|
---|
10 |
|
---|
11 | /**
|
---|
12 | * This class is used to manage the garbage collection on the AGGREGATOR
|
---|
13 | * database tables. It will get run each time a startTransaction is called,
|
---|
14 | * and it will use settings in the gateway.properties file to determine
|
---|
15 | * how often to kick off the garbage collection thread and how hold a
|
---|
16 | * transaction must be to be considered stale.
|
---|
17 | *
|
---|
18 | * @author Les Westberg
|
---|
19 | */
|
---|
20 | public class GarbageCollectorMgr
|
---|
21 | {
|
---|
22 | private static Log log = LogFactory.getLog(GarbageCollectorMgr.class);
|
---|
23 | private static Date dtLastRun = new Date(); // The date that the garbage collector was last run.
|
---|
24 | private static final String GATEWAY_PROPERTY_FILE = "gateway";
|
---|
25 | private static final String GARBAGE_COLLECT_TIME_DURATION = "aggregatorGarbageCollectionTimeDuration";
|
---|
26 | private static final String GARBAGE_COLLECT_STALE_DURATION = "aggregatorGarbageCollectionStaleDuration";
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Default constructor.
|
---|
30 | */
|
---|
31 | public GarbageCollectorMgr()
|
---|
32 | {
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * This method checks to see if we should run the collector based on the
|
---|
37 | * time settings in the gateway.properties file. If we should run it, then
|
---|
38 | * true is returnd. Otherwise false is returned.
|
---|
39 | *
|
---|
40 | * @return TRUE if we should run the garbage collector.
|
---|
41 | */
|
---|
42 | private static boolean bRunCollector()
|
---|
43 | {
|
---|
44 | String sTimeDuration = "";
|
---|
45 | int iTimeDuration = 0;
|
---|
46 | try
|
---|
47 | {
|
---|
48 | sTimeDuration = PropertyAccessor.getProperty(GATEWAY_PROPERTY_FILE, GARBAGE_COLLECT_TIME_DURATION);
|
---|
49 | iTimeDuration = Integer.parseInt(sTimeDuration);
|
---|
50 | }
|
---|
51 | catch (Exception e)
|
---|
52 | {
|
---|
53 | String sErrorMessage = "Failed to read and parse property: " + GARBAGE_COLLECT_TIME_DURATION +
|
---|
54 | " from PropertyFile: " + GATEWAY_PROPERTY_FILE + ".propertues. No " +
|
---|
55 | "garbage collection will be done on the aggregator tables.";
|
---|
56 | log.error(sErrorMessage);
|
---|
57 | return false;
|
---|
58 | }
|
---|
59 |
|
---|
60 | if ((sTimeDuration == null) ||
|
---|
61 | (sTimeDuration.length() <= 0))
|
---|
62 | {
|
---|
63 | String sErrorMessage = "Failed to read and parse property: " + GARBAGE_COLLECT_TIME_DURATION +
|
---|
64 | " from PropertyFile: " + GATEWAY_PROPERTY_FILE + ".propertues. No " +
|
---|
65 | "garbage collection will be done on the aggregator tables.";
|
---|
66 | log.error(sErrorMessage);
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 |
|
---|
70 | Calendar oCal = Calendar.getInstance();
|
---|
71 | oCal.add(Calendar.SECOND, (-1) * iTimeDuration);
|
---|
72 |
|
---|
73 | if (oCal.getTime().getTime() > dtLastRun.getTime())
|
---|
74 | {
|
---|
75 | return true;
|
---|
76 | }
|
---|
77 |
|
---|
78 | return false;
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * This method checks to see if it is time to run the garbage collection
|
---|
83 | * thread based on the settings in the gateway.properties file. If it is, then
|
---|
84 | * it spins off a thread to do garbage collection of the aggregator tables.
|
---|
85 | */
|
---|
86 | public static void runGarbageCollection()
|
---|
87 | {
|
---|
88 | // see if we need to run the collector...
|
---|
89 | //----------------------------------------
|
---|
90 | if (bRunCollector())
|
---|
91 | {
|
---|
92 | String sStaleDuration = "";
|
---|
93 | int iStaleDuration = 0;
|
---|
94 |
|
---|
95 | try
|
---|
96 | {
|
---|
97 | sStaleDuration = PropertyAccessor.getProperty(GATEWAY_PROPERTY_FILE, GARBAGE_COLLECT_STALE_DURATION);
|
---|
98 | iStaleDuration = Integer.parseInt(sStaleDuration);
|
---|
99 | }
|
---|
100 | catch (Exception e)
|
---|
101 | {
|
---|
102 | String sErrorMessage = "Failed to read and parse property: " + GARBAGE_COLLECT_STALE_DURATION +
|
---|
103 | " from PropertyFile: " + GATEWAY_PROPERTY_FILE + ".propertues. No " +
|
---|
104 | "garbage collection will be done on the aggregator tables.";
|
---|
105 | log.error(sErrorMessage);
|
---|
106 | return;
|
---|
107 | }
|
---|
108 |
|
---|
109 | if ((sStaleDuration == null) ||
|
---|
110 | (sStaleDuration.length() <= 0))
|
---|
111 | {
|
---|
112 | String sErrorMessage = "Failed to read and parse property: " + GARBAGE_COLLECT_STALE_DURATION +
|
---|
113 | " from PropertyFile: " + GATEWAY_PROPERTY_FILE + ".propertues. No " +
|
---|
114 | "garbage collection will be done on the aggregator tables.";
|
---|
115 | log.error(sErrorMessage);
|
---|
116 | return;
|
---|
117 | }
|
---|
118 |
|
---|
119 | Calendar oCal = Calendar.getInstance();
|
---|
120 | oCal.add(Calendar.SECOND, (-1) * iStaleDuration);
|
---|
121 |
|
---|
122 | log.debug("Running aggregator garbage collection thread now.");
|
---|
123 |
|
---|
124 | GarbageCollectorThread oCollectorThread = new GarbageCollectorThread(oCal.getTime());
|
---|
125 | oCollectorThread.run();
|
---|
126 | dtLastRun = new Date();
|
---|
127 |
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | }
|
---|