Code Shrinking with Quarkus and Panache ORM

With Panache ORM and Quarkus a JSON-B / JPA entity:


package workshops;

import java.time.LocalDate;
import javax.persistence.Entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;

@Entity
public class Workshop extends PanacheEntity{

    public String name;
    public LocalDate date;
}    
can be directly exposed via a JAX-RS resource:

package workshops;

import java.util.List;

import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@Path("/workshops")
public class WorkshopsResource {

    @GET
    public List<Workshop> workshops() {
        return Workshop.listAll();
    }

    @POST
    @Transactional
    @Consumes(MediaType.APPLICATION_JSON)
    public void save(Workshop workshop) {
        workshop.persist();
    }
}    

without any direct reference to an EntityManager or declaration of persistence.xml.

The data source is configured via MicroProfile configuration:


quarkus.datasource.url = jdbc:postgresql://localhost:5432/postgres
quarkus.datasource.driver = org.postgresql.Driver
quarkus.datasource.username = airhacks
quarkus.datasource.password = airhacks
quarkus.hibernate-orm.database.generation = drop-and-create
See it in action (and from scratch):

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

What are: ThinWARs, HollowJARs, SkimmedJARs, FatJARs, UeberJARs, FatWARs and Executable Images?

ThinWAR
contains application code only. The runtime is strictly separated. "Hello,World" is about 4kB and takes advantage of Docker layering. The WAR is deployed to a (running) server.
HollowJAR
deployment comprises a single runtime JAR and an application "Thin"JAR (sometimes a WAR). The ThinJAR contains application code and it is going to be deployed at startup of the runtime. The ThinJAR (could be even a WAR) is as small as a ThinWAR and it also takes advantage of Docker layering.
SkimmedJAR (inversed HollowJAR)
the application JAR is executable and comes with a minimalistic bootstrap mechanism. The SkimmedJARs are larger: ~50-100kB than the strict "Thin*" variants. The immutable runtime JARs are strictly separated from application code. The SkimmedJAR has to be deployed on every "commit", the runtime remains untouched. There is no deployment phase, the runtime and the application are considered at runtime as a single unit.

In container environments the application JAR ships in the upper most container layer the infrastructure JARs in a layer below.

FatJAR, UeberJAR
The application code and the runtime are not separated. Container image layering is not possible - everything ships at once. a "Hello,World" comes with a few MBs. FatJARs are perfectly suited for bare metal environments (without containers like e.g. Docker) and CLIs (like e.g. wad.sh, jwtenizr.sh). There is no deployment phase, there is only a single JAR.
FatWAR
Infrastructural services like e.g. Jenkins ship as executable WARs. (java -jar jenkins.war). Ease of installation is the main motivation. Container layering is not possible - everything ships at once.
Executable Image
Runtimes like GraalVM allow "Ahead of Time" compilation of Java applications. The AOT process results in an OS-specific, executable binary file, which contains the JVM, the runtime and the application code.

The executable image contains the JVM, entire runtime and the application code, so it does not take advantage of container layering. However the executable file is significantly smaller than the sum of all docker layers in a "traditional" Java deployment.

The smaller the deliverable (JAR, WAR) affected by a "commit", the less artefacts have to be built, packaged, (security) scanned, processed and uploaded to clouds.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Helidon, FatJARs, ThinWARs, HollowJARs, MicroProfile, Jakarta EE and GraalVM--airhacks.fm Podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #58 airhacks.fm episode with Tomas Lager (@tomas_langer) about:

Helidon, FatJARs, ThinWARs, HollowJARs, MicroProfile, Jakarta EE and GraalVM
is available for download.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

The Simplest Possible Web Component (CustomElement)

A WebComponent (CustomElement) is an ES 6 class:


class HelloWorld extends HTMLElement { 
    connectedCallback() { 
        const message = "world";
        this.innerText = `
            hello, ${message}
        `;
    }
}
customElements.define('hello-world',HelloWorld);    

which renders itself after including in a "html" page:


<!DOCTYPE html>
<html>
<body>
    <hello-world></hello-world>
    <script src="HelloWorld.js"></script>
</body>
</html>    

WebComponents are supported in all recent browsers.

See you at "Build to last" effectively progressive applications with webstandards only -- the "no frameworks, no migrations" approach, at Munich Airport, Terminal 2 or effectiveweb.training (online).


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Custom Map Updates without Null Checks: Map#merge

The Java 1.8+ Map#merge method is useful for upserts of a Map with custom bevavior. Null checks are not required:


import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import org.junit.Test;

public class MapMergeTest {

    @Test
    public void mergeInMap() {
        Map<String, Integer> map = new HashMap<>();
        int initial = 1;
	
        int result = map.merge("key", initial, (oldValue, newValue) -> oldValue + newValue);
        assertThat(result, is(1));

        int update = 42;
        int expected = initial + update;
        result = map.merge("key", update, (oldValue, newValue) -> oldValue + newValue);
        assertThat(result, is(expected));
    }
}

The Map#merge is equivalent to:


if (oldValue != null ) {
    if (newValue != null)
        map.put(key, newValue);
    else
        map.remove(key);
    } else {
    if (newValue != null)
        map.put(key, newValue);
    else
        return null;
    }    

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Installing and Deploying Swagger UI

The following dependency:


<dependency>
    <groupId>org.microprofile-ext.openapi-ext</groupId>
    <artifactId>swagger-ui</artifactId>
    <version>1.0.2</version>
</dependency>

installs the Swagger UI "website" and makes it available directly from a configurable URI (in our case: http://localhost:8080/openapiui/resources/openapi-ui/).

The example was deployed with wad.sh to payara.fish. Warning: most application servers / runtimes already ship with Swagger UI.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Threads, Transactions, EntityManager, Fluid Logic,Quarkus, AMQP and Jakarta EE -- the 67th airhacks.tv

The 67th airhacks.tv episode covering:

"Jakarta EE without Docker and FatJARs, Fulltext Search, Connection Pools, Password Management, Fluid Logic, Thread-Safety and EntityManager, Quarkus productivity, AMQP, Jakarta EE"

...is available:

Any questions left? Ask now: https://gist.github.com/AdamBien/2735e9c8845fe1eba40720281d9c2c09 and get the answers at the next airhacks.tv.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Change Data Capture, Debezium, Streaming and Kafka--airhacks.fm Podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #58 airhacks.fm episode with Gunnar Morling (@gunnarmorling) about:

Change Data Capture with Debezium, Streaming, Kafka and Use Cases
is available for download.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Jakarta EE without Docker, Fulltext Search, Connection Pools, Passwords, Fluid Logic, Thread-Safety and EntityManager--or 67th airhacks.tv

Topics for 67th airhacks.tv episode (https://gist.github.com/AdamBien/1a227df3f1701e4a12a751d3f7d1633e):

  1. Quarkus JSF
  2. The best approach to deploy Jakarta EE applications without Docker / containers
  3. Fulltext search with JPA and EclipseLink
  4. Integration testing and databases
  5. Configuring the number of DB connections in a connection pool
  6. Quarkus datasource configuration
  7. Thoughts on "Fluid Logic" pattern implementation
  8. EntityManager: transactions vs. thread-safeness
  9. Integration tests with JPA and auto-registration
  10. Jakarta EE and Java EE specifications
  11. JAX-RS and the added value of @Stateless
  12. The easy setup of Jakarta EE

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Jason's Binding and Fast, Greek Birds--airhacks.fm Podcast

Subscribe to airhacks.fm podcast via: spotify| iTunes| RSS

The #56 airhacks.fm episode with Dmitry Kornilov (@m0mus) about:

JPA-RS, EclipseLink, JSON-B and the road to helidon.io
is available for download.

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.


Web Apps, SPA, PWA with vanilla Java Script (ES 6+), CSS 3 and WebStandards only. As simple as possible, but not simpler. See you at: (Progressive) Web apps, Single Page Apps and WebStandards airhacks workshops at MUC airport, Winter Edition

airhacks.fm the podcast:

Stay in touch: airhacks.news.

Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License