Java Download Service Listener


Bookmark and Share

One of the previous post [Applet Startup Time] provided a way to measure start-up time of JavaFX Applets. Many responded with requested information (Thanks a lot!). One of the main concern raised was high start-up time for “Without JavaFX Runtime”. This is high because it includes time taken to download and install JavaFX Runtime.

Unfortunately there was no way to notify the user about exact progress of this. Hence user will not be able to differentiate between download and install of “JavaFX Runtime” and “JavaFX Application”.

Recent JavaFX Update 1.2.3 reduced the download size and start-up time. You can notice this change by trying out the start-up time measurement applet again.. But still it doesn’t provide a way to exactly notify the start-up progress. We can only specify a custom splash-screen as specified here.

And then we have Java Update 1.6.0_18 Not sure how many of you noticed this feature – Download Service Listener It allows you to provide a Custom Progress UI using AWT/Swing/Java2D APIs. It has callback methods which will provide information related to download and validation of resources. Please refer to Download Service Listener Example for more information. Refer to ProgressListener.java and ProgressView.java for implementation used in this sample.


To launch click on above image or

Now we can implement custom progress listener with actual values. We can use the same approach for JavaFX applications as well. There is minor issue in using this feature with JavaFX Applet deployed using dtfx.js. This script is not updated to utilize this feature. So for now we need to explicitly hide the default splash screen by invoking JavaScript method hideOverlay as shown in ProgressListenerFX.html

Reference:

Hmm.. thatz a long post with lots of information.. But I think its very useful.. As usual, please try it out and let me know feedback..

Advertisements

To Pack or not to Pack?


I’m back from vacation.. Hmm.. well it was not this long though.. (my last post was more than a month back).. I traveled to few places, exploring new things.. You may view the some of the photos here!

Well was busy with many other things as well..
Oracle OpenWorld, Mobile Application Conference..

Anyway back to the topic.. Is it always good to compress the jar while deploying the Java/JavaFX application? What will be its impact on startup time? Or may be I was doing too much pack and unpack of luggage during vacation and hence the thought is still lingering in my mind?!

I think the answer depends on the application and target users. Pack helps to compress the jar file size (reduce download time). But it may increase the start-up time due to additional step – unpack. So if the application is too small, it may be better not to use compressed jar. Also if the target audience has very good bandwidth, then user may not see the benefit of reduced download time.

Applet – Pack Applet – No-Pack

Note: Above example is not the best to demonstrate any significant difference in warm start-up time

Also set update check to background in jnlp files as shown below.. Please refer to tech tip on start-up time for more information.


<update check="background"/>

JavaFX packager was generating jnlp files with missing / for close tag for applet-desc – param tag. Due to this any subsequent tags were getting ignored. So we need to ensure that all tags are properly closed. This issue with javafxpackager is resolved (not be available in 1.2)


<applet-desc ... >
    
<param name="MainJavaFXScript" value="..." />
</applet-desc>

Best way to validate the jnlp is to launch the application and view the jnlp in Java Cache Viewer. Ensure that all entries are reflected properly. Update check must be set to “background” and not “timeout”.

Well.. I never thought that one character (or lack of it) can contribute so much towards start-up time! Keep exploring!!

JavaFX – Upload and Download Large Files


We can upload or download large files using new HttpRequest attributes available in JavaFX 1.2.

HttpRequest has input and onInput attributes that provides InputStream which can be used to download data. This stream supports mark, reset, available, skip etc. But to support this HttpRequest buffers the data. Due to this buffering it was not possible to use this approach to download large files.

JavaFX 1.2 introduce two new attributes – source (InputStream) and sink (OutputStream). If we set sink attribute, we can directly download the content of stream without buffering. Also with source we can upload the content. Eg: By setting it to FileInputStream. When source or sink attribute is used, corresponding input, onInput, output, onOutput will not be functional.

For Applet mode, click on above image

For standalone mode

The application downloads a larger version of the photo. I took it from Somnathpur using Nikon Coolpix! 🙂


function downloadFile(url , outputFile) {

    def getRequest: HttpRequest = HttpRequest {

        location: url
        sink: new java.io.FileOutputStream(outputFile)

        onToRead: function(bytes: Long) {
            toRead = bytes;
            println("onToRead({bytes})");
        }

        onRead: function(bytes: Long) {
            read = bytes;
            println("onRead - {read * 100/toRead}%");
        }

        onDone: function() { println("onDone") }
    }

    getRequest.start();
}

In above sample, sink is assigned to a FileOutputStream. So all the content will be directly written to file without buffering. Note: For making the application compatible with mobile or other platforms we will have to use only subset of java.io package. Example: MID Profile Core API.

Source: