diff options
Diffstat (limited to 'src/main/Main.java')
-rw-r--r-- | src/main/Main.java | 76 |
1 files changed, 67 insertions, 9 deletions
diff --git a/src/main/Main.java b/src/main/Main.java index 809d6b0..e076d32 100644 --- a/src/main/Main.java +++ b/src/main/Main.java @@ -10,13 +10,19 @@ import io.TweetReader; import java.io.IOException; import java.sql.Connection; import java.sql.SQLException; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; /** * The main class. */ -public class Main { +public class Main implements Callable<Boolean> { /** * The main method of the application. @@ -32,7 +38,37 @@ public class Main { System.exit(1); return; } - main.run(); + + ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); + // the main IO thread + Future<Boolean> mainTask = scheduler.submit(main); + + // the status thread + if (main.statusInterval > 0) { + scheduler.scheduleAtFixedRate(new Watcher(main, main.statusInterval), + main.statusInterval, main.statusInterval, TimeUnit.SECONDS); + } + // now wait for completion of the main thread + try { + while (true) { + try { + if (mainTask.get()) { + System.out.println("Import successful."); + } else { + System.out.println("Import (partially) failed."); + } + break; + } catch (InterruptedException ex) { + Logger.getLogger(Main.class.getName()) + .info("Interrupted while waiting for main"); + } + } + } catch (ExecutionException ex) { + Logger.getLogger(Main.class.getName()) + .log(Level.SEVERE, "Main failed", ex); + } + // cancel status thread + scheduler.shutdown(); } private String m_filename; @@ -41,6 +77,7 @@ public class Main { * Whether the database should be contacted or not. */ private boolean skipDb; + private Integer statusInterval; public Main(String[] args) { // default connection properties @@ -50,22 +87,29 @@ public class Main { .setPassword("2IOC02") .setDbName("twitter"); skipDb = false; + statusInterval = 2; /* parse the global options. */ parseGlobalOptions(args); } - private void printTweets(ITweetReader reader) throws IOException { + /** + * The current tweet number that is being processed. + */ + private volatile int tweetNo; + + private boolean printTweets(ITweetReader reader) throws IOException { Tweet tweet; - long tweetNo = 1; + tweetNo = 1; while ((tweet = reader.getTweet()) != null) { System.out.println("/*" + tweetNo++ + "*/ " + tweet); } + return true; } - private void tweetsToDb(ITweetReader reader) throws IOException { + private boolean tweetsToDb(ITweetReader reader) throws IOException { Tweet tweet = null; - int tweetNo = 1; + tweetNo = 1; try (Connection connection = cb.create()) { /* create the object that fills the database */ DataFiller filler = new DataFiller(connection); @@ -73,6 +117,7 @@ public class Main { filler.processTweet(tweet); ++tweetNo; } + return true; } catch (JsonParseException ex) { if (tweet != null) { System.err.println("Faulty tweet " + tweetNo + ": " + tweet); @@ -86,10 +131,17 @@ public class Main { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "DB error", ex); } + return false; } - public void run() { + public int getProcessedTweets() { + return tweetNo; + } + + @Override + public Boolean call() { ITweetReader reader = null; + boolean success = false; try { if (m_filename == null) { reader = new TweetReader(System.in); @@ -97,9 +149,9 @@ public class Main { reader = new FileTweetReader(m_filename); } if (skipDb) { - printTweets(reader); + success = printTweets(reader); } else { - tweetsToDb(reader); + success = tweetsToDb(reader); } } catch (JsonSyntaxException ex) { System.err.println("Got an invalid tweet: " + ex); @@ -110,6 +162,8 @@ public class Main { reader.close(); } } + + return success; } private void parseGlobalOptions(String[] args) @@ -131,6 +185,8 @@ public class Main { cb.setDbName(getArg(args, ++i, "--dbname")); } else if ("--skipdb".equals(args[i])) { skipDb = true; + } else if ("--status".equals(args[i])) { + statusInterval = Integer.valueOf(getArg(args, ++i, "--status")); } else if (args[i].startsWith("-")) { throw new IllegalArgumentException("Invalid option: " + args[i]); } else { @@ -171,6 +227,8 @@ public class Main { " --dbport PORT Database port (defaults to 5432)", " --dbname NAME Database name (defaults to 'Twitter')", " --skipdb Do not contact the database at all, just print data.", + " --status SECS The interval in which import status information", + " should be printed, zero disables it (defaults to 2)", "", "If no tweets file is given, data will be read from standard input." }; |