Categories
Development

MyHome Crud

Lets start with create/update/delete using good old fashioned WebMvc Spring, Kotlin and JOOQ.

Following checkpoint has:

  • Gradle kts build script
  • JOOQ code generation from DB
  • SQL script to initialize DB
  • Controllers and repositories to create/update/delete many entities.
  • Some basic error handling
  • Some extension functions.
  • Open API auto-generated

https://github.com/MavoCz/myhome/tree/checkpoint-01-crud

Notes:

To set up logging of JOOQ queries:

Which gives you awesome insight into every SQL query and result:

Code generation of JOOQ works well, but its a bit hard to set up. The configuration below scans a database and creates JOOQ table definitions and records.

It can automatically map Kotlin/Java enums for you, but each needs to be configured. See lines 27.

Kotlin gotcha: For some reason injection into super class did not work without open keyword for every property.

Some notes to DB update code:

Don’t forget to call execute at the end. updatedRecord is R : Record, where record is org.jooq.Record. Idea keeps importing java.lang.Record which is quite annoying. updatedRecord creates a minimal set or changes, so if it contains only one changed field, it will have only one field in final SQL. Nice.

Jooq Records are a nice way to hold/manipulate/transfer DB data to insert/update/result.

It can be mapped nicely to and from a DTO/data class, generator is able to create on per entity record class with convenience methods named after columns. And if you don’t want to deal with those pesky DTOs, it can serialize data to JSON too.

Categories
Development

Copilot experiments

Since its that time of the year again and holiday spirit is once again elevated by advent of code challenge, I decided to participate, but this time with the help of Github AI assistant Copilot.

So start 2 month trial, install Idea plugin and here we go. And the first impressions were … not great. At first it did not generate anything. I spent 10 minutes with trying few things. Then I found out how to define method and first sudgestions appeared.

It seemed like it was reading my mind … code was sometimes usable as is, sometimes needed a bit of tweaking, rarely was completely off. But I was amazed about how it worked out

Categories
Development

Expression evaluation

I had to implement a library which evaluates a set of mathematical expressions with variables, where each variable can also be an expression. Luckily we had JGraphT library already on the class path, so it was quite quick and painless. All that is needed is:

  • Parse all expressions, extract variables
  • Build a directed graph, expression is a node and edge represents a dependency of an expression on other expression/variable.
  • Check that it does not contain cycle.
  • Create a DepthFirstIterator and gradually calculate all expression values and collect them in a map. This ensures that all variable values are available when an expression is evaluated.

Code can be found here:

https://github.com/MavoCz/GraphExpressionEvaluator

Example of an expression tree as created by JGraphT visualisation:

Categories
Development

Spring Boot & Kotlin & Jooq

Last year I dabbled with Kotlin during Advent of code. This year as I’ still not using Kotlin daily at work, I decided to make something a little more practical … code a webserver using Kotlin. It will be an ongoing project for December, but I’m starting to write my notes down so I don’t forget them.

Since the energy crisis is happening here in Czech Republic, I will make “MyHome” server which will:

  • Make an inventory of all my items/appliances at my home. What is where, what is under warranty, and so on.
  • Enable me to monitor energy consumption and upkeeps for each item (electricity of a fridge, diesel for my car, upkeep for my car)
  • Monitor list or item rentals to my friends.

So its Kotlin webserver app and I chose Spring Boot and JOOQ. Spring Boot as I need to brush up on my Spring skillz, JOOQ because we are using it on current project and I quite like the no bullshit, no magic, we love SQL approach.

The plan is to eventually several technologies that I was not able to get my hands on:

  • Native Image with Graal and Sprint 6 (around the corner)
  • GraphQL API
  • Security with JWT
  • Multi tenant experiments
  • Frontend/full stack framework Hilla
  • Frontend with React or Flutter

MyHome project code is public on Github:

https://github.com/MavoCz/myhome

Main/Master will hold the most current version, branches with checkpoint-xx will be snapshots of some functionality.

Categories
Development

Postman JWT token generator

Use script below to generate and set JWT token with HS256 signature and store it in “jwt” environment variable. To use it edit SECRET_KEY and set headers as you need. Then paste the script into pre-request script in postman HTTP request definition. Use the environment variable {{jwt}} in your authentication of type Bearer to use it.

You can check the result token in https://token.dev/. And you can read all about JWT and their signing at https://auth0.com/blog/rs256-vs-hs256-whats-the-difference/ .

Code taken/was derived from  https://hirosht.medium.com/customized-jwt-encoded-with-hmac-sha256-hs256-267574bd7639 

Categories
Development Travel

Spring iO Barcelona 2022

I have attended Spring IO in Barcelona in spring 2022. It was a well organised conference with a lot of lectures, few workshops, good food, great location, good aircon. And since it was the first bigger conference after Covid, it was sold out! I got a ticket for Spring 2020, which surprisingly was still valid. Anyway, here are my notes from workshops and sessions that I have attended.

Most of the videos and slides are being published on  Spring IO twitter: https://twitter.com/spring_io or youtube channel: https://www.youtube.com/c/SpringIOConference.

Saturday started with a workshop

https://2022.springio.net/sessions/howto-from-source-code-to-container-runtime-workshop

There was a 20 minute intro about various containers (docker, pack, git). Then we each got a virtual machine to which we connected and experimented with various image types. In the end we spent most of the time copy/pasting stuff from github repo to vm. But it was nicely explained what every command ford and it did gave me the overview and some hands on experience.

https://2022.springio.net/sessions/building-a-framework-on-top-of-spring-boot

Which was ok with message being that you should not force the coders to extend your classes implementing your common functionality, but instead use the spring boot mechanisms (starters, imports, filters) and annotations. This way developers work with standard spring and do not have to learn a lot of custom code.

https://2022.springio.net/sessions/event-streaming-for-the-best-of-all-worlds

Was one of the worst sessions. I learned a definition of the event and then I fell asleep. The sleep was more informative then the session which in the end did not really sell the Apache Pulsar that much.

https://2022.springio.net/sessions/java-meets-typescript-building-modern-web-apps-with-full-stack-type-safety

It was a brief introduction to Hilla framework with some live coding showcasing the framework strengths. It made me with to try the framework. Looks like a cool evolution of vaadin framework with network typescript layer generated from java backend types and endpoints and out of the box support for websockets and events. Out of the box support for push notifications looks interesting.

https://2022.springio.net/sessions/kubernetes-native-java

I like Josh, he is an entertainer turned advocate, or advocate turned entertainer. His high energy showcases are fun and I learned something too. A little bit shallow, but wide live coding  session showcasing spring native and some other spring boot stuff.

Sunday started with a session Securing SPAs with Spring

https://2022.springio.net/sessions/securing-spas-with-spring

I really liked this one. Well prepared live coding with scenarios showcasing different hacker attacks and how to mitigate them. He ended with an app that looked well secured against well known attacks:

https://github.com/marcusdacoregio/springio-2022-securing-spas-with-spring/tree/main

https://2022.springio.net/sessions/analyzing-safe-flights-and-firey-wrecks-with-jdk-flight-recorder

Well I was expecting a little bit more indepth look. In the end I got a session that said that a tool exists, you can run it this way and open the recodings this way. Not much in a way of real ussage. A basic tutorial online does the same.

https://2022.springio.net/sessions/bootiful-opentelemetry

It gave a good overview over what open telemetry is and where it is going and why its needed.

https://xeraa.net/talks/bootiful-opentelemetry/

https://2022.springio.net/sessions/welcome-spring-for-graphql

Spring has finally a native graphql support. Great introduction, want to try that.

https://2022.springio.net/sessions/building-an-enterprise-product-on-top-of-spring-boot-a-real-life-testimony-from-flowable

From company that provides a product as code for customisation both on free and payed tier. It was really interesting to watch. It showed how much the spring boot stuff is complex when you dig deep and want to build and extensible framework. On the other hand, the result looked good … albeit with a lot of magic.

https://2022.springio.net/sessions/thoughts-about-layered-architecture-mapping-efficiently-with-sql

Well I got to know that we don’t have to strictly stick to 3 layered architecture. The message was simple yet the way to it was 45 minutes long. It is too long for the message it gave, but on the other hand it was funny in the way that only Germans can be funny.

https://pitch.com/public/2f78637f-139b-4dc2-9792-b353abf38e25

https://2022.springio.net/sessions/the-dream-stream-team-for-pulsar-and-spring

Finally I got to know what pulsar is and why should I care about it. Chilled Dude which flyed though 100+ slides in 30 minutes with 100+ “pretty straightforward” in every sentence. But I kind of got curious about Pulsar as it seems to know a lot.

https://2022.springio.net/sessions/our-datamesh-journey-data-architectures-for-microservices

The best real life experience sharing sessions from the conference. Carlos Saona-Vazquez described their way from monolith to microservices. Why they did it (team size), how they exchange information between services (events),  what is inside those events. How they try to make sense of all data domains.

Categories
Development

Advent of Code

I participated in Advent of Code challenge this year (2021). For those unfamiliar with this its a series of 25 coding challenges, each released on next day from 1st to 25th of December. For completing each you get a star and bragging rights in front of your colleagues :).

https://adventofcode.com/2021/about

For those who like moving pictures, there is a Youtube video from the creator and maintainer of Advent of Code. Quite funny.

I implemented 20 and half days in Kotlin language to brush up on my Kotlin skills. Learned a lot and quite a lot of fun, but in the end I got burned out and did not finish the last 4 and half days.

https://github.com/MavoCz/advent-of-code

I started to like Kotlin :heart:

Categories
Development

Switching to Mac

I had to switch to Mac from Windows for my current job. Its a reasonably painful transition so far :). This post is to be used as memory dump to help me (and perhaps others) with the transition.

First observation is that Intel Mac has the same fan problems as windows laptop :). Got the 16 inch Mac Pro with I9 and the fans are loud even with medium load.

Second is that the Dell thunderbolt dock does not work with Mac. Oh well.

Install git

which is part of xcode for some reason and installation takes for ever

xcode-select --install
git config --global user.name "<your name>"
git config --global user.email "<your email>"

Copy your ssh key to ~/.ssh (or create a new one).

Brew

Install brew as described on https://brew.sh

Terminal

Make it pretty with oh my zsh:

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

And install ITerm2.

Install Java

Use SDKMAN as described here: https://stackoverflow.com/questions/52524112/how-do-i-install-java-on-mac-osx-allowing-version-switching

Java will be installed here: /Users/USER/.sdkman/candidates/java/current

Install Postgresql

brew install postgresql
createuser -s postgres
brew services restart postgresql

Windows app replacement

Install Commander One as Total Commander replacement through Apple store.

Install Rectangle for better window management similar to PowerTools.

Install MonitorControl so you can actually control brightness of an external display :facepalm:

https://github.com/MonitorControl/MonitorControl

Useful shortcuts

Shortcuts on Macbook pro are quite a bit more complex. Instead of two switch buttons Alt+Ctrl on Win, you have Ctrl+Option+Command on Mac. Additionally on Mac with touch bar (my case) you must press fn quite a lot. In Idea the combinations of switches does not make much sense … or I did not yet figure out the various permutations of keys.

Command + Control + Shift + 4 – capture screen to clipboard

Command + ~ – Switch between windows of same app. For example two chrome browsers with different accounts.

Option + left/right – move to Next/previous word

Command + Q – quit

Command + Controll + Q – lock the computer

Command + Option + Escape – Force Quit is hidden in the Apple menu in the top left-hand corner of your screen.

Disable unneeded shortcuts in System preferences / Keyboard / Shortsuts as they are clashing with Intellij Idea.

Categories
Uncategorized

Coding in WSL2

Windows 10 added a new feature called windows subsystem for Linux. Suprisingly, it keeps evolving, now in version 2 and supposedly will be even better in windows 11 with easier install and more features. Also my IDE of choice IDEA is supporting it. And as I had a bit of time on my hands in new job as I was waiting on a new Mac to arrive to replace my windows machine, I figured I will give it a try.


As the new code base, run environment and built tools of my new project was not compatible with windows, I had the following idea:

Have the code, java, git and runtime in WSL2 and leave the IDEA in windows. The installation was fine, using the apputility to install everything in a jiffy.

  • Go to windows store :). Search for Ubuntu. Install latest version.
  • Start ubuntu from start menu.
  • Install git, java, install docker in Ubuntu (manually or by using Docker Desktop which sets it up)
sudo apt install git
sudo apt install openjdk-11-jdk
sudo docker pull postgres:11sudo docker run --name postgres11 -p 5432:5432 -e POSTGRES_HOST_AUTH_METHOD=trust -d postgres:11

Checked out the code on WSL2 subsystem using the WSL2 git.

Then I went to IDEA and configured it to use git from the WSL. This works surprisingly well and gives you the option to skip git installation on windows which is a weird linux/win hybrid.

Then I opened the project on WSL2 subsystem. Yes it is possible and should work fine … but it does not. My project was maven multi-module and all maven projects were marked with error message:

Connection refused to host: 127.0.0.1 in WSL maven connector

Apparently this is a known bug in IDEA: https://youtrack.jetbrains.com/issue/IDEA-266222 and as of writing this it should be fixed, but not released. But in my case this was a blocker and it rendered the project unusable.

Plan B was to install IDEA in WSL2 and use its UI through XServer

Install XServer: https://techcommunity.microsoft.com/t5/windows-dev-appconsult/running-wsl-gui-apps-on-windows-10/ba-p/1493242

Then follow: https://nirisarri.github.io/2020-11-03-how-to-install-and-run-intellij-idea-for-linux-in-wsl2/

I had to install idea from a zip/tar file as apparently snap utility is not supported on wsl. See https://github.com/microsoft/WSL/issues/5126.

The ide ran fine, IDEA ran in WSL, UI was being stream to xserver in windows. But the UI experience was rather poor. Icons had a strange outline, popup windows did not render correctly, some of the popups were not rendered at just displaying some bufferend UI fragment. Otherwise it was usable, project imported correctly), UI speed through xserver was OK. I managed to fix the icons by setting some obscure idea parameter, but not the bad popups.

And then I finally received my Mac and could give up on this. So the outcome is that the WSL IDEA support is not there yet.

WSL on its own is awesome and provides much needed full linux support on windows.

Categories
Development

Graal basics

I spent last year designing and implementing javascript scripting support in our Java/Spring backend with the help of GraalVM and its polyglot support. I fell in love with Graal, as it brings quite a lot of features and performance improvements to Java JVM. Will focus on basics of Graal Polyglot capabilities in this article.

I’m not going to write about what is Graal, others have done this better:

https://www.graalvm.org/why-graalvm/
https://www.graalvm.org/reference-manual/embed-languages/

Nice article to read is also Top 10 Things To Do With GraalVM.

Make sure you follow their twitter and blog:
https://twitter.com/graalvm
https://medium.com/graalvm

And its a good idea to join public Graal slack group as the devs and community is quite active and eager to respond.

https://join.slack.com/t/graalvm/shared_invite/zt-anuczas6-PrCle0QVfUFPrnMEcp_JEw

From my experience you will need to ask for some advanced/hidden features as they are not described in docs or scattered between several sources. Advanced topics are described here:

https://github.com/oracle/graaljs/tree/master/docs/user

One of the killer features of Graal is its polyglot support. The most mature one is the Javascript scripting engine. And as opposed to previous javascript java implementation, its fully ECMAScript compliant so you have the whole JS library ecosystem at your disposal.