0% found this document useful (0 votes)
16 views

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

Java Virtual Threads Reactive Streams Oracle Cloud Database
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

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

Java Virtual Threads Reactive Streams Oracle Cloud Database
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

JUAREZ

BARBOSA JR
Oracle

Revolutionize Java Database App


Development with Reactive Streams
and Virtual Threads

27-31 MARCA 2023 ONLINE


Agenda
•Java App Dev with Oracle Database
•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 - JEP 425 (Preview API)
•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
•Live Labs/Free Oracle Cloud Account/Oracle ACE Program

Copyright © 2022, Oracle and/or its affiliates


Java App Dev with Oracle Database

Copyright © 2022, Oracle and/or its affiliates


Support for the Latest Java Versions
•Java 11 - native support, compiled with it
•Java 17 - certified
•JDBC Standards - 4.2 and 4.3
•JMS 1.1 - latest is 2.0
•GraalVM - native image instrumentation
•Reactive Streams - Java Flow API support
•Project Loom - Virtual Threads support
•Data access is critical in mission-critical apps

Copyright © 2022, Oracle and/or its affiliates


Overview of Oracle DB Access with Java
operators (map, reduce, filters), Implements Java SE
concurrency modeling, reactive stream
Oracle JDBC
monitoring, tracing interface (Flow) driver
User Java code

R2DBC
+
Full Reactive 3rd party JDBC
Streams Reactive Reactiv
Streams Oracle
Java User
Libraries
e
Extensio Standar Database
Java d JDBC
Business Code n
API
Async call with non-blocking
Logic backpressure

Sync/blocking JDBC calls


VTs/lightweight JDBC calls

Copyright © 2022, Oracle and/or its affiliates


Oracle JDBC – Sync and Async

Reactive Programming
 Asynchronous database access with non-blocking network I/O
 Oracle R2DBC + Oracle JDBC Reactive Extensions + Oracle Reactive
Streams Ingestion
 Application call stack must be fully asynchronous
 Libraries are here to support it: Reactor, RxJava, Akka, Vert.x

Project Loom/Virtual Threads
 Synchronous database access with lightweight threads
 Standard JDBC + Virtual Threads
 Client application call stack may use conventional (synchronous) code
 Libraries must be compatible with Virtual Threads
 Oracle instrumented the Oracle JDBC 21c driver to support Virtual Threads
Copyright © 2022, Oracle and/or its affiliates
Classic Java Platform Threads

Blocking Threads

A JDBC call blocks a thread for 100’s 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

Scheduling many platform threads is expensive

Preemptive scheduling vs time-slicing

Copyright © 2022, Oracle and/or its affiliates


Virtual Threads - JEP 425 (Preview API)

Virtual Threads

Lightweight 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 existing code that uses the java.lang.Thread API to adopt virtual
threads with minimal change

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

Copyright © 2022, Oracle and/or its affiliates


Demo # 1: Virtual vs Platform Threads

Virtual Threads versus Platform Threads

JEP 425 Virtual Threads (Project)

Runs on Java 19 (Preview API)

javac --release 19 --enable-preview

java --enable-preview

Comparison of OS resources consumption (Threads only + JDBC)

Oracle JDBC Driver 21c instrumented to support Virtual Threads

API is Oracle JDBC with the Java SE library (Executor, ThreadFactory)

Copyright © 2022, Oracle and/or its affiliates


Reactive JDBC - Synchronous vs Asynchronous 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 © 2022, 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: 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 © 2022, Oracle and/or its affiliates


From Sync to Reactive JDBC: 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 © 2022, Oracle and/or its affiliates


Demo # 3: Oracle R2DBC

Oracle Reactive Relational Database Connectivity (R2DBC)

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

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

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 RS library

Runs on Java 11+

Copyright © 2022, Oracle and/or its affiliates


Virtual Threads
or Reactive?
•Oracle JDBC supports both!

•Want Virtual Threads?


• Oracle JDBC has been “Virtual Thread
Compatible” since 21.1.0.0

•Want Reactive?
• Oracle R2DBC 1.0.0 is available now
• Consume Flow interfaces directly from
Oracle JDBC’s Reactive Extensions

Copyright © 2022, Oracle and/or its affiliates


Virtual Threads or Reactive?
•Benefits of Virtual Threads: Benefits of Reactive:
• Easier to read and write • Available now. Supported on JDK 11
• Easier to debug • Reactive Libraries (Reactor, RxJava, Akka,
• Integration with JDK tools Vert.x)
• Stream-like API with a functional style
• Low-level concurrency is handled for you
(locks, atomics, queues).

•Limitations of Virtual Threads: Limitations of Reactive:


• Still a preview feature in JDK 19 • Steep learning curve
• Some libraries are not compatible • Harder to read and write
• Harder to debug
Copyright © 2022, Oracle and/or its affiliates
References
• JDK 19 / Project Loom
• JDK 19 - https://openjdk.org/projects/jdk/19/
• Loom - https://openjdk.org/projects/loom/
• JEP 425 Virtual Threads (Preview) - https://bugs.openjdk.org/browse/JDK-8277131
• 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 © 2022, Oracle and/or its affiliates
About me
•Juarez Barbosa Junior - @juarezjunior
• Senior Principal Java Developer Evangelist
• 27 years of experience
• SW Engineering, Developer Relations
• Microsoft, Oracle, IBM, Nokia, Unisys, Accenture,
and a few startups
• Microsoft Azure Developer Relations Lead
• IBM Watson Tech Evangelist & Cloud Rockstar
• IBM Mobile Tech Evangelist & Global Thought
Leader
• Nokia Developers Global Champion
• Lead Software/DevOps Architect
• Expertise
• Java, Cloud, DevOps, Cloud-native, Blockchain
Copyright © 2022, Oracle and/or its affiliates
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!
Create your FREE
Cloud Account

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

Copyright © 2022, Oracle and/or its affiliates


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
Thank you!

Copyright © 2022, Oracle and/or its affiliates | Confidential: Internal


22

You might also like