Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interoperability with Structured Concurrency #272

Open
njr-11 opened this issue Mar 24, 2023 · 2 comments
Open

Interoperability with Structured Concurrency #272

njr-11 opened this issue Mar 24, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@njr-11
Copy link
Contributor

njr-11 commented Mar 24, 2023

Structured Concurrency has been in incubation in Java 19 and 20 and might be part of Java 21, in which case the Concurrency specification should address how it pertains to Jakarta EE.

The main way of interacting with Structured Concurrency is StructuredTaskScope, which has a fork method that accepts a Callable task. There is no way provided for the Jakarta EE Concurrency specification to automatically tie into it.

There are some steps that users can take manually to leverage existing APIs from the Jakarta EE Concurrency specification to ensure that their tasks run with context.

At the most granular level, you can use the ContextService.contextualCallable method that we added in Jakarta EE 10 to pre-contextualize Callables that you supply to the fork method.

try (var scope = new StructuredTaskScope<Object>()) {
    Future<Long> future1 = scope.fork(contextService.contextualCallable(task1));
    Future<Long> future2 = scope.fork(contextService.contextualCallable(task2));
    scope.join();
    ...
}

There is also a constructor that you can use to designate a specific ThreadFactory to be used by the StructuredTaskScope. The ThreadFactory could be a ManagedThreadFactory,

try (var scope = new StructuredTaskScope<Object>("MyTaskScopeWithContext", managedThreadFactory)) {
    Future<Long> future1 = scope.fork(task1);
    Future<Long> future2 = scope.fork(task2);
    scope.join();
    ...
}

The above is what will be possible if we don't do anything new in the Jakarta EE Concurrency spec for this. There will be the above ways of contextualizing tasks, but no ways of constraining concurrency.

StructuredTaskScope can be extended with subclasses. It even provides a couple of built-in ones:
https://download.java.net/java/early_access/jdk20/docs/api/jdk.incubator.concurrent/jdk/incubator/concurrent/StructuredTaskScope.ShutdownOnFailure.html
https://download.java.net/java/early_access/jdk20/docs/api/jdk.incubator.concurrent/jdk/incubator/concurrent/StructuredTaskScope.ShutdownOnSuccess.html
The Jakarta EE Concurrency specification could do something with subclasses and maybe let you obtain StructuredTaskScopes from managed executors if we thought that would be useful. More investigation is needed.

@njr-11 njr-11 added the enhancement New feature or request label Mar 24, 2023
@hantsy
Copy link

hantsy commented May 31, 2023

For my opinion, the structured task scope is similar to Kotlin Coroutines scope in concept.

I think adding an annotation(the functionality is similar to Kotlin suspend and CoroutinesScope) on method to wrap current execution into a constructed task scope.

But like async support, how to support the context prorogation seamlessly in background without copying context data explicitly .

@mswatosh
Copy link
Member

This was only a preview in Java 21, so this can be considered for EE12
https://openjdk.org/jeps/453

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants