java - JavaFX Block the UI (and the code) until a server call is finished -


this topic around in countless articles of "not blocking javafx ui during long lasting call time consuming (server)". know , googled , tried lot. of "internet" explains long lasting calls caused javafx events need made in thread. after thread finished, update fx ui results via platform.runlater(). of fx libraries encapsulate sophisticated code constructs (really cool stuff). current problem is: migrating swing rich client javafx. huge one, have include/replace javafx ui it, until full fx client. there functionality in client server call , has wait before user can continue work. server uses jee6 stateless session beans , interfaces. interfaces known client , little library of our own, implemented little proxy hiding away communication layer client. client creates "remoteproxy" library calling remote interface , library propagates call server. method called , result or exception transported client. client appears local call. here problem. typical code fragment looks this:

... serverremoteinterface myserverremoteproxy = helper.getproxyforinterface(serverremoteinterface.class) ; ... complexresult result = myserverremoteproxy.dothingsonserver(serializableparameter p1 ...) dosomethingusefull() ; 

the call server triggered in swing ui thread (by listener). stops execution of program (listener code) although done in thread. "dosomethingusefull()" called after server got back. developer not have take care threading here. how accomplished? using "spin library" (http://spin.sourceforge.net/). clever tricks swing edt. alternative use modal dialog, decided not not have window, have glasspane disabling ui components instead.

so long explanation , short question... there similar javafx helping seamlessly call server, stop program execution until got , not blocking javafx ui? best if can work java swing parts of code.

edit... adding compressed example demonstration of use hidden jdialog.

we need server remote interface. interface do.

public interface serverremoteinterface {    public string method1() ; // string result in our case test purposes    public string method2() ; // exceptions possible. } 

then need proxy invocation handler

public class serverproxy implements invocationhandler {    public object result;    jdialog modaldialog = new jdialog((frame)null, true);     @override    public object invoke(object proxy, method method, object[] args) throws throwable    {       servercallhelper serverthread = new servercallhelper(args, method) ;       modaldialog.setlocation(4000, 5000); // if using spin library. here use dialog block in location off screen.       serverthread.start();       modaldialog.setvisible(true) ;       return result;    }     class servercallhelper extends thread    {       object[] args;       method method;        public servercallhelper(object[] args, method method)       {          this.args = args;          this.method = method;       }        public void run()       {          // server call via rmi or tunnel via http, rest or whatever , provide call parameters. on server side there must receiver propagating call wanted session bean.          // here simulation          try          {             thread.sleep(3000);          } catch (interruptedexception e)          {             // interupt ok here.          }           // hand result call back. simulate fix result          // have caught exception , deal it.          result = "simulated result" ;          // since in worker thread => inform edt close dialog.          swingutilities.invokelater(()->modaldialog.setvisible(false));       }     }  } 

and code show functionality

public class sampleui extends jframe {    jbutton servercallbutton = new jbutton("call server") ;    jlabel resultlabel = new jlabel("no result far") ;    public sampleui()    {       jpanel cont = new jpanel() ;       cont.add(servercallbutton) ;       cont.add(resultlabel) ;       this.setcontentpane(cont);        servercallbutton.addactionlistener((e)->processserverbuttoncall());     }     private void processserverbuttoncall()    {       serverremoteinterface serveraccess = (serverremoteinterface) proxy.newproxyinstance(sampleui.class.getclassloader(), new class[]{serverremoteinterface.class}, new serverproxy());        string result = serveraccess.method1() ;       resultlabel.settext(result);      }     public static void main(string[] args)    {       sampleui sampleui = new sampleui() ;       sampleui.pack();       sampleui.setvisible(true);    }   } 

the example compressed should show principle. developer not have take care call server server call. me it's local call. not have take care in edt thread, because am. said work same way in fx modal stage. tried set opaque 0.0 => not drawn anyway. works.

the question remains: there ways around jdialog or stage ?

if understood intentions correctly, use case future:

completablefuture.supplyasync(() -> myserverremoteproxy.dothingsonserver(...))         .thenaccept(result -> dosomethinguseful(result)); 

both server call , dosomethinguseful executed in thread, need use platform.runlater in dosomethinguseful if want access scene graph.


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -