JavaFX Async operations

I am helping a client with a JavaFX application and the client caches data locally using Mysql, after fetching it from a web service. To start the application,  an animated splash screen is shown, then the initial fetch of data is done, before showing the main application screen. 

What I noticed is that if I did the fetch routines in JavaFX script, the animation on the splash screen would pause. This is because JavaFX script executes on a single thread (Event Dispatch Thread, EDT, in the Desktop environment). To get around this, I needed the database and web service access to happen off the EDT in their own Thread. JavaFX does not support creating you own threads, so I had to resort to the javafx.async package. 

 Currently, there is built in support for asynchrously retrieving a remote text document from the Web via the javafx.async.RemoteTextDocument  class. However, my problem is I had to implement logic that would first check locally in the database, and if not present there, then go out over the web. Because of this, I could not use this class; off to write my own accessor class.

To do this, I had to write a Java class that  extends com.sun.javafx.runtime.async.AbstractAsyncOperation.  This class requires a constructor that takes at a minimum, a com.sun.javafx.runtime.async.AsyncOperationListener  object. The AsyncOperationListener provides various call back methods allowing control over the asynchrounous call and reporting of status, such as completion or failure. The could news here is that in the JavaFX side, the javafx.async.AbstractAsyncOperation  class handles this plumbing.


import com.mycompany.model.Employee;
import com.sun.javafx.runtime.async.AbstractAsyncOperation;
import com.sun.javafx.runtime.async.AsyncOperationListener;
import java.util.List;
import javax.sql.DataSource;

/\* EmployeeLoadImpl.java \*/
public class EmployeeLoadImpl extends AbstractAsyncOperation  {
    private Employee employee;
    public EmployeeLoadImpl(Employee employee, AsyncOperationListener listener) {
        super(listener);
        this.employee = employee;
    }

    @Override
    public Object call() throws Exception {
        DataSource ds2 = MyDatasource.getCommonsPooledDatasource();
        java.sql.Connection con = ds2.getConnection();

        // getDirectReports checks local db, and then web service if needed
        List directReports = EmployeeReportsDB.getDirectReports(con, employee.getId());
        employee.setDirectReports(directReports);
        return employee;
    }
}


The reason this is done in Java, is that the Call method executes its own Thread, and is not on the Event Dispatch Thread (EDT). By not attempting this in JavaFX, you can avoid unintended clashes with the JavaFX runtime. For example, you avoid clashes with binding or triggers. To be totally safe, the Employee model object is in Java, also.

The second part of this is to write a JavaFX class that extends javafx.async.AbstractAsyncOperation.  javafx.async.AbstractAsyncOperation provides the basic plumbing to  interact with the asynchronous runtime. Here is an example:


import javafx.async.AbstractAsyncOperation;
import 
com.mycompany.model.Employee;
/\*\*
 \* @author jclarke
 \*/

public class EmployeeLoad extends AbstractAsyncOperation  {
    var peer: 
EmployeeLoadImpl;
    public-init var employee:
Employee;

    public var onComplete: function(
employeeEmployee): Void;

    public override function cancel() : Void {
        if (peer != null) then peer.cancel();
    }

    protected override function start() : Void {
        peer = new 
EmployeeLoadImpl(employee, listener);
        peer.start();
    }

    protected override function onCompletion(value : Object) : Void {
        onComplete(
employee);
    }
}


 To use this in a program:


EmployeeLoad {

    employee: employee

    onComplete: function(empEmployee) : Void {   

           employeeUI = EmployeeUI {

                employee: emp 

         } 

   } 

}; 


 This is a little cumbersome, having to jump down into Java to execute the asynchrounous logic. The JavaFX team is currently looking at solutions to asynchronous use cases and hopefully enhanced language support or more Aysnchrounous framework classes will make this easier.




Comments:

Thanks for the great writeup!

Posted by Coffeejolts on December 10, 2008 at 02:05 PM EST #

James,

In the JavaFX code of class EmployeeLoad a in the function start() where is the listener parameter coming from?

protected override function start() : Void {
peer = new EmployeeLoadImpl(employee, listener);
peer.start();
}

These are the little things that make things great in applications.

Thanks, nice article!

-Carl

Posted by Carl on December 26, 2008 at 04:22 AM EST #

More on such good stuffs

Posted by Sunjava on January 07, 2009 at 12:45 AM EST #

Thanks, that was helpful.
Now is there a way to track the progress of the async operation? Like some onProgress callback method, so that javafx could update some progress bar, or status messages while async process is being executed?

Posted by Mike on January 19, 2009 at 02:45 AM EST #

Many thanks!

Your code was very helpful for enabling 3D rendering in JavaFX. It allows to initialize the Java 3D engine, the 3D scene, and the lightweight panel JCanvas3D as a SwingComponent asynchronously. Unfortunately, asynchronous repainting of a SwingComponent often results in a deadlock.

See post 'Java 3D meets JavaFX' at http://forums.java.net/jive/thread.jspa?threadID=56033&tstart=0

Posted by August on January 24, 2009 at 08:56 PM EST #

Presumably

peer = new EmployeeLoadImpl(employee, listener);

should be

peer = new EmployeeLoadImpl(employee, onCompletion);

Posted by Rhys Parsons on March 26, 2009 at 02:36 AM EDT #

Forget last post - it was rubbish!

listener is a member on AbstractAsyncOperation!

Posted by Rhys Parsons on March 26, 2009 at 02:37 AM EDT #

That"s Really Great. I Have To Learn ASbout Java Because I"ll Study IT At SENA. THIS WAS A AMAZING INFORMATION FOR ME. I!LL TRY TO LEARN MORE. I"M LOOKING FOR A FREE COPY OR SAMPLE OF JAVA FOR MY STUDIES. THANKS A LOT AJCV

Posted by ADOLFO JAV IER CUELLO VANEGAS on April 05, 2009 at 10:14 PM EDT #

Thanks Javes.

How can I detect the failure of the operation?

Thanks

Posted by Antonio on May 04, 2009 at 05:00 AM EDT #

how can i use it in jdbc??

Posted by sayantan chakraborty on May 07, 2009 at 08:48 PM EDT #

how can i use it in jdbc??

Posted by sayantan chakraborty on May 07, 2009 at 08:48 PM EDT #

see example for JavaFX v1.2
http://jfxstudio.wordpress.com/2009/06/09/asynchronous-operations-in-javafx/
Example requests Google Path API in asynchronous mode.

Posted by surikov on June 08, 2009 at 08:35 PM EDT #

y

Posted by guest on June 15, 2009 at 10:25 PM EDT #

Third ... Rosenberg says that we are opening our "competing proprietary products long after a successful open source project has eclipsed their proprietary alternatives." He then juxtaposes SPARC vs x86 as an example of this. Fascinating. I didn't know that OpenSPARC was in response to the previously open source x86 project. I must have missed that one.
http://www.watchrolexshop.com
http://www.gamegoldme.com
http://www.cheap-lotrogold.com
http://www.globalsale.me/Aion-gold-083.aspx
http://www.cheap-gamegold.org
http://www.gamegoldvip.org

Posted by replica rolex on June 24, 2009 at 09:38 PM EDT #

commercial printing
commercial printing like hot pancakes come the month of December. Every end-of-the-year transactions, impressive sales are made with commercial printing alone. Whether for personal, individual or corporate uses, cards are simply useful in all ends.<a href="http://www.7days-printing.com"> commercial printing </a>. Our commercial printing works closely with our customers to create the best card designs for your business. [URL=http://www.7days-printing.com]commercial printing[/URL]
We are an online commercial printing company that provides various services for making cards. Our company is composed of people who are experts in commercial printing and effective commercial printing facilities. Our commercial printing can create cards that you can use for type of advertisement. We can offer high quality production of cards using different commercial printing services. We usually use commercial printing for making cards.
membership cards
membership cards, membership cards are one of the best ways you can step ahead of your competion. Their durability shows your customers that you are building a long lasting relationship with them and that you value their membership cards.
<a href="http://www.7days-plasticcards.co.uk"> membership cards </a> our membership cards works closely with our customers to create the best card designs for your business. [URL=http://www.7days-plasticards.co.uk]membership cards[/URL] Check you membership cards your cards online today and we will start the process to make your new membership cards .For customers who have never ordered membership cards before, it is simple to find out membership cards .we also have made available an informative membership cards our customer support professinals today if you need help with your membership cards or if you have questions regarding the membership cards printing process.
plastic card manufacturer
plastic card manufacturer is popular with business owners who are looking for a unique way to promote their companies. A plastic card manufacturer’s card is a great way to distinguish your business from others that may be competing with you.
[URL=http://www.7daysprinting.com]plastic card manufacturer[/URL].The people to whom you give a plastic card manufacturer’s card is much more likely to remember you because your plastic card manufacturer’s card distinguishes your business. We’ll present some information about high quality plastic card manufacturer’s card for business owners who are interested in using this plastic card manufacturer’s card as it is cost-effective tool to increase traffic, sales and customer base.<a href="http://www.7daysprinting.com"> plastic card manufacturer</a>. plastic card manufacturer’s card degisns can be printed on two basic types of stock. You know that plastic card manufacturer’s card stock has a significant advantage over paper because it is more durable and feels different from typical buseness cards. This is plastic card manufacturer’s card’s advantages. A plastic card manufacturer’s card is an especially good choice for high tech companies that might want to use the plastic card manufacturer’s card as a plastic.
hologram security
after hologram security in many years of experience, we have developed a step-by-step procedure that keeps you ,the customer, in full control of your order.
[URL=http://www.hologram-sticker.co.uk]hologram security [/URL]And we know you will remember the hologram security. You can jost log yourself into our members area and and you will see the hologram security. The hologram security is quick and easy to find. If you have questions, please view our company. We with hologram security will be happy to help you.
<a href="http://www.hologram-sticker.co.uk">hologram security</a>. hologram security offer quality service as well as expert support. With hologram security, you will receive free art work&sepup of your graphic design. That’s right ,every customer has the chance to know our hologram security. that is usually enough time to recognize the hologram security. you will be pleased with our serveice in hologram security.
plastic card design
the plastic card design and other types of plastic cards for some of the most recognizable and prestigious names around. Our company made its mark with full plastic card design. plastic card design just sits there, quietly waiting to be found.
<a href="http://www.dynamicworldwide.co.uk"> plastic card design </a>. The plastic card design represent the front line image of your business, after all, plastic card design is very often used to make first contact with a prospect. plastic card design is imperative that your primary marketing tools portray professionalism and high quality. [URL=http://www.dynamicworldwide.co.uk]plastic card design[/URL]
Moreover, efforts put towards your plastic card design card, will give your customers a sense of how much attention is brought to your products and services. Our plastic card design will help you competing with others to get customers to contact you. plastic card design will help you grap attention.
metal business card
the mental business card and other types of plastic cards for some of the most recognizable and prestigious names around. Our company made its mark with full mental business card. mental business card just sits there, quietly waiting to be found.<a href="http://www.metal-card.co.uk"> metal business card </a>. The mental business card represent the front line image of your business, after all, mental business card is very often used to make first contact with a prospect. mental business card is imperative that your primary marketing tools portray professionalism and high quality. [URL=http://www.metal-card.co.uk]metal business card[/URL]
Moreover, efforts put towards your mental business card card, will give your customers a sense of how much attention is brought to your products and services. Our mental business card will help you competing with others to get customers to contact you. mental business card will help you grap attention.
gift bag printing
the gift bag printing and other types of plastic cards for some of the most recognizable and prestigious names around. Our company made its mark with full gift bag printing. gift bag printing just sits there, quietly waiting to be found.
<a href="http://www.printing-gift.co.uk"> gift bag printing </a>. The gift bag printing represent the front line image of your business, after all, gift bag printing is very often used to make first contact with a prospect. gift bag printing is imperative that your primary marketing tools portray professionalism and high quality.
[URL=http://www.printing-gift.co.uk]gift bag printing[/URL]Moreover, efforts put towards your gift bag printing card, will give your customers a sense of how much attention is brought to your products and services. Our gift bag printing will help you competing with others to get customers to contact you. gift bag printing card will help you grap attention.
smart card supplier
You might want to consider a smart card supplier with printing in a vivid color. Like most other types of business cards, smart card supplier ‘s a good idea to keep the design simple. [URL=http://www.smartcard-supplier.co.uk]smart card supplier[/URL]
This is also true of any artwork you might want to use on a unique smart card supplier. The smart card supplier will emphasize your message and make people easy to remember. smart card supplier include many things. A smart card supplier can be printed on bothe sides. A smart card supplier tends to encourage customers to keep it rather than discard it .<a href="http://www.smartcard-supplier.co.uk"> smart card supplier </a>. smart card supplier includes many techniques and tips. And the smart card supplier hasmany types. Owners who are looking for smart card supplier should seriously consider plastic rather than paper cards. Due to their versatility and impact, smart card supplier is a great and cost effective way to promote companies all over the world.

Posted by molly on July 24, 2009 at 02:59 AM EDT #

Hi,

i want to know is this example is compatible with javaFX 1.2 ?

or there is some other way to implement Async in JavaFX 1.2... ?

Posted by Nihar on September 22, 2009 at 12:40 AM EDT #

If you are interested in high quality but low price replica watches, please contact us via our website http://www.progiftstore.com/.

Posted by progiftstore on October 11, 2009 at 09:36 PM EDT #

JavaFX 1.2 does not have javafx.async.AbstractAsyncOperation class, but there must be a way to do the same job, I suppose... (Perhaps a more conveninent way)

Posted by Abdullah on November 08, 2009 at 07:25 PM EST #

watches-c-24.html">replica Gucci watches</a> ||
<a href="http://www.watchho.com/replica-bell-ross-c-5.html/">Replica Bell & Ross watches</a> ||
<a href="http://www.watchho.com/replica-chanel-c-9.html/">Replica Chanel watches</a> ||
<a href="http://www.watchho.com/replica-gucci-c-26.html/">Replica Gucci watches</a> ||
<a href="http://www.watchho.com/">luxury watches</a> ||
<a href="http://www.iluxurywatches.com/">rolex watches</a> ||
<a href="http://www.giftshot.net/">replica movado watches</a> ||
<a href="http://www.watchesgift.net/">replica chanel watches</a> ||
<a href="http://www.watchhandbag.com/">Replica watches</a> ||
<a href="http://www.handbag-handbags.com/">louis vuitton handbagss</a> ||

Posted by luxury watches on January 03, 2010 at 06:29 PM EST #

ok, could anyone tell me if javafx.runtime.async work on JFX mobile runtime 1.2, thanks.

Posted by Ginkan on May 14, 2010 at 11:31 PM EDT #

I tried to think so, but i found it was not as the same in the actual process. As you mentioned, I still have doubts, but really thank you for sharing!

Posted by eps machinery on December 29, 2010 at 11:59 AM EST #

I tried to think so, but i found it was not as the same in the actual process. As you entioned, I still have doubts, but really thank you for sharing!

Posted by vacuum forming machine on December 29, 2010 at 12:00 PM EST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

jimclarke

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today