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

  ViewVC Help
Powered by ViewVC 1.1.20