Quarkus Club_Revolutionize Java Database App Development with Reactive Streams and Java Virtual Threads

juarezjunior 16 views 29 slides Mar 11, 2025
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

Quarkus Club_Revolutionize Java Database App Development with Reactive Streams and Java Virtual Threads


Slide Content

i

ORACLE ai
uns 23 CES
Revolutionize Java Database App

Development with Reactive Streams and
Java Virtual Threads

Quarkus Club

Juarez Barbosa Junior @juarezjunior
Senior Principal Java Developer Evangelist

ORACLE

[9] ojuarezjunior Juarez Barbosa Junior
Senior Principal Java Developer Evangelist @ Oracle

+ Speaking from Dublin, Ireland

+ 28 years of experience in SW Engineering & DevRel
+ Oracle, Microsoft, IBM, Nokia, Unisys, Accenture, startups
+ Microsoft Azure Developer Relations Lead
+ IBM Watson Tech Evangelist & Cloud Rockstar
+ IBM Mobile Tech Evangelist & Global Thought Leader
+ Nokia Developers Global Champion
. um Python, Cloud, DevOps, SRE, Cloud-native, loT, Al, Blockchain,
ist
+ Speaker at conferences

+ Oracle CloudWorld, Oracle Code, Microsoft Ignite & TechX, ¡Prime,
JCON, DevConf.cz, GeeCon, DevOpsDays, DeveloperWeek, DevOps
Institute, CloudLand, DWX, The Developer's Conference (TDC),
Sec4Dey, JSNation, NodeConf, Conf42, Shift Conf, Global Azure,
Open-Source Lisbon, CodeFrenzy, Mélée Numérique, React Summit,
Tests Summit, Porto TechHub Conf, Pyjamas, MiTechCon, Data
Science Summit, OpenSourceNorth, WeAreDevelopers, Global
Software Architecture Summit, JavaCro, JUGs, OUGs, GDGs,

meetups, hackathons, and customer engagements.

Confidential - Oracle Restricted

Confidential Oracle Restricted

Agenda

Java App Dev with the Oracle Database

Oracle JDBC - Support for the Latest Java Versions
Overview of Oracle DB Access with Java

Oracle JDBC - Sync and Async

Classic Java Platform Threads

Project Loom - Virtual Threads

Virtual Threads - JEPs 425, 436, 444

Demo # 1: Virtual vs Platform Threads

Reactive JDBC - Synchronous vs Asynchronous JDBC
Reactive Streams Ingestion (RSI)

Demo # 2: Reactive Streams Ingestion (RSI)

From Sync to Reactive JDBC: Oracle R2DBC

Demo # 3: Oracle R2DBC with Project Reactor

te ©

Grave oca Koon Java App
paa Dev with
Oracle
Database

— Gpgo- 28eecs

Zo

Overview of Oracle DB Access with Java

operators (map, reduce, filters),
concurrency modeling,

monitoring, tracing, JavaucFlow Oracle JDBC Driver

User Java code + JDBC

Full Reactive Streams.

Oracle Database

Copyright © 2024, Oracle

Confidential - Oracle Restricted

Oracle JDBC - Support for the Latest Java Versions

+ Java 11 - native support, compiled with it

+ Java 17 - certified

+ JDBC Standards - 4.2 and 4.3

+ GraalVM - native image instrumentation

+ Reactive Streams - Java Flow API support

+ Project Loom - Virtual Threads support

+ Data access is crucial in mission-critical apps

E...

Copyright © 2024, Oracle and/or safiiates
confidential - Oracle RESTES #

Java 22 is now available

Oracle JDBC Reactive Extensions 3
(Flow API)

SQL Execution (OraclePreparedStatement):
Publisher<Boolean> executeAsyncOracle()
Publisher<Long> executeUpdateAsyncOracle()
Publisher<Long> executeBatchAsyncOracle()
Publisher<OracleResultset>
executeQueryAsyncOracle()

Comeactton Creation: Introduction to JDBC Reactive

(OracleConnectionBuilder): Row Fetching (OracleResultSet): mere era

Publisher<OracleConnection> Publisher<T> Extensions with the Oracle

buildConnectionPublisherOracle() publisherdracle(Function<OracleRow, T> f) Database 23c Free — Developer

Database 23c Free — Developer

LOB 1/0 (OracleBlob):

'comectian cronos Publisher<byte[ ]> publisherOracle(long Release

(OracleConnection): position)

Publisher<Success>

ramen sie ie iio

Transaction Closing 108 1/0 (Oractectob):

(OracLeConnection): ines IES

Publishen<Success» e eee

conmitAsyncOracle() Subscriber<String> subscriberOracle(long

Publisher<Success>

rolIbackAsyncOracle() — JDBC

Oracle JDBC — Async and Sync

- Project Loom - Virtual Threads

+ Synchronous database access with lightweight threads

+ Standard JDBC + Virtual Threads

+ Client application call stack may use blocking, synchronous code

+ Libraries must be compatible with Virtual Threads

+ Oracle instrumented the Oracle JDBC driver to support Virtual Threads
. Reactive Programming

+ Asynchronous database access with non-blocking network 1/0

+ Oracle Reactive Streams Ingestion (RSI) + Oracle R2DBC + Oracle JDBC Reactive
Extensions (Flow)

+ Reactive Streams Libraries: Reactor, RxJava, Akka, Vert.x

Classic Java Platform Threads

o> m

thread

. Database access with blocking threads progam sa]

+ A JDBC call blocks a thread for 100s of milliseconds
(thread-per-request style)

+ Thread count increases linearly with the number
of JDBC calls

+ Performance degrades as the number of threads
increases

1 MB of stack memory per thread typically

+ Scheduling many platform threads is expensive

Preemptive scheduling vs time-slicing

Virtual Threads - JEPs 425, 436, 444

- Virtual Threads — JEPs (JDK Enhancement Proposals)

. Lightweight (user mode) threads that dramatically reduce the effort of writing,
maintaining, and observing high-throughput concurrent applications

+ Enable applications written in the simple thread-per-request style to scale with near-
optimal hardware utilization

+ Enable easy troubleshooting, debugging, and profiling of virtual threads with existing JDK
tools

+ Do not remove the traditional implementation of threads
+ Do not alter the basic concurrency model of Java

. Enable existing code that uses Java threads (java.lang.Thread) to adopt virtual threads with
minimal change

Virtual Threads - JEPs 425, 436, 444

- Virtual Threads — JEPs (JDK Enhancement Proposals)
+ Virtual threads typically employ a small set of platform threads used as carrier threads
+ Code executing in a virtual thread is not aware of the underlying carrier thread
+ The currentThread() method will always return the Thread object for the virtual thread

+ Virtual threads do not have a thread name by default. The getName() method returns the
empty string if a thread name is not set

+ Virtual threads have a fixed thread priority that cannot be changed

+ Virtual threads are daemon threads so do not prevent the shutdown sequence from
beginning (low-priority ones).

- JDK Enhancement Proposals
+ JEP 425: Virtual Threads (Preview)

+ JEP 436: Virtual Threads (Second Preview)
+ JEP 444: Virtual Threads

Virtual | iii

+ final Boolean - isVirtual
Th rea ds ind + static Thread.Builder.OfVirtual - ofVirtual
J E P S 42 5 + static Thread.Builder.OfPlatform - ofPlatform
a . static Thread - startVirtualThread(Runnable task
436, 444 - java.lang.Thread.Builder
- java.util.concurrent.Executors

- static ExecutorService -
newVirtualThreadPerTaskExecutor()

Virtual Threads - JEPs 425, 436, 444

Runnable runnable =

// Start a daemon thread to run a task

Thread thread = Thread.ofPlatform().daemon().start(runnable);
+ java.lang.Thread.Builder

// Create an unstarted thread with name "duke", its start() method R

1 must be invoked to schedule it to execute. + Thread defines a

Thread thread = Thread.ofPlatform().name("duke") .unstarted(runnable); Thread.Builder API for

creating and starting
// A ThreadFactory that creates daemon threads named “worker-0", "worker-1", ... both platform and virtual
ThreadFactory factory = Thread. ofPlatform() .daemon().name("worker-", 0) .factory(); threads. The following

// Start a virtual thread to run a task are examples that use
Thread thread = Thread.ofVirtual().start(runnable); the builder:

// A ThreadFactory that creates virtual threads
ThreadFactory factory = Thread.ofVirtual().factory();

Copyright © 2024, Oracle and/or its affiliates

Confidential - Oracle Restricted

Demo # 1: Virtual vs Platform Threads

OSThread | OSThread | OSThread | OS Thread
+ Virtual Threads vs Platform Threads
JavaPlatform — JavaPlatiorm — JavaPlatiorm Java Platform
« JEP 425 Virtual Threads (Preview) Thread Thread Thread Thread
VI VI VI VI VI VI VI VI VI VI VI VI VI vr vr vr
« JEP 436 (Second Preview)
+» JEP 444 (JDK 21)

+ Oracle JDBC Driver instrumented to support Virtual Threads

*VT- Virtual Thread

« Intrinsic locks replaced with ReentrantLock to avoid thread pinning
+ Verifiable comparison of OS/HW resources consumption cig
(Platform Threads versus Virtual Threads)

« Introduction to Oracle JDBC Driver Support for Virtual Threads

Confidential - Oracle Rested

Setup

Handle Result

==.

Setup

Non-Blocking

Handle Result

Oracle Restricted

Reactive Streams Ingestion (RSI)

Java Library for Reactive Streams Ingestion
+ Streaming capability: Ingest data in an unblocking, and Prem
reactive way from a large group of clients
+ Group records through RAC (Real App Clusters),
and Shard affinity using native UCP (Universal Connection
Pool)

+ Optimize CPU allocation while decoupling record

Processing from |/O

« Fastest insert method for the Oracle Database through

Direct Path Insert, bypassing SQL and writing directly into the DB files

Confidential - Oracle Rested

Confidential - Oracle Restricted

Demo #2: RSI - Architecture

Listener Application

Topic Producer — Topic Subscriber
on y Y
rr =} 2 Stomp
cak> m os]
e ActiveMQ $:
© = (Direct path insert) ml
Dataset

Oracle Database

confidential -Orac

Confidential - Oracle Restricted

Demo #2: RSI - Project Structure

v 53 rsi-example
v 2 src/main/java
y @ rsiexample.amgp
D Listenerjava
y @ rsiexample.common
D AuthHeadersRequestFilter.
D MessageProducer java
D Retailerjava
D RSIServicejava
v 8 rsiexample.matt
à Listenerjava
v 8 rsi.example.stomp
D Listenerjava
añ JRE System Library (jdk-19]
zà Referenced Libraries
& src
& target

ass 8 pomxml

ce RESTES

Demo # 2: Reactive Streams Ingestion (RSI)

Memoptimized
Kubernetes Rowstore
worker nodes
ORACLE CLOUD INFRASTRUCTURI

51 Runtime: = Push Docker i

2 blocking, images to Bi ier h
8 streaming data E Registry rom Registry (= !
3 through Direct Path dard tr O ET :
E toolchain Cloud Container !
E Córtalner Infrastructur Enginefor |
3 one ‘ubernetes |
= Pipelines, e Registry I
Jenkins, etc. — i

images to :

production :

Record JDBC :

Y Streaming — :

E over Containers Direct Path == H

< Se ne INSERT

3 deployed over ATP, Fous ATP-D;

> Kubernetes im :

3 :

5 :

2 H

Confidential

cle Restricted

— — R2DBC - Reactive Relational DB Connectivity

From Sync to Reactive JDBC: Oracle
R2DBC
Oracle Reactive Relational Database Connectivity (R2DBC)

Oracle R2DBC Driver is a Java library that supports reactive
programming with the Oracle Database

It implements the R2DBC Service Provider Interface (SPI) as
specified by the Reactive Relational Database Connectivity
(R2DBC) spec

+ The R2DBC SPI exposes Reactive Streams as an abstraction
for remote database operations

+ The sample code uses Project Reactor. It could use RxJava,
Akka, or any Reactive Streams library

Runs on Java 11+

Oracle RDBC 1.2.0, Apache v2.0 (OSS)

cle Restrictes

Oracle R2DBC
driver

JDBC-thin 21c
Async APIs

TTC / sqinet

_——— |

Oracle RDBMS 19c
or above

N 1a

=R2DBC

- Oracle Restricted

Demo # 3: Oracle R2DBC

static String queryldbc(java.sql.Connection connection) throws static Publisher<String> queryR2dbe(io.r2dbe.spi Connection
SQLException { connection) {
try (javassql.Statement statement = connection.createStatement()) { return Flux. from(connection.createStatement(
ResultSet resultSet = "SELECT * FROM CUSTOMERS")
statement.executeQuery("SELECT + FROM CUSTOMERS") 5 «execute())
sflatMap(result ->
if (resultSet.next()) result.map(row => rowsget(9, String.class)))
return resultSet.getString(1); «switehIfEmpty (Flux.error(
else new NoSuchElementException("Query returned zero rows")))$
throw new NoSuchElementException("Query returned zero rows"
3 3
3

R2DBC

<confidentil- Oracle Restncted

Virtual Threads or Reactive?

+ Oracle JDBC supports both!

+ Want Virtual Threads?
The Oracle JDBC driver is Virtual Thread compatible
Pipelined Database Operations

Want Reactive?
Oracle R2DBC, Oracle RSI, Oracle JDBC Reactive Extensions
+ Pipelined Database Operations

Virtual Threads or Reactive?

+ Benefits of Virtual Threads: Benefits of Reactive:

+ Easier to read and write + Reactive Libraries (Reactor, RxJava, Akka, Vert.x)
+ Easier to debug + Stream-like API with a functional style

+ Integration with JDK tools + Low-level concurrency is handled for you

+ Do not alter the basic concurrency model of Java (IoCkS, atomics, queues)

+ Now available in JDK 21 / 22

+ Limitations of Virtual Threads: Limitations of Reactive:
+ Some libraries/frameworks are not + Steep learning curve
compatible yet + Harder to read and write

+ Harder to debug

Confidential - Oracle Restricted

Technical References
+ JDK22

+ The Arrival of JDK 22 - https://blogs.oracle.com/java/post/the-arrival-of-java-22
+ Virtual Threads
+ JEP 444 Virtual Threads - https://openjdk.org/jeps/444
+ JEP 480 Structured Concurrency (Third Preview) - https://openjdk.org/jeps/480
+ Introduction to Oracle JDBC Driver Support for Virtual Threads - https://bit.ly/3UINJWP.
+ Reactive Streams Ingestion Library
+ Getting Started with the Java library for Reactive Streams Ingestion (RSI) - https: / /bit.ly/3rEiRnC
+ AHigh-Speed Data Ingestion Solution in Java Using MQTT, AMQP, and STOMP -
https://medium.com/oracledevs/a-high-speed-data-ingestion-microservice-in-java-using-matt-
amap-and-stomp-135724223ae1
+ R2DBC
+ Getting Started with Reactive Relational Database Connectivity and the Oracle R2DBC Driver —
http://rb.gy/95u5f8
+ Pipelined Database Operations - https: //rb.gy/k57g3r
+ Generative Al Service - Oracle Cloud Infrastructure (OCI) - https: //rb.gy/a9k69e
+ What's in Oracle Database 23ai for Java Developers?
+ https://www.oracle.com/a/tech/docs/database/whats-in-oracledb23ai-for-java-developers.pdf

Confidential - Oracle Restricted

Juarez Junior

Y | @juarezjunior

ORACLE

Oracle LiveLabs

Showcasing how Oracle's solutions can
solve your business problems

500+

free workshops,
available or in
development

people have already visited
LiveLabs

600+

events run
using LiveLabs
workshops

developer.oracle.com/livelabs
learn something new ...at your pace!

Qos

Oracle ACE Program

3 membership tiers à Oracle ACE

Oracle ACE à Oracle ACE Oracle ACE ás
Director Pro Associate Nominate

yourself or a candidate:

ace.oracle.com/nominate
Learn more - ace.oracle.com

Connect: EP [email protected] E @oracleace facebook.com/OracleACEs — [8] blogs.oracle.com/ace

Create your FREE
Cloud Account

* Goto

https://signup.cloud.oracle.com/

ORACLE
Cloud Infrastructure

de

~—_

Oracle Cloud Free Tier

Get started with.

Terms of Use