Server-side image creation using Java FX

I am currently working on a Jax Rs application, and I want to output a Base64 encoded image to the client. The client is a mobile device.

The mobile device will call this service with some parameters, and the server should draw a histogram and send it back to the device in the form of a base64 encoded string.

Since java Fx having the necessary diagram libraries, I made a sample using the following tutorial. The snapshot function also worked correctly as expected (to create a screen image).

http://docs.oracle.com/javafx/2/charts/bar-chart.htm#CIHJFHDE

Now I want to do this without extending the Application class, because I need this in a Jax Rs application. So that I can just use the api to create a BuffredImage, and then use it to create a Base64 string.

I found a way to do this using JFreeChart. But I prefer if I can do this using Java FX. I have no previous experience with Java Fx

I ask for advice

+6
source share
1 answer

Server Based JavaFX Initialization

To run JavaFX on the server, you must either:

These are the only ways that the JavaFX runtime system is initialized in JavaFX 2 so that you can use it.

Using a JFXPanel is probably a little less efficient than using a JavaFX application.

The following discusses JavaFX system initialization in the StackOverflow question: JavaFX 2.1: Toolkit not initialized .

JavaFX is a single-threaded system

You can create most JavaFX components in any thread. However, to render components in a scene, you must do work on the JavaFX application thread. This means that if you have a multi-threaded server process that most servers run on and you want to generate multiple diagrams, you will need to execute a single flow of diagram rendering requests using concurrency restrictions.

Then, the server handler stream can be used by ImageIO to convert the AWT image to the output stream in png format. You can take the resulting stream and Base64 encode it and return the base 64-encoded stream to the server in response to calling the request for the original image request.

Ensure a graceful shutdown

You will want to call Platform.setImplicitExit (false) when your server starts and registers a stop hook or ServletContextListener , which controls when the servlet is destroyed, so you also call the .exit () platform to disable the JavaFX system. If you do not, it is likely that your server will not be able to shut down because the JavaFX application thread will continue to work, waiting for work.

JavaFX 2.2 is not really certified to work on a headless server

Swing applications can run silently using the java.awt.headless system property. I do not know about a similar property for JavaFX, although there may be one, and if that were possible, you could find out what it is by asking the openjfx-dev mailing list .

JavaFX is primarily designed as a client graphics toolkit. Although you can make it work and work satisfactorily for your application on the server, for this you may need to make sure that the server is not configured as a headless server and that it has an appropriate accelerator graphics card that provides reasonable loading performance.

You can request official headless mode support for JavaFX bug tracking .

+8
source

Source: https://habr.com/ru/post/959312/


All Articles