I have a SwingWorker which initiates a LinkedBlockingQueue, passes it to another method (PortalDriver, below), then reads from it in the doInBackground() method. The LinkedBlockingQueue holds futures for a custom object (and it is definitely filled correctly). As a check, the objects that I am creating (via an ExecutorService), have a println(this) at the end of the constructor, so I know when they're being created.
The issue is, via the println() calls (both in doInBackground and in the constructor), after a number of successful iterations in the while loop in doInBackground, the line
System.out.println("future.isDone before get(): " + futureListing.isDone());
prints true, even though the constructor hasn't printed that it has been created (this is clear just by counting). The while loop blocks permanently, even though the rest of the objects are created.
private class ReadSchedWorker extends SwingWorker<Void, Listing> {
private ChromeOptions options = new ChromeOptions();
private WebDriver driver;
private LinkedBlockingQueue<Future<Listing>> listingQueue;
private LoginFactory loginFactory;
private int year;
private int month;
private int day;
public ReadSchedWorker(Login mediasiteLogin, Login tmsLogin,
int year, int month, int day) {
System.setProperty("webdriver.chrome.driver", "\\\\private\\Home\\Desktop\\chromedriver.exe");
listingQueue = new LinkedBlockingQueue<>();
loginFactory = new LoginFactory(mediasiteLogin, tmsLogin);
this.year = year;
this.month = month;
this.day = day;
}
@Override
protected Void doInBackground() throws Exception {
this.options.addArguments("--disable-extensions");
driver = new ChromeDriver(options);
String portalUsername = loginFactory.getPortalUsername();
String portalPassword = loginFactory.getPortalPassword();
PortalDriver portalDriver = new PortalDriver(driver, listingQueue);
portalDriver.getScheduleElements(portalUsername, portalPassword, year, month, day);
while (!listingQueue.isEmpty()) {
System.out.println("beginning");
Future<Listing> futureListing = listingQueue.take();
System.out.println("future.isDone before get(): " + futureListing.isDone());
Listing listing = futureListing.get();
System.out.println("future.isDone after get(): " + futureListing.isDone());
System.out.println("before publish");
publish(listing);
System.out.println("after publish");
}
return null;
}
@Override
protected void process(List<Listing> listings) {
for (Listing listingItem : listings) {
listingListModel.addElement(listingItem);
}
}
}
Just for completion, here is getScheduleElements, which fills up the LinkedBlockingQueue:
public void getScheduleElements(String username, String password, int year, int month, int day) {
driver.get("https://rxsecure.umaryland.edu/apps/schedules/view/?type=search&searchtype=resource&id=100&start=" +
year + "-" + month + "-" + day + "&scope=week");
PortalLoginPage loginPage = PageFactory.initElements(driver, PortalLoginPage.class);
PortalScheduleEventsWeekPage scheduleEventsWeekPage = loginPage.login(username, password);
webElementsQueue = scheduleEventsWeekPage.initEventsQueue();
// doesn't need to be a queue...
// this is sequential....
// could do a parallel stream
while (webElementsQueue.peek() != null) {
Callable<Listing> callable = new PortalListingCallable(webElementsQueue.poll());
Future<Listing> future = executor.submit(callable);
listingQueue.offer(future);
}
executor.shutdown();
}
Edit: An example of what I mean (the future not being done, but reporting that it is, via the println()s in doInBackground()):
beginning
future.isDone before get(): true //the future says it done, but the object being created is below
in listing contructor: PHAR580 Pharmacy Law;Specialty Pharmacy 2;Recorded in Mediasite PH S201 2015-04-27T10:00:00.000-04:00 2015-04-27T11:50:00.000-04:00