Reactive Programming with JavaFX

Java 8 is finally released and Lambda expression are an official part of Java. Thanks to this it’s much easier to write applications in a reactive way. One of the main concepts in reactive architecture is an event driven design. The reactive manifesto contains the following about event driven design:

In an event-driven application, the components interact with each other through the production and consumption of events—discrete pieces of information describing facts. These events are sent and received in an asynchronous and non-blocking fashion.

Reactive Programming with Java

By using Lambdas it’s very easy to define callbacks that can react on specific events. Here is a short example about an event driven design without the usage of Lambda expressions:

public static void hello(String... names) {
        Observable.from(names).subscribe(new Action1<String>() {

            @Override
            public void call(String s) {
                System.out.println("Hello " + s + "!");
            }

        });
    }

Thanks to Lambda expressions the same functionallity can be coded in Java 8 this way:

public static void hello(String... names) {
        Observable.from(names).subscribe((s) -> System.out.println("Hello " + s + "!"));
    }

The example is part of the RxJava tutorial.

Reactive Programming with JavaFX

Let’s take a look at JavaFX. Because the JavaFX API was designed for Java 8 it provides a lot of Lambda support and callbacks are used a lot. But next to the default JavaFX APIs there are currently an open source projects that adds a lot of reactive design and architecture to the JavaFX basics: ReactFX.
By using ReactFX you can do a lot of cool event driven stuff with only a few lines of code. Here is an example how event handlers can be designed to react on user inputs:

EventStream<MouseEvent> clicks = EventStreams.eventsOf(node, MouseEvent.MOUSE_CLICKED);
clicks.subscribe(click -> System.out.println("Click!"));

I think the API provides a lot of cool functionallity that let you design JavaFX applications that are more reactive and I hope to see a lot of more code like shown in the example above.

Summery

The are currently 2 cool reactive APIs for Java out there: RxJava for a basic use in Java and ReactFX that is specialized for JavaFX. Theoretically you can do everything (or most of the stuff) you can do with ReactFXwith the help of RxJava, too. But here you need to concern about the JavaFX Application Thread. Because ReactFX is implementated for JavaFX (or a single threaded environment) you don’t need to handle this. A first comparison of this two libraries can be found here.

2 Responses to Reactive Programming with JavaFX

  1. Based on the reactive manifesto, JavaFX can’t do reactive since it is single threaded and so event handlers will always block. Although requiring async seems like an implementation detail to me.

    • Hi Andy,
      but you can define background tasks that will run in background thread. These tasks can be defined in a reactive way.
      When developing a server application the requests can be executed in several threads. But if you only have one user you could execute all the requests in a single thread. By doing so all requests should return a response as fast as possible. By using a reactive architecture you will send internal messages / requests to your subsystems or business logic and add callbacks that will be called once the messages will be handled. All these callback can send the calculated data to the client. SSE or WS could be used here. This is an reactive architecture how it can be used for web based application. You can do the same for JavaFX, too. All the user event handlers will be called in a single thread (JavaFX Application Thread) but doesn’t need to calculate a result. These handlers send a message that will be handled by a different thread. By adding a callback that will update the UI once the calculation is done you can define a JavaFX application in an reactive way.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>