0% found this document useful (0 votes)
17 views29 pages

BCNJUG-Revolutionize Java DB AppDev With Reactive Streams and Virtual Threads

Java Virtual Threads Reactive Streams Oracle Database BigData
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views29 pages

BCNJUG-Revolutionize Java DB AppDev With Reactive Streams and Virtual Threads

Java Virtual Threads Reactive Streams Oracle Database BigData
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

Revolutionize Java Database

Application Development with Reactive


Streams and Virtual Threads

BarcelonaJUG
Juarez Barbosa Junior @juarezjunior
Sr. Principal Java Developer Evangelist
ORACLE

Copyright © 2023, Oracle and/or its affiliates


@juarezjunior
@juarezjunior Juarez Álvares Barbosa Junior
Senior Principal Java Developer Evangelist

• Coming from Dublin, Ireland


• Developer since 1995 (Java ☕️1997)
• 28 years of experience in SW Engineering &
DevRel
• Microsoft, Oracle, 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
• Java, Python, Cloud, DevOps, SRE, Cloud-native, IoT, AI,
Blockchain, Rust
• Speaker at conferences
• Oracle CloudWorld, Oracle Code, Microsoft Ignite, Microsoft
TechX, jPrime, JCON, DevConf.cz, GeeCon, DevOpsDays,
DeveloperWeek, DevOps Institute, CloudLand, DWX, The
Developer’s Conference (TDC), Sec4Dev, JSNation,
NodeConf, Conf42, Shift Conf, Global Azure, Open-Source
Lisbon, CodeFrenzy, Mêlée Numérique, React Summit,
Test.js Summit, Porto TechHub Conf, Pyjamas, JUGs,
meetups, etc.
Agenda
•Java App Dev with 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
•Live Labs/Free Oracle Cloud Account/Oracle ACE Program

Copyright © 2023, Oracle and/or its affiliates


Java App Dev with Oracle Database

Copyright © 2023, Oracle and/or its affiliates


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 critical in mission-critical apps

Copyright © 2023, Oracle and/or its affiliates


Overview of Oracle DB Access
with Java

Copyright © 2023, Oracle and/or its affiliates


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 I/O
• Oracle Reactive Streams Ingestion + Oracle R2DBC + Oracle JDBC Reactive Extensions (Flow)
• Libraries are available: Reactor, RxJava, Akka, Vert.x
Copyright © 2022, Oracle and/or its affiliates
Classic Java
Platform Threads


Database access with blocking threads

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
Copyright © 2023, Oracle and/or its affiliates
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

Copyright © 2022, Oracle and/or its affiliates


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).

Copyright © 2022, Oracle and/or its affiliates



JDK Enhancement Proposals
• JEP 425: Virtual Threads (Preview)

Virtual • JEP 436: Virtual Threads (Second Preview)


• JEP 444: Virtual Threads

Threads •
java.lang.Thread

- JEPs final Boolean - isVirtual()



static Thread.Builder.OfVirtual - ofVirtual()

425, •
static Thread.Builder.OfPlatform - ofPlatform()
static Thread - startVirtualThread(Runnable task)
436, 444


java.lang.Thread.Builder

java.util.concurrent.Executors

static ExecutorService -
newVirtualThreadPerTaskExecutor()

Copyright © 2023, Oracle and/or its affiliates


Virtual Threads - JEPs 425, 436, 444


java.lang.Thread.Builder

Thread defines a
Thread.Builder API for
creating and starting
both platform and virtual
threads. The following
are examples that use
the builder:

Copyright © 2022, Oracle and/or its affiliates


Demo # 1: Virtual vs Platform Threads

Virtual Threads vs Platform Threads

JEP 425 Virtual Threads (Preview)

JEP 436 (Second Preview)

JEP 444 (JDK 21)

Runs on Java 19, 20, 21

javac --release 19 --enable-preview

java --enable-preview

Oracle JDBC Driver instrumented to support Virtual Threads

Verifiable comparison of OS/HW resources consumption (Platform Threads x
Virtual Threads)

Copyright © 2022, Oracle and/or its affiliates


Reactive JDBC - Sync vs Async JDBC

Synchronous JDBC Setup Setup Setup

Blocking Blocking Blocking


Database
Handle Result Handle Result Handle Result

Reactive JDBC Setup Setup Setup

Non-Blocking Non-Blocking Non-Blocking


Database

Handle Result Handle Result Handle Result

Copyright © 2023, Oracle and/or its affiliates


Reactive Streams Ingestion (RSI)

Java Library for Reactive Streams Ingestion

Streaming capability: Ingest data in an unblocking, and
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 I/O

Fastest insert method for the Oracle Database through
Direct Path Insert, bypassing SQL and writing directly into the DB files

Copyright © 2022, Oracle and/or its affiliates


Demo #2: RSI - Architecture

Copyright © 2023, Oracle and/or its affiliates


Demo #2: RSI - Project Structure

Copyright © 2023, Oracle and/or its affiliates


Demo # 2: Reactive Streams Ingestion (RSI)
RSI Runtime: Non-
blocking, optimized Build
Push
Microservices
Docker Pull images
library for streaming images to from
Test
data through Direct Define Registry Registry
Path, Shard & RAC/FAN build for Push
support. CI/CD Cloud Container
HTTP / REST Engine over toolchain Infrastruct Engine for
Container Kubernetes
Helidon ure
Pipelines,
Registry
gRPC / AMQP / MQTT Jenkins, Deploy
Engines etc. images to
production

Record JDBC
HTTP / REST Streaming
over Containers Direct Path
IoT Devices /

AMQP multiple running INSERT


protocols. microservices
Apps

deployed over ATP, ADW, ATP-D,


cvgRPC Kubernetes AFDW-D
Memoptimized
MQTT Kubernetes Rowstore
worker
Files / Logs nodes

ORACLE CLOUD INFRASTRUCTURE

Copyright © 2023, Oracle and/or its affiliates


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.1.1 (2023), Apache v2.0, OSS
Copyright © 2022, Oracle and/or its affiliates
Demo # 3: Oracle R2DBC

static String queryJdbc(java.sql.Connection connection) throws SQLException { static Publisher<String> queryR2dbc(io.r2dbc.spi.Connection connection) {

try (java.sql.Statement statement = connection.createStatement()) { return Flux.from(connection.createStatement(


ResultSet resultSet = "SELECT * FROM CUSTOMERS")
statement.executeQuery("SELECT * FROM CUSTOMERS"); .execute())
.flatMap(result ->
if (resultSet.next()) result.map(row -> row.get(0, String.class)))
return resultSet.getString(1); .switchIfEmpty(Flux.error(
else new NoSuchElementException("Query returned zero rows")));
throw new NoSuchElementException("Query returned zero rows");
} }

Copyright © 2023, Oracle and/or its affiliates


Virtual Threads
or Reactive?
•Oracle JDBC supports both!

•Want Virtual Threads?


• The Oracle JDBC driver has been
“Virtual Thread Compatible” since
21c

•Want Reactive?
• Oracle R2DBC 1.1.1 is available now
• Consume Flow interfaces directly
from Oracle JDBC’s Reactive
Extensions
Copyright © 2023, Oracle and/or its affiliates
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
(locks, atomics, queues)
• Now available in JDK 21

•Limitations of Virtual Threads:


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

Copyright © 2023, Oracle and/or its affiliates


References
• JDK 21
• The Arrival of JDK 21 - https://blogs.oracle.com/java/post/the-arrival-of-java-21
• Project Loom / Virtual Threads
• Loom - https://openjdk.org/projects/loom/
• JEP 444 Virtual Threads (Targeted) - https://openjdk.org/jeps/444
• Introduction to Oracle JDBC 21c Driver Support for Virtual Threads -
https://bit.ly/3UlNJWP
• Reactive Streams Ingestion Library
• Getting Started with the Java library for Reactive Streams Ingestion (RSI) -
https://bit.ly/3rEiRnC
• High-throughput stream processing with the Java Library for Reactive Streams
Ingestion (RSI), Virtual Threads, and the Oracle ATP Database - https://bit.ly/3rATCTd
• RSI - https://docs.oracle.com/en/database/oracle/
• R2DBC
• Oracle R2DBC Driver – https://github.com/oracle/oracle-r2dbc
• Develop Java applications with Oracle Database
• JDBC – https://www.oracle.com/database/technologies/appdev/jdbc.html

Copyright © 2023, Oracle and/or its affiliates


Oracle http://oracle.com/23cFree

• Oracle Database 23c accelerates

Database
Oracle’s mission to make it simple
to develop and run all data-driven
apps

23 c
• Provides developers easy early
access to 23c app dev features

• Limited database size, memory,


CPU threads (same limits as
Express
Oracle Edition)
Database 23c Free – Developer
Edition available for download now
Free Developer
Release
http://oracle.com/23cFree
Oracle LiveLabs 500+
free
Showcasing how Oracle’s solutions workshops,
can solve your business problems available or
in
development
3.5 million
people have already visited
LiveLabs

600+
events run
using
LiveLabs
workshops
developer.oracle.com/livelabs
learn something new …at your pace!
500+ technical experts &
community leaders helping peers
globally

The Oracle ACE Program recognizes & rewards individuals for


their technical & community contributions to the Oracle community

3 membership tiers

Nominate
yourself or a candidate:
ace.oracle.com/nominate
Learn more -
ace.oracle.com

Connect: @oracleace
[email protected] facebook.com/OracleACEs blogs.oracle.com/ace
Create your FREE
Cloud Account

• Go to
https://signup.cloud.oracle.com/

Copyright © 2023, Oracle and/or its affiliates


Juarez Junior
@juarezjunior

You might also like