/[projects]/dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/LookupMain.java
ViewVC logotype

Contents of /dao/FuldDaekningWorker/src/dk/daoas/fulddaekning/LookupMain.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2259 - (show annotations) (download)
Mon Feb 9 13:21:39 2015 UTC (9 years, 3 months ago) by torben
File size: 5414 byte(s)
use a threadPool instead of launching 100's of threads
1 package dk.daoas.fulddaekning;
2
3 import java.io.File;
4 import java.io.FileReader;
5 import java.util.List;
6 import java.util.logging.FileHandler;
7 import java.util.logging.Logger;
8 import java.util.logging.SimpleFormatter;
9 import java.util.concurrent.*;
10
11 public class LookupMain {
12
13
14 static final String CONFIG_FILENAME = "fulddaekning.properties";
15
16 static boolean rename_tables;
17 static SafeProperties conf;
18
19 static int max_workers;
20 static boolean verbose;
21
22 static String distributor;
23
24 final static Logger logger = Logger.getLogger( LookupMain.class.toString() );
25
26
27 static Statistik flestDaekkede = new Statistik();
28 static Statistik flestIkkeDaekkede = new Statistik();
29 static Statistik mestBrugteTid = new Statistik();
30 static Statistik stoersteDataset = new Statistik();
31
32 static Adresse[] alleDaekkedeAdresser;
33
34 static ThreadPoolExecutor threadPool;
35
36 private static void setupThreadPool() {
37 threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(max_workers, new ThreadFactory() {
38 @Override
39 public Thread newThread(Runnable r) {
40 return new Thread(r, "lookupWorker");
41 }
42 });
43 }
44
45
46 public static void main(String[] args) throws Exception {
47
48 //Setup j.u.l Logger
49 Logger root = Logger.getLogger("");
50 FileHandler fhandler = new FileHandler("fulddaekning.log"); // Ingen max størrelse, ingen rotation og ingen append
51 fhandler.setFormatter( new SimpleFormatter() );
52 root.addHandler( fhandler );
53
54
55
56
57 File confFile = new File( CONFIG_FILENAME );
58 if (! confFile.exists() ) {
59 logger.warning("Config file not found: " + CONFIG_FILENAME);
60 System.exit(1);
61 }
62
63 conf = new SafeProperties();
64 conf.load( new FileReader(confFile) );
65
66 max_workers = Integer.parseInt( conf.getSafeProperty("MAX_WORKERS") );
67 if (max_workers <= 0) {
68 logger.info("!!! AUTO-DETECT MAX_WORKERS !!!");
69 int cores = Runtime.getRuntime().availableProcessors();
70 cores -= 1;//Efterlad 1 core/cpu i reserve til systemet
71
72 max_workers = Math.max(1, cores); //Dog skal der som minimum være 1 core til beregning
73
74 }
75 logger.info("Starting with MAX_WORKERS:" + max_workers);
76 setupThreadPool();
77
78
79 verbose = Boolean.parseBoolean( conf.getSafeProperty("VERBOSE") );
80 logger.info("Starting with VERBOSE:" + verbose);
81
82 rename_tables = Boolean.parseBoolean( conf.getSafeProperty("RENAMETABLES") );
83 logger.info("Starting with RENAMETABLES:" + rename_tables);
84
85 distributor = conf.getSafeProperty("DISTRIBUTOR");
86 distributor = distributor.toUpperCase();
87 logger.info("Starting for DISTRIBUTOR:" + distributor);
88
89 Constants.init(distributor);
90 Constants consts = Constants.getInstance();
91
92 Database db = new Database(conf);
93
94
95
96
97 boolean testRun= false;
98
99
100 long start = System.currentTimeMillis();
101
102 if (testRun == false) {
103
104 logger.info("Finder postnumre");
105 List<String> postnumre = db.hentPostnumre();
106
107 // Først validerer vi BBox på alle postnummre, for at undgå fuldt stop midt i beregningen
108 for(String postnr : postnumre) { //
109 logger.info("Validerer BBox for " + postnr);
110 BoundingBox bbox = db.getBoundingbox(postnr);
111 bbox.validateBbox();
112 }
113
114 if (consts.doExtendedLookup()) {
115 logger.info("Henter alle daekkede adresser");
116 alleDaekkedeAdresser = db.hentAlleDaekkedeAdresser();
117 logger.info( "AlleDaekkedeAdresser.length=" + alleDaekkedeAdresser.length);
118 }
119
120
121 //pre-check er ok - reset tmp tabel og start søgningen
122 db.resetResultTable();
123
124 for(String postnr : postnumre) {
125 Lookup lookup = new Lookup(postnr, db, threadPool);
126 lookup.doLookup();
127 }
128
129 if (rename_tables) {
130 db.renameResultTables();
131 } else {
132 logger.info( "Rename tables is disabled !!!" );
133 }
134
135
136
137 } else {
138 /// Test
139 db.resetResultTable();
140
141 if (consts.doExtendedLookup()) {
142 alleDaekkedeAdresser = db.hentAlleDaekkedeAdresser();
143 logger.info( "AlleDaekkedeAdresser.length=" + alleDaekkedeAdresser.length);
144 }
145
146 Lookup lookup = new Lookup("458x", db, threadPool);
147 lookup.doLookup();
148 }
149
150 long now = System.currentTimeMillis();
151 long elapsed = now - start ;
152
153 logger.info("Mest brugte tid: " + mestBrugteTid);
154 logger.info("Flest Ikke-dækkede, " + flestIkkeDaekkede);
155 logger.info("Flest Dækkede, " + flestDaekkede);
156 logger.info("Største Dataset, " + stoersteDataset);
157 logger.info("Fuld load done : " + formatMilliSeconds(elapsed) );
158 }
159
160
161 public static void saveStatistics(Statistik stat) {
162 if (stat.antalDaekkede > flestDaekkede.antalDaekkede) {
163 flestDaekkede = stat;
164 }
165 if (stat.antalIkkeDaekkede > flestIkkeDaekkede.antalIkkeDaekkede) {
166 flestIkkeDaekkede = stat;
167 }
168
169 if (stat.totalDataset > mestBrugteTid.totalDataset) {
170 stoersteDataset = stat;
171 }
172
173 if (stat.forbrugtTid > mestBrugteTid.forbrugtTid) {
174 mestBrugteTid = stat;
175 }
176 }
177
178 static String formatMilliSeconds(long milliseconds) {
179 int mseconds = (int) milliseconds % 1000;
180 int seconds = (int) (milliseconds / 1000) % 60 ;
181 int minutes = (int) ((milliseconds / (1000*60)) % 60);
182 int hours = (int) ((milliseconds / (1000*60*60)) % 24);
183
184 return String.format("%02d:%02d:%02d.%03d", hours, minutes, seconds, mseconds);
185 }
186 }

  ViewVC Help
Powered by ViewVC 1.1.20