forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added synthetic bean with @VirtualThreads qualifier for the managed E…
…xecutorService backed by virtual threads
- Loading branch information
1 parent
2265840
commit bd26805
Showing
7 changed files
with
271 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...l-threads/runtime/src/main/java/io/quarkus/virtual/threads/DelegatingExecutorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.quarkus.virtual.threads; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutionException; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Future; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
/** | ||
* An implementation of {@code ExecutorService} that delegates to the real executor, while disallowing termination. | ||
*/ | ||
class DelegatingExecutorService implements ExecutorService { | ||
private final ExecutorService delegate; | ||
|
||
DelegatingExecutorService(final ExecutorService delegate) { | ||
this.delegate = delegate; | ||
} | ||
|
||
public void execute(final Runnable command) { | ||
delegate.execute(command); | ||
} | ||
|
||
public boolean isShutdown() { | ||
// container managed executors are never shut down from the application's perspective | ||
return false; | ||
} | ||
|
||
public boolean isTerminated() { | ||
// container managed executors are never shut down from the application's perspective | ||
return false; | ||
} | ||
|
||
public boolean awaitTermination(final long timeout, final TimeUnit unit) { | ||
return false; | ||
} | ||
|
||
public void shutdown() { | ||
throw new UnsupportedOperationException("shutdown not allowed on managed executor service"); | ||
} | ||
|
||
public List<Runnable> shutdownNow() { | ||
throw new UnsupportedOperationException("shutdownNow not allowed on managed executor service"); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Callable<T> task) { | ||
return delegate.submit(task); | ||
} | ||
|
||
@Override | ||
public <T> Future<T> submit(Runnable task, T result) { | ||
return delegate.submit(task, result); | ||
} | ||
|
||
@Override | ||
public Future<?> submit(Runnable task) { | ||
return delegate.submit(task); | ||
} | ||
|
||
@Override | ||
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException { | ||
return delegate.invokeAll(tasks); | ||
} | ||
|
||
@Override | ||
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | ||
throws InterruptedException { | ||
return delegate.invokeAll(tasks, timeout, unit); | ||
} | ||
|
||
@Override | ||
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException { | ||
return delegate.invokeAny(tasks); | ||
} | ||
|
||
@Override | ||
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) | ||
throws InterruptedException, ExecutionException, TimeoutException { | ||
return delegate.invokeAny(tasks, timeout, unit); | ||
} | ||
|
||
public String toString() { | ||
return delegate.toString(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...ntime/src/main/java/io/quarkus/virtual/threads/FallbackVirtualThreadsExecutorService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.quarkus.virtual.threads; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.AbstractExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import io.smallrye.mutiny.infrastructure.Infrastructure; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.impl.ContextInternal; | ||
|
||
/** | ||
* Fallback executor service implementation in case the virtual threads are disabled or not available on the current platform. | ||
* <p> | ||
* Executes tasks on the current Vert.x context worker pool, or when not available, on the Mutiny Infrastructure default worker | ||
* pool | ||
* Shutdown methods are no-op as the executor service is a wrapper around these previous execute methods. | ||
*/ | ||
class FallbackVirtualThreadsExecutorService extends AbstractExecutorService { | ||
|
||
@Override | ||
public void execute(Runnable command) { | ||
var context = Vertx.currentContext(); | ||
if (!(context instanceof ContextInternal)) { | ||
Infrastructure.getDefaultWorkerPool().execute(command); | ||
} else { | ||
context.executeBlocking(() -> { | ||
command.run(); | ||
return null; | ||
}, false); | ||
} | ||
} | ||
|
||
@Override | ||
public void shutdown() { | ||
// no-op | ||
} | ||
|
||
@Override | ||
public List<Runnable> shutdownNow() { | ||
return Collections.EMPTY_LIST; | ||
} | ||
|
||
@Override | ||
public boolean isShutdown() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isTerminated() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean awaitTermination(long timeout, TimeUnit unit) { | ||
return false; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ions/virtual-threads/runtime/src/main/java/io/quarkus/virtual/threads/VirtualThreads.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.quarkus.virtual.threads; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.ElementType.METHOD; | ||
import static java.lang.annotation.ElementType.PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.enterprise.util.AnnotationLiteral; | ||
import jakarta.inject.Qualifier; | ||
|
||
/** | ||
* Qualifies an injected virtual threads executor service. | ||
*/ | ||
@Qualifier | ||
@Target({ FIELD, METHOD, PARAMETER }) | ||
@Retention(RUNTIME) | ||
public @interface VirtualThreads { | ||
|
||
final class Literal extends AnnotationLiteral<VirtualThreads> implements VirtualThreads { | ||
|
||
public static final Literal INSTANCE = new Literal(); | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.