-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Virtual Threads in Jetty & Tomcat (#701)
Allows enabling virtual thread support for Jetty and Tomcat. Undertow has issues with Virtual threads so not implemented. See spring-projects/spring-boot#39812 Co-authored-by: Sergio del Amo <[email protected]> --------- Co-authored-by: Sergio del Amo <[email protected]>
- Loading branch information
1 parent
9135e6e
commit ab6b5f9
Showing
7 changed files
with
209 additions
and
48 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
21 changes: 21 additions & 0 deletions
21
http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyVirtualThreadSpec.groovy
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,21 @@ | ||
package io.micronaut.servlet.jetty | ||
|
||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.eclipse.jetty.server.Server | ||
import org.eclipse.jetty.util.thread.QueuedThreadPool | ||
import spock.lang.Requires | ||
import spock.lang.Specification | ||
|
||
@MicronautTest | ||
@Requires({ jvm.java21 }) | ||
class JettyVirtualThreadSpec extends Specification { | ||
@Inject Server server | ||
|
||
void "test virtual thread enabled on JDK 21+"() { | ||
expect: | ||
server.threadPool instanceof QueuedThreadPool | ||
server.threadPool.virtualThreadsExecutor != null | ||
|
||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
http-server-tomcat/src/main/java/io/micronaut/servlet/tomcat/TomcatVirtualThreadEnabler.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,49 @@ | ||
/* | ||
* Copyright 2017-2024 original authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.micronaut.servlet.tomcat; | ||
|
||
import io.micronaut.context.annotation.Requires; | ||
import io.micronaut.context.event.BeanCreatedEvent; | ||
import io.micronaut.context.event.BeanCreatedEventListener; | ||
import io.micronaut.core.annotation.NonNull; | ||
import io.micronaut.servlet.http.ServletConfiguration; | ||
import jakarta.inject.Singleton; | ||
import org.apache.catalina.connector.Connector; | ||
import org.apache.coyote.ProtocolHandler; | ||
import org.apache.tomcat.util.threads.VirtualThreadExecutor; | ||
|
||
/** | ||
* Enables virtual thread configuration if enabled. | ||
*/ | ||
@Requires(sdk = Requires.Sdk.JAVA, version = "21") | ||
@Singleton | ||
class TomcatVirtualThreadEnabler implements BeanCreatedEventListener<Connector> { | ||
private final ServletConfiguration servletConfiguration; | ||
|
||
public TomcatVirtualThreadEnabler(ServletConfiguration servletConfiguration) { | ||
this.servletConfiguration = servletConfiguration; | ||
} | ||
|
||
@Override | ||
public Connector onCreated(@NonNull BeanCreatedEvent<Connector> event) { | ||
Connector connector = event.getBean(); | ||
if (servletConfiguration.isEnableVirtualThreads()) { | ||
ProtocolHandler protocolHandler = connector.getProtocolHandler(); | ||
protocolHandler.setExecutor(new VirtualThreadExecutor("tomcat-handler-")); | ||
} | ||
return connector; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...-server-tomcat/src/test/groovy/io/micronaut/servlet/tomcat/TomcatVirtualThreadSpec.groovy
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,20 @@ | ||
package io.micronaut.servlet.tomcat | ||
|
||
|
||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import jakarta.inject.Inject | ||
import org.apache.catalina.startup.Tomcat | ||
import org.apache.tomcat.util.threads.VirtualThreadExecutor | ||
import spock.lang.Requires | ||
import spock.lang.Specification | ||
|
||
@MicronautTest | ||
@Requires({ jvm.java21 }) | ||
class TomcatVirtualThreadSpec extends Specification { | ||
@Inject Tomcat server | ||
|
||
void "test virtual thread enabled on JDK 21+"() { | ||
expect: | ||
server.connector.protocolHandler.executor instanceof VirtualThreadExecutor | ||
} | ||
} |
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