From 1a1b0bb9f977750b8dc9a79754a4977fbf26dd25 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Thu, 24 Apr 2014 15:39:32 -0400 Subject: [PATCH] hatch: avoid nesting event loops; more logging Signed-off-by: Bill Erickson --- src/org/evergreen_ils/hatch/Hatch.java | 39 ++++++++++++++++++++------- src/org/evergreen_ils/hatch/PrintManager.java | 6 ++++- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/src/org/evergreen_ils/hatch/Hatch.java b/src/org/evergreen_ils/hatch/Hatch.java index d81f911..60e8d05 100644 --- a/src/org/evergreen_ils/hatch/Hatch.java +++ b/src/org/evergreen_ils/hatch/Hatch.java @@ -69,6 +69,10 @@ import java.io.FileInputStream; * blocking thread queue, observed by a separate Service thread, whose * job is only to pull messages from the queue. * + * Beware: On Mac OS, the "FX Application Thread" is renamed to + * "AppKit Thread" when the first call to print() or showPrintDialog() + * [in PrintManager] is made. This is highly confusing when viewing logs. + * */ public class Hatch extends Application { @@ -130,7 +134,6 @@ public class Hatch extends Application { @Override public void start(Stage primaryStage) { this.primaryStage = primaryStage; - logger.debug("start()"); startMsgTask(); } @@ -151,8 +154,7 @@ public class Hatch extends Application { String contentType = (String) params.get("contentType"); browser = new BrowserView(); - Scene scene = new Scene(browser, 640, 480); // TODO: printer dimensions - //Scene scene = new Scene(browser); + Scene scene = new Scene(browser); primaryStage.setScene(scene); browser.webEngine.getLoadWorker() @@ -161,7 +163,21 @@ public class Hatch extends Application { logger.info("browser load state " + newState); if (newState == State.SUCCEEDED) { logger.info("Print browser page load completed"); - new PrintManager().print(browser.webEngine, params); + + // Avoid nested UI event loops -- runLater + Platform.runLater(new Runnable() { + @Override public void run() { + new PrintManager().print(browser.webEngine, params); + + // don't start a new message listener thread + // until we are done with this print call. + // otherwise, we risk running parallel print + // operations and that could be bad... not sure. + // In the meantime, pending messages will be + // sitting in the WebSocket input buffers. + startMsgTask(); + } + }); } }); @@ -189,11 +205,16 @@ public class Hatch extends Application { Map message = (Map) t.getSource().getValue(); - // send the message off to be printed - handlePrint(message); - - // go back to listening for more requests - startMsgTask(); + // avoid nesting UI event loops by kicking off the print + // operation from the main FX loop after this event handler + // has exited. + Platform.runLater( + new Runnable() { + @Override public void run() { + handlePrint(message); + } + } + ); } }); diff --git a/src/org/evergreen_ils/hatch/PrintManager.java b/src/org/evergreen_ils/hatch/PrintManager.java index 3568b8e..bfe908c 100644 --- a/src/org/evergreen_ils/hatch/PrintManager.java +++ b/src/org/evergreen_ils/hatch/PrintManager.java @@ -104,9 +104,10 @@ public class PrintManager { if (showDialog != null && showDialog.booleanValue()) { logger.info("Print dialog requested"); + if (!job.showPrintDialog(null)) { // job canceled by user - + logger.info("after dialog"); job.endJob(); socket.reply("Print job canceled", msgid); return; @@ -117,7 +118,10 @@ public class PrintManager { logger.info("printing..."); engine.print(job); + logger.info("after print"); + job.endJob(); + socket.reply("Print job succeeded", msgid); } -- 2.11.0