Last active
October 6, 2020 18:01
-
-
Save mananai/7ec403b8e2235ecd8f2870eec62418e4 to your computer and use it in GitHub Desktop.
Friend finder main class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package twitter.friends; | |
import java.io.IOException; | |
import java.sql.SQLException; | |
import java.util.List; | |
import java.util.concurrent.BlockingQueue; | |
import java.util.concurrent.LinkedBlockingQueue; | |
import db.DBResources; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterFactory; | |
import utils.FileUtils; | |
public class FriendFinder { | |
private Twitter twitter; | |
public FriendFinder() throws SQLException { | |
this.twitter = new TwitterFactory().getInstance(); | |
} | |
public void find(List<Long> userIds) throws InterruptedException, SQLException { | |
BlockingQueue<Long> friendsLookupQueue = new LinkedBlockingQueue<>(); | |
BlockingQueue<Long> usersLookupQueue = new LinkedBlockingQueue<>(); | |
friendsLookupQueue.addAll(userIds); | |
Thread friendsLookupThread = new Thread(new FriendsLookupRunnable(twitter, | |
friendsLookupQueue, usersLookupQueue)); | |
Thread usersLookupThread = new Thread(new UsersLookupRunnable(twitter, usersLookupQueue)); | |
friendsLookupThread.start(); | |
usersLookupThread.start(); | |
Runtime.getRuntime().addShutdownHook(new Thread(()-> | |
{ | |
friendsLookupThread.interrupt(); | |
usersLookupThread.interrupt(); | |
DBResources.getInstance().close(); | |
})); | |
friendsLookupThread.join(); | |
usersLookupThread.join(); | |
DBResources.getInstance().close(); | |
} | |
public static void main(String[] args) throws SQLException, InterruptedException, IOException { | |
if (args.length < 2) { | |
throw new IllegalArgumentException(); | |
} | |
String jdbcURL = args[0]; | |
String fileName = args[1]; | |
String sql = FileUtils.readFile(fileName); | |
System.out.printf("PID is %d\n", ProcessHandle.current().pid()); | |
DBResources.newInstance(jdbcURL); | |
DBResources.getInstance().getConnection().setAutoCommit(false); | |
List<Long> userIds = FriendDAO.getUserIds(sql); | |
FriendFinder friendFinder= new FriendFinder(); | |
friendFinder.find(userIds); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment