@ -146,6 +146,11 @@ public class MediawikiImporter extends Thread implements Importer {
@Override
@Override
public void run ( ) {
public void run ( ) {
this . start = System . currentTimeMillis ( ) ;
this . start = System . currentTimeMillis ( ) ;
final int threads = Math . max ( 2 , Runtime . getRuntime ( ) . availableProcessors ( ) - 1 ) ;
// out keeps a outputfile open until poisened, to make sure underlaying thread gets the end condition
// regardless of any exception (e.g. eof memory) a add(poison) is added to the most outer final block
final BlockingQueue < wikiparserrecord > out = new ArrayBlockingQueue < wikiparserrecord > ( threads * 10 ) ;
final wikiparserrecord poison = newRecord ( ) ;
try {
try {
String targetstub = this . sourcefile . getName ( ) ;
String targetstub = this . sourcefile . getName ( ) ;
int p = targetstub . lastIndexOf ( "\\." ) ;
int p = targetstub . lastIndexOf ( "\\." ) ;
@ -161,10 +166,7 @@ public class MediawikiImporter extends Thread implements Importer {
StringBuilder sb = new StringBuilder ( ) ;
StringBuilder sb = new StringBuilder ( ) ;
boolean page = false , text = false ;
boolean page = false , text = false ;
String title = null ;
String title = null ;
final wikiparserrecord poison = newRecord ( ) ;
final int threads = Math . max ( 2 , Runtime . getRuntime ( ) . availableProcessors ( ) - 1 ) ;
final BlockingQueue < wikiparserrecord > in = new ArrayBlockingQueue < wikiparserrecord > ( threads * 10 ) ;
final BlockingQueue < wikiparserrecord > in = new ArrayBlockingQueue < wikiparserrecord > ( threads * 10 ) ;
final BlockingQueue < wikiparserrecord > out = new ArrayBlockingQueue < wikiparserrecord > ( threads * 10 ) ;
final ExecutorService service = Executors . newCachedThreadPool ( ) ;
final ExecutorService service = Executors . newCachedThreadPool ( ) ;
final convertConsumer [ ] consumers = new convertConsumer [ threads ] ;
final convertConsumer [ ] consumers = new convertConsumer [ threads ] ;
final Future < ? > [ ] consumerResults = ( Future < ? > [ ] ) Array . newInstance ( Future . class , threads ) ;
final Future < ? > [ ] consumerResults = ( Future < ? > [ ] ) Array . newInstance ( Future . class , threads ) ;
@ -261,8 +263,6 @@ public class MediawikiImporter extends Thread implements Importer {
for ( int i = 0 ; i < threads ; i + + ) {
for ( int i = 0 ; i < threads ; i + + ) {
consumerResults [ i ] . get ( 10000 , TimeUnit . MILLISECONDS ) ;
consumerResults [ i ] . get ( 10000 , TimeUnit . MILLISECONDS ) ;
}
}
out . put ( poison ) ;
writerResult . get ( 10000 , TimeUnit . MILLISECONDS ) ;
} catch ( final InterruptedException e ) {
} catch ( final InterruptedException e ) {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} catch ( final ExecutionException e ) {
} catch ( final ExecutionException e ) {
@ -271,11 +271,18 @@ public class MediawikiImporter extends Thread implements Importer {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} catch ( final Exception e ) {
} catch ( final Exception e ) {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} finally {
out . put ( poison ) ; // output thread condition (for file.close)
writerResult . get ( 10000 , TimeUnit . MILLISECONDS ) ;
}
}
} catch ( final IOException e ) {
} catch ( final IOException e ) {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} catch ( final Exception e ) {
} catch ( final Exception e ) {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} finally {
try {
out . put ( poison ) ; // out keeps output file open until poisened, to close file if exception happend in this block
} catch ( InterruptedException ex ) { }
}
}
}
}
@ -733,10 +740,12 @@ public class MediawikiImporter extends Thread implements Importer {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
} finally {
} finally {
try {
try {
this . osw . write ( SurrogateReader . SURROGATES_MAIN_ELEMENT_CLOSE + "\n" ) ;
if ( osw ! = null ) { // maybe null on poison (immediately)
this . osw . close ( ) ;
this . osw . write ( SurrogateReader . SURROGATES_MAIN_ELEMENT_CLOSE + "\n" ) ;
final String finalfilename = this . targetstub + "." + this . fc + ".xml" ;
this . osw . close ( ) ;
new File ( this . targetdir , this . outputfilename ) . renameTo ( new File ( this . targetdir , finalfilename ) ) ;
final String finalfilename = this . targetstub + "." + this . fc + ".xml" ;
new File ( this . targetdir , this . outputfilename ) . renameTo ( new File ( this . targetdir , finalfilename ) ) ;
}
} catch ( final IOException e ) {
} catch ( final IOException e ) {
ConcurrentLog . logException ( e ) ;
ConcurrentLog . logException ( e ) ;
}
}