Sunday, September 11, 2022

ZIO and JavaFX - Hello World

As part of learning ZIO (http://www.zio.dev/)  for other projects I’m working on I’ve decided to use LogoRRR as a testbed to see which challenges have to be solved in the context of JavaFX application programming with Maven and Scala.

For context:

ZIO is a framework to perform type-safe, composable asynchronous and concurrent programming for Scala.  

LogoRRR is a desktop application written in Scala, leveraging JavaFX as desktop application technology. LogoRRR helps to analyse log files and its target audience are support/devops engineers.


Starting fresh with no relevant ZIO experience, I’m interested in the CI setup. LogoRRR uses maven as build system, which is rather uncommon for a Scala project. There are two reasons for that - I know maven quite well, and I’m using Gluon’s  gluonfx-maven-plugin - that's the main reason why I'm using maven here. 


Anyway, now I’m confronted with following challenges:

  • How to setup a maven project with ZIO?
    • Is ZIO compatible with LogoRRR’s GraalVM compilation workflow?
  • How to integrate ZIO with JavaFX?


How to setup a maven project with ZIO 


In any case, one has to add ZIO as a dependency in the pom.xml file:





At the time of writing, ZIO supports Scala 2.12, 2.13 and also Scala 3 which is great for LogoRRR. (There was an attempt to upgrade LogoRRR to Scala 3, but IntelliJ Support for Scala 3 isn’t there yet. When my motivation allows I’ll try again.) 


The project still compiles without any issue, also GraalVM seems not to bother much about this new dependency.  You can see the relevant commit here: https://github.com/rladstaetter/LogoRRR/commit/7d6fd36cdf92a97648786adee2748dd9652742d5 



How to integrate ZIO with JavaFX?


To try out ZIO in LogoRRR, I start with copying ZIO HelloWorld to LogoRRR’s Source code:





This works without any problems. That’s already great, but does not prove anything other than that classpath resolution works in IntelliJ. Better than nothing.


JavaFX has it’s own Application class (javafx.application.Application) and ZIO as well (zio.ZIOAppDefault). Both of them initialise their environment. I’m stuck?


Searching for information in ZIO’s Discord Channels yields some information which may be valuable - a discussion from 3 years ago between BrajEje and Kaiser gives some hints: (https://discord.com/channels/629491597070827530/630498701860929559/645653183422660608


Anyways, as far as I understand it at the time of writing, ZIO is all about describing WHAT should happen in certain datastructures and magic incantations. 


In short, my strategy for a JavaFX app backed by ZIO is:

  • use JavaFX infrastructure to startup the application
  • describe ZIO effects
  • run effects where necessary

As seen from ZIO, LogoRRR is no ‘greenfield’ project. For this usecase there is an explicit way to enter ZIO’s territory via a `Runtime` class. See https://zio.dev/overview/overview_running_effects#default-runtime which I’ll follow here.


I’m starting with a basic JavaFX startup class:





Apart from naming it fancy ‘LogoRRRZIO’ this code has nothing to do with ZIO at all at the moment. But it will serve as a starting point for a JavaFX ZIO application showcase I hope.


First thing I’m doing now is to instantiate a ZIO runtime and try to click a button:





In order to start, JavaFX needs some command line parameters - see openjfx.io for details on that. I’ll add them to the IntelliJ run configuration: 



This seems to work:




Great! From now on it should be trivial to use ZIO as backend for everything. ☺️ 


… just joking, many challenges have to be solved ... but it is a start! :) 


See my work in progress on github and a presumably long living branch, this article refers to https://github.com/rladstaetter/LogoRRR/commit/42832fbd9ec92a4edfcca3ea9c69c1d58fed898f


Of course you can download latest builds of LogoRRR here to try out the application itself. I didn't blog about it, but this application is already usable from an end point users view and I would be happy to hear from you either as a programmer discussing implementation details or an end user requesting features or finding bugs 🙌

No comments:

Post a Comment