Jakarta Concurrency:
Present and Future
Steve Millidge
Agenda
• Why do we need Jakarta Concurrency?
• What are the main components?
• What’s new in Jakarta EE 10?
• Futures
• Get Involved.
3
Why do we need
Jakarta Concurrency?
6
What is Jakarta Concurrency
New name for JSR236: Concurrency Utilities for Java EE
• Introduced in Java EE 7
• No change in Java EE 8
• No change in Jakarta EE 8
• Namespace change in Jakarta EE 9
• New Features Jakarta EE 10
New Maven coordinates in Jakarta EE 8/9/10
<dependency>
<groupId>jakarta.enterprise.concurrent</groupId>
<artifactId>jakarta.enterprise.concurrent-api</artifactId>
<version>1.1.2 (8) 2.0 (9.x) 3.0.2 (10.0)</version>
</dependency>
Steve Millidge
7
Goal
• Utilize existing applicable Java EE platform services. Provide a simple yet
flexible API for application component providers to design applications
using concurrency design principles.
• Allow Java SE developers a simple migration path to the Java EE
platform by providing consistency between the Java SE and Java EE
platforms.
• Allow application component providers to easily add concurrency to
existing Java EE applications.
• Support simple (common) and advanced concurrency patterns without
sacrificing usability
What is an
Application
Server?
9
What is the Use Case?
Adding an Asynch Break in your application
(Before JSR 236)
Many older Java EE Applications
use JMS just for this!
10
What is the Use Case?
Adding an Asynch Break in your application
(Jakarta Concurrency)
Much more lightweight than a
JMS Broker!!!
Show me the Code
(Simple Asynch Break – pre JakartaEE 10)
What is the Use Case?
Running Tasks in Parallel
(Jakarta Concurrency)
Show me the Code
Parallel Tasks
Running Periodic Tasks
(Jakarta Concurrency)
What is the Use Case?
Before Java EE Concurrency this
could only be achieved using EJB
Timers
Tasks can also be started at Application Deploy
Trigger enables programmatic scheduling
15
What are the Main Components?
Managed Executor Services
Managed
Executor
Service
Jakarta EE
Executor
Service
Java SE
Managed
Scheduled
Executor
Service
Scheduled
Executor
Service
Jakarta EE specifies
a default instance of
each.
Additional instances
can be configured for
specific needs.
Managed Task
Listener can track
Task execution
Trigger interface can
provide business logic
on when to schedule
a task
Managed Thread Factory
Managed
Thread
Factory
Jakarta EE
Thread
Factory
Java SE
Used where you need a non-
Jakarta EE component to
create threads that can be
utilised with Jakarta EE.
or
You need a managed thread
to do something asynch.
Used when you need an object to run on a non managed thread using a Jakarta EE
Context.
Key for maintaining Security Context that can be propagated to other threads
One example is a JMX Notification Listener
Context Service
19
What’s new in Jakarta EE 10?
Jakarta EE 10 provides annotations for application scoped objects.
@ManagedExecutorDefinition (
name="java:comp/env/concurrent/MyExecutor",
context = "java:comp/env/concurrent/ContextService",
maxAsync = 20,
hungTaskThreshold = 5000
)
@ManagedScheduledExecutorDefinition (
name="java:comp/env/concurrent/MyExecutor",
context = "java:comp/env/concurrent/ContextService",
maxAsync = 20,
hungTaskThreshold = 5000
)
Deployable Managed Objects
● ContextServiceDefinition
● ManagedExecutorDefinition
● ManagedScheduledExecutorDefintion
● ManagedThreadFactoryDefinition
Deployable Managed Objects
Like the EJB Asnchronous annotation but can be used with CDI beans.
Methods must return CompletableFuture, CompletionStage or void
@ApplicationScoped
public class AsynchBean {
@Asynchronous(executor=" java:comp/env/concurrent/MyExecutor")
public CompletableFuture<Double> asynchMethod() {
Double total;
…
return Asynchronous.Result.complete(total);
}
}
Specifying the Executor service will enable fine grained concurrency management when combined
with deployable executor services.
New @Asynchronous annotation
Support ForkJoinPool in a standard way and allow creation of Managed Fork Join Pools
public interface ManagedThreadFactory extends ThreadFactory,
ForkJionPool.ForkJoinWorkerThreadFactory
Support CompletableFuture on Executor
CompletableFuture<Void> runAsync(Runnable runnable);
<U> CompletableFuture<U> supplyAsync(Supplier<U> supplier);
Date Time Support on Triggers
public interface ZonedTrigger extends Trigger {
public ZonedDateTime getNextRunTime(LastExecution lastExecutionInfo,
ZonedDateTime taskScheduledTime);
…
Catch up with Java
Fork Join Pool
Example
Easier creation of Contextual aware components
public <R> Supplier<R> contextualSupplier(Supplier<R> supplier);
public <T, R> Function<T, R> contextualFunction(Function<T, R>
function);
public <T, U> BiConsumer<T, U> contextualConsumer(BiConsumer<T, U>
consumer);
public <R> Callable<R> contextualCallable(Callable<R> callable);
public Runnable contextualRunnable(Runnable runnable);
public <T> CompletionStage<T> withContextCapture(CompletionStage<T>
stage);
public <T> CompletableFuture<T>
withContextCapture(CompletableFuture<T> stage);
Context Service Upgrade to Java SE 8+
CronTrigger class - Implementation
● Concrete Trigger Implementation for Convenience
public CronTrigger(final String cron, final ZoneId zone)
Use Expressions
trigger = new CronTrigger("0 7 * SEP-MAY MON-FRI",
ZoneId.of("America/New_York"));
Use Fluent API
trigger = new CronTrigger(ZoneId.of("America/Los_Angeles"))
.months(Month.DECEMBER)
.daysOfMonth(24)
.hours(16, 18);
27
Futures
COPYRIGHT (C) 2019, ECLIPSE FOUNDATION, INC. | MADE AVAILABLE UNDER THE ECLIPSE PUBLIC LICENSE 2.0 (EPL-2.0) 27
Like the EJB Timer annotation but for other components e.g. CDI Beans (non-persistent)
@ApplicationScoped
public class ScheduledBean {
@Schedule(executor="MyExecutor", minute="*/5")
public void fiveMinuteRule() {
}
}
Supports cron like commands (c.f. EJB).
Some comments are that this can be done in plain Java utilising CronTrigger and would
need to change the schedule
@Schedule Annotation · Issue #98 · jakartaee/concurrency (github.com)
New @Schedule Annotation
Currently Objects are tied to JNDI therefore need @Resource
@ApplicationScoped
public class CDIBean {
@Inject ManagedExecutorService service;
}
@Inject Support for Managed Objects
Provides locking semantics for CDI Beans – especially useful for ApplicationScoped
@ApplicationScoped
public class CDIBean {
@Lock(LockType.READ) doReadMany(){};
@Lock(LockType.WRITE) doOneWriter();
}
Semantics would be similar to EJB Locks
@Lock Annotation
Lock Annotation · Issue #135 · jakartaee/concurrency (github.com)
@Lock(LockType.READ)
Limits through Semaphore the number of threads that can execute a method or all
methods concurrently.
@ApplicationScoped
public class CDIBean {
@MaxConcurrency(2) maxTwoThreadsMethod(){};
@MaxConcurrency(10) maxUseofAPI();
}
Thread Isolation can be currently handled through Asynchronous with a
ManagedExecutor with maxConcurrency specified.
@MaxConcurrency Annotation
MaxConcurrency annotation · Issue #136 · jakartaee/concurrency (github.com)
@Lock(LockType.READ)
Possible Future Platform Alignment
● Specify more how Concurrency interacts with Jakarta Context and Dependency
Injection Contexts
● Enable propagation of Transactions across threads
● Reimplement Jakarta Enterprise Beans Asynchronous on Concurrency
○ Use new Concurrency Asynchronous annotation
○ Alternate allow MES to be specified
● Reimplement Jakarta Enterprise Beans Timers on Concurrency
○ Use new Schedule annotation
○ Alternate allow MES to be specified
● Align Asynch Servlets and JAX-RS onto Concurrency
○ Provide logical executor name
● Support Flow API
○ https://github.com/jakartaee/concurrency/issues/257
○ No work done as yet
● Make usable in Core Profile
○ Remove requirements for JNDI and EJBs in TCK
Others
34
Getting Involved
COPYRIGHT (C) 2019, ECLIPSE FOUNDATION, INC. | MADE AVAILABLE UNDER THE ECLIPSE PUBLIC LICENSE 2.0 (EPL-2.0) 34
Get Involved!
• Eclipse Project
• https://projects.eclipse.org/
projects/ee4j.cu
• Mailing List
• https://accounts.eclipse.org
/mailing-list/cu-dev
• Code
• https://github.com/jakartaee/concurrency
• Raise Issues for New Features.
• Review what is their for EE10
• Eclipse Compatible Implementation
(used in GlassFish and Payara)
• https://github.com/eclipse-ee4j/concurrency-ri
v
THIS IS CODE FIRST, OPEN SOURCE DEVELOPMENT...
Please visit us at:
payara.fish/join-us
Payara is always on the hunt for the
best people to work with - Someone
that makes a difference, cares for
quality, and is really good at their job
Learn more:
https://payara.fish/careers
We’re Hiring
• Payara is a proud 2021 Queen's Award for
Enterprise recipient for International Trade.
• Overseas sales grew by 107% in the last
three years and the percentage exported
grew from 33% to 75%
Winner of The
Queens Award for
Enterprise
Join our Global Meetup Group to find out more about our
future events and get involved with our community
Learn more:
https://www.meetup.com/payara-global-meetup/
Payara Global Meetup

Jakarta Concurrency: Present and Future

  • 1.
    Jakarta Concurrency: Present andFuture Steve Millidge
  • 2.
    Agenda • Why dowe need Jakarta Concurrency? • What are the main components? • What’s new in Jakarta EE 10? • Futures • Get Involved.
  • 3.
    3 Why do weneed Jakarta Concurrency?
  • 4.
    6 What is JakartaConcurrency New name for JSR236: Concurrency Utilities for Java EE • Introduced in Java EE 7 • No change in Java EE 8 • No change in Jakarta EE 8 • Namespace change in Jakarta EE 9 • New Features Jakarta EE 10 New Maven coordinates in Jakarta EE 8/9/10 <dependency> <groupId>jakarta.enterprise.concurrent</groupId> <artifactId>jakarta.enterprise.concurrent-api</artifactId> <version>1.1.2 (8) 2.0 (9.x) 3.0.2 (10.0)</version> </dependency> Steve Millidge
  • 5.
    7 Goal • Utilize existingapplicable Java EE platform services. Provide a simple yet flexible API for application component providers to design applications using concurrency design principles. • Allow Java SE developers a simple migration path to the Java EE platform by providing consistency between the Java SE and Java EE platforms. • Allow application component providers to easily add concurrency to existing Java EE applications. • Support simple (common) and advanced concurrency patterns without sacrificing usability
  • 6.
  • 7.
    9 What is theUse Case? Adding an Asynch Break in your application (Before JSR 236) Many older Java EE Applications use JMS just for this!
  • 8.
    10 What is theUse Case? Adding an Asynch Break in your application (Jakarta Concurrency) Much more lightweight than a JMS Broker!!!
  • 9.
    Show me theCode (Simple Asynch Break – pre JakartaEE 10)
  • 10.
    What is theUse Case? Running Tasks in Parallel (Jakarta Concurrency)
  • 11.
    Show me theCode Parallel Tasks
  • 12.
    Running Periodic Tasks (JakartaConcurrency) What is the Use Case? Before Java EE Concurrency this could only be achieved using EJB Timers Tasks can also be started at Application Deploy Trigger enables programmatic scheduling
  • 13.
    15 What are theMain Components?
  • 14.
    Managed Executor Services Managed Executor Service JakartaEE Executor Service Java SE Managed Scheduled Executor Service Scheduled Executor Service Jakarta EE specifies a default instance of each. Additional instances can be configured for specific needs. Managed Task Listener can track Task execution Trigger interface can provide business logic on when to schedule a task
  • 15.
    Managed Thread Factory Managed Thread Factory JakartaEE Thread Factory Java SE Used where you need a non- Jakarta EE component to create threads that can be utilised with Jakarta EE. or You need a managed thread to do something asynch.
  • 16.
    Used when youneed an object to run on a non managed thread using a Jakarta EE Context. Key for maintaining Security Context that can be propagated to other threads One example is a JMX Notification Listener Context Service
  • 17.
    19 What’s new inJakarta EE 10?
  • 18.
    Jakarta EE 10provides annotations for application scoped objects. @ManagedExecutorDefinition ( name="java:comp/env/concurrent/MyExecutor", context = "java:comp/env/concurrent/ContextService", maxAsync = 20, hungTaskThreshold = 5000 ) @ManagedScheduledExecutorDefinition ( name="java:comp/env/concurrent/MyExecutor", context = "java:comp/env/concurrent/ContextService", maxAsync = 20, hungTaskThreshold = 5000 ) Deployable Managed Objects
  • 19.
    ● ContextServiceDefinition ● ManagedExecutorDefinition ●ManagedScheduledExecutorDefintion ● ManagedThreadFactoryDefinition Deployable Managed Objects
  • 20.
    Like the EJBAsnchronous annotation but can be used with CDI beans. Methods must return CompletableFuture, CompletionStage or void @ApplicationScoped public class AsynchBean { @Asynchronous(executor=" java:comp/env/concurrent/MyExecutor") public CompletableFuture<Double> asynchMethod() { Double total; … return Asynchronous.Result.complete(total); } } Specifying the Executor service will enable fine grained concurrency management when combined with deployable executor services. New @Asynchronous annotation
  • 21.
    Support ForkJoinPool ina standard way and allow creation of Managed Fork Join Pools public interface ManagedThreadFactory extends ThreadFactory, ForkJionPool.ForkJoinWorkerThreadFactory Support CompletableFuture on Executor CompletableFuture<Void> runAsync(Runnable runnable); <U> CompletableFuture<U> supplyAsync(Supplier<U> supplier); Date Time Support on Triggers public interface ZonedTrigger extends Trigger { public ZonedDateTime getNextRunTime(LastExecution lastExecutionInfo, ZonedDateTime taskScheduledTime); … Catch up with Java
  • 22.
  • 23.
    Easier creation ofContextual aware components public <R> Supplier<R> contextualSupplier(Supplier<R> supplier); public <T, R> Function<T, R> contextualFunction(Function<T, R> function); public <T, U> BiConsumer<T, U> contextualConsumer(BiConsumer<T, U> consumer); public <R> Callable<R> contextualCallable(Callable<R> callable); public Runnable contextualRunnable(Runnable runnable); public <T> CompletionStage<T> withContextCapture(CompletionStage<T> stage); public <T> CompletableFuture<T> withContextCapture(CompletableFuture<T> stage); Context Service Upgrade to Java SE 8+
  • 24.
    CronTrigger class -Implementation ● Concrete Trigger Implementation for Convenience public CronTrigger(final String cron, final ZoneId zone) Use Expressions trigger = new CronTrigger("0 7 * SEP-MAY MON-FRI", ZoneId.of("America/New_York")); Use Fluent API trigger = new CronTrigger(ZoneId.of("America/Los_Angeles")) .months(Month.DECEMBER) .daysOfMonth(24) .hours(16, 18);
  • 25.
    27 Futures COPYRIGHT (C) 2019,ECLIPSE FOUNDATION, INC. | MADE AVAILABLE UNDER THE ECLIPSE PUBLIC LICENSE 2.0 (EPL-2.0) 27
  • 26.
    Like the EJBTimer annotation but for other components e.g. CDI Beans (non-persistent) @ApplicationScoped public class ScheduledBean { @Schedule(executor="MyExecutor", minute="*/5") public void fiveMinuteRule() { } } Supports cron like commands (c.f. EJB). Some comments are that this can be done in plain Java utilising CronTrigger and would need to change the schedule @Schedule Annotation · Issue #98 · jakartaee/concurrency (github.com) New @Schedule Annotation
  • 27.
    Currently Objects aretied to JNDI therefore need @Resource @ApplicationScoped public class CDIBean { @Inject ManagedExecutorService service; } @Inject Support for Managed Objects
  • 28.
    Provides locking semanticsfor CDI Beans – especially useful for ApplicationScoped @ApplicationScoped public class CDIBean { @Lock(LockType.READ) doReadMany(){}; @Lock(LockType.WRITE) doOneWriter(); } Semantics would be similar to EJB Locks @Lock Annotation Lock Annotation · Issue #135 · jakartaee/concurrency (github.com) @Lock(LockType.READ)
  • 29.
    Limits through Semaphorethe number of threads that can execute a method or all methods concurrently. @ApplicationScoped public class CDIBean { @MaxConcurrency(2) maxTwoThreadsMethod(){}; @MaxConcurrency(10) maxUseofAPI(); } Thread Isolation can be currently handled through Asynchronous with a ManagedExecutor with maxConcurrency specified. @MaxConcurrency Annotation MaxConcurrency annotation · Issue #136 · jakartaee/concurrency (github.com) @Lock(LockType.READ)
  • 30.
    Possible Future PlatformAlignment ● Specify more how Concurrency interacts with Jakarta Context and Dependency Injection Contexts ● Enable propagation of Transactions across threads ● Reimplement Jakarta Enterprise Beans Asynchronous on Concurrency ○ Use new Concurrency Asynchronous annotation ○ Alternate allow MES to be specified ● Reimplement Jakarta Enterprise Beans Timers on Concurrency ○ Use new Schedule annotation ○ Alternate allow MES to be specified ● Align Asynch Servlets and JAX-RS onto Concurrency ○ Provide logical executor name
  • 31.
    ● Support FlowAPI ○ https://github.com/jakartaee/concurrency/issues/257 ○ No work done as yet ● Make usable in Core Profile ○ Remove requirements for JNDI and EJBs in TCK Others
  • 32.
    34 Getting Involved COPYRIGHT (C)2019, ECLIPSE FOUNDATION, INC. | MADE AVAILABLE UNDER THE ECLIPSE PUBLIC LICENSE 2.0 (EPL-2.0) 34
  • 33.
    Get Involved! • EclipseProject • https://projects.eclipse.org/ projects/ee4j.cu • Mailing List • https://accounts.eclipse.org /mailing-list/cu-dev • Code • https://github.com/jakartaee/concurrency • Raise Issues for New Features. • Review what is their for EE10 • Eclipse Compatible Implementation (used in GlassFish and Payara) • https://github.com/eclipse-ee4j/concurrency-ri v THIS IS CODE FIRST, OPEN SOURCE DEVELOPMENT...
  • 34.
    Please visit usat: payara.fish/join-us
  • 35.
    Payara is alwayson the hunt for the best people to work with - Someone that makes a difference, cares for quality, and is really good at their job Learn more: https://payara.fish/careers We’re Hiring
  • 36.
    • Payara isa proud 2021 Queen's Award for Enterprise recipient for International Trade. • Overseas sales grew by 107% in the last three years and the percentage exported grew from 33% to 75% Winner of The Queens Award for Enterprise
  • 37.
    Join our GlobalMeetup Group to find out more about our future events and get involved with our community Learn more: https://www.meetup.com/payara-global-meetup/ Payara Global Meetup