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.

3 comments:

  1. FX Experience (links of the (past few) week(s), October 13) sais: "Carlos Martins has posted about how to developed a JavaFX websocket client application that is deployed via Webstart JNLP."

    Thank you for the mention.

    ReplyDelete
  2. Hello,

    Thank you for the post.

    Can we invoke jnlp and get response back to the browser using tomcat(server) and swings(client) through websocket.

    ReplyDelete