Skip to main content

How to use Groovy + GraalVM + Quarkus together?

If you are one among those who like Groovy for its simplicity and tight integration with Java (and not for its dynamic capabilities) and wondered how to write Groovy code in a Quarkus application and compile it to native using GraalVM?

I have you covered. You dont have to look anywhere else.

Scope

  • groovy code as part of quarkus application
  • statically compilable groovy code with no dynamic feature
  • compile the final application to native code using graalvm
Code example can be found here.

Steps

  1. Create a quarkus application as outlined here
  2. Add your groovy class under src/main/java
  3. Annotate your groovy class with @groovy.transform.CompileStatic
  4. create a file called application.properies under src/main/resources folder with the following content.

quarkus.native.additional-build-args=\
    --allow-incomplete-classpath, \
    --report-unsupported-elements-at-runtime, \
    --initialize-at-build-time, \
    --initialize-at-run-time=org.codehaus.groovy.control.XStreamUtils\\,groovy.grape.GrapeIvy, \
    --no-fallback

Build your native applicaton using,
./mvnw package -Pnative -Dquarkus.native.container-build=true
  

Voila! You groovy application is compiled to native code.

Comments

  1. I would like to use groovy with quarkus for a regular java application.

    I did the following steps

    1. Create a quarkus application as outlined here
    2. Add your groovy class under src/main/java
    3. Annotate your groovy class with @groovy.transform.CompileStatic

    My application worked, only the live reload of the groovy classes didn't.

    Will I have a problem? The quarkus documentation doesn't say anything about groovy

    ReplyDelete
  2. Yes, live reload will not work but it should not be a problem in production

    ReplyDelete

Post a Comment

Popular posts from this blog

Using Nginx as proxy server for Keycloak

I have used Keycloak  in its very early stage ( when it is was in 2.x version). But now it has come a long way (at this time of writing it is in 21.x) In this article let's configure Keycloak behind Nginx. Here are the points to consider.  If you want to configure Apache2 as a proxy server for your java application, please check  this article . We are going to use a domain name other than localhost Anything other than localhost will require Keycloak to run in production mode which requires SSL configurations etc. Or it requires a proxy server. Lets begin. Requirements Keycloak distribution Ubuntu 22.04 server Configuring Keycloak 1. Download Keycloak from here . 2. Extract it using tar -xvzf  keycloak-21.0.1.tar.gz 3. Create a script file called keycloak.sh with the following contents #!/bin/bash export KEYCLOAK_ADMIN=<admin-username-here> export KEYCLOAK_ADMIN_PASSWORD=<admin-password-here> nohup keycloak-21.0.0/bin/kc.sh start-dev --proxy edge --hos...

Installing GoDaddy certificate in Wildfly/Keycloak

In the previous post we saw how to set up Keycloak . Here we will see how to generate and install GoDaddy.com certificate in Keycloak. The steps are similar for Wildfly as well. Step 1: Generate CSR file Run the following commands in your terminal. <mydomain.com> has to be replaced with your actual domain name. keytool -genkey -alias mydomain_com -keyalg RSA -keysize 2048 -keystore mydomain_com.jks keytool -certreq -alias mydomain_com -file mydomain_com.csr -keystore mydomain_com.jks Step 2: Generate certificate Upload  mydomain_com . csr  file content into GoDaddy.com, generate and download certificate for tomcat server (steps to generating SSL certificate is beyond the scope of this article). If you unzip the file, you will see the following files. gd_bundle-g2-g1.crt ..5f8c...3a89.crt   #some file with alphanumeric name gdig2.crt Files 1 and 2 are of our interest. Third file is not required. Step 3: Import certificate to key...

Hibernate & Postgresql

If you are using Hibernate 3.5 or above to talk to Postgresql database, have you ever tried to store a byte array? Let's take an example. Here is the mapping which will store and read byte[] from the database. @Lob @Column(name = "image") private byte[] image; Here is the JPA mapping file configuration. <persistence version="2.0"  xmlns="http://java.sun.com/xml/ns/persistence"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">   <persistence-unit name="testPU" transaction-type="JTA">     <provider>org.hibernate.ejb.HibernatePersistence</provider>     <jta-data-source>test</jta-data-source>     <properties>     </properties>   </persistence-unit> </persistence> When you try to save your entity you will get t...

HTTP/2 Java Server using Embedded Jetty - Part 1

Have you ever wanted to configure internal Java servers using HTTP/2 (or H2 in short), because of the following advantages? Internal clients making concurrent calls, can now use a single connection by utilizing the multiplexing nature of HTTP/2. Server will/can use less threads as there are less connections overall. Servers can also push resources/data and can communicate using websockets But what if you don’t want to go through tedious process of setting up SSL/TLS certificates? HTTP/2 insists on SSL/TLS as no browser supports HTTP/2 on insecure mode. Or what if you want to save every bit of CPU and time by skipping encryption and decryption? You are at the right place. Since everyone nowadays is moving towards (or talking about) micro services, we will also configure Jetty HTTP/2 server in embedded mode. What more? We are also going to configure our server to use Asynchronous Servlets and Asynchronous IO (aka non-blocking IO or NIO) in Part-2 of this article. ...

How to retry a method call in Spring or Quarkus?

Have you ever come across a situation where you wanted to retry a method invocation automatically? Let's say you are calling a stock ticker service for a given stock and get a transient error. Since it is a transient error, you will try again and it may work in second attempt. But what if it doesn't? Well, you will try third time. But how many times can you try like that? More importantly after how much time will you retry? Imagine if you have a handful of methods like this. Your code will become convoluted with retry logic. Is there a better way? Well, if you are using spring/spring boot, you are in luck. Here is how you can do that using spring. Let's write our business service as follows. import java.time.LocalDateTime; import java.util.concurrent.CompletableFuture; import lombok.extern.slf4j.Slf4j; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Retryable; import org.springframework.scheduling.annotation.Async; import...