Showing posts with label javafx. Show all posts
Showing posts with label javafx. Show all posts

Monday, November 3, 2014

Automagical JavaFX2 Beans

FX Serials allows your traditional Java Beans to play in the new JavaFX scene.

Why?

JavaFX 2.0 introduced the JavaFX Beans specification that adds properties support to Java objects through the help of the properties classes from the JavaFX 2.0 properties and bindings framework. This allows the JavaFX GUI to bind its controls with the data model without the need for extra binding frameworks.
Although properties support has been long awaited in Java, the adoption of the new JavaFX2 Beans by enterprise application developers faces two challenges:
  1. For new applications, there is the questionable need for binding or observing a property on the server side, together with the additional dependencies and performance overhead needed to handle the extended domain model (see James Denvir's One Bean to Bind Them All post);
  2. For existing applications, there is the need to re-code the domain model in order to accommodate the new specification.
On the one hand, It would help the decision for new applications if there were some successful case studies. And on the other hand, facilitating the move for existing applications would help create some of those case studies.

How about a solution where you have traditional java beans on the server side which are "automagically" transformed into JavaFX2 Beans on the client side? - The concerns in 1 would be mute and there would be no need to re-code the domain model. Cool!

I implemented a first approach to this solution in my latest project: FXSerials. I hope you find it cool too :)

Version 1.0.1 is out and you can add it as a maven dependency with the following coordinates:
<groupid>io.github.juffrou</groupid>

<artifactid>fx-serials</artifactid>

<version>1.0.1</version>

This is still early stages but you can already proxy a class and a bean instance.

Please see the examples at the FXSerials page and check out the source code on Github.
Don't hesitate to drop me a line if you have a question or a suggestion about FXSerials.

Monday, October 6, 2014

JavaFX2 Websocket Client Deployed by Webstart JNLP

This post builds on the previous Websockets with Spring Framework 3.x and answers 2 questions:

  1. How to use webstart-maven-plugin to pack a JavaFX 2 client in a web application and launch it using Java Web Start (JNLP)
  2. How to use Tyrus, a java websocket client implementation, for client server websocket communication
The source code referenced here is part of a fully functioning application which can be downloaded and tested from the websocket-test repository at Github. Please read the README file there for pre-requisites and instructions on building and testing.

Project structure:

Maven project called websocket-test (packaging: pom) with two modules:

  1. websocket-client - JavaFX 2 application (packaging: jar) 
  2. websocket-server - Spring framework web application  (packaging: war)

Project packaging



The websocket-server module is packed with the depicted structure. The webstart folder contains the jar file generated by the websocket-client module and it's transient dependency jars. It also contains the websocket-client.jnlp descriptor file

I - Using webstart-maven-plugin to pack a JavaFX 2 client in a web application and launch it using Java Web Start (JNLP)

On the websocket-server module, configure the webstart-maven-plugin as follows:

Notice the build timestamp passed to the JNLP file. this will allow java web start to detect wether a cached application needs to be refreshed.
The details pertaining to the user certificate in the <sign> section are defined in the maven settings.xml file, so they remain private.

You must also provide a velocity template for the JNLP file, like the following:

This template contains variables. Some of them will be substituted at build time (ex: ${buildTS}) and some will be substituted by the JnlpDownloadServlet at download time (Ex: $$context)

Notice the parameter definition on line 28 (<fx:param name="server-url" value="$$context"/>). This will be passed to the JavaFX2 application and will tell it where to connect the client websocket.

II - Using Tyrus for client server websocket communication

Tyrus is the websocket reference implementation in java therefore it made sense to build this test project using Tyrus, although any other implementation could be used.

The application is going to establish a websocket connection to the server and the address of the server is calculated based on the value of the server-url parameter in the JNLP file:

Starting the Tyrus client manager will create a separate process with several execution threads to control communication between the client and the server. We need to tell the application that the security permissions bestowed by the java web start configuration are needed to launch the Tyrus client, otherwise an "access denied" exception will be thrown. We do this by starting the Tyrus client in a privileged block:

Please download the source code for all the details (git clone https://github.com/cemartins/websocket-test.git) and leave a comment here or raise an issue at Github if you have any thoughts or suggestions you'd like to share.