Comments (5)
The fix would mean rewriting ConcurrencyUtil.kt to either not use reflection, or refactor it in such a way that it works with GraalVM.
How can we achieve that while still targeting Java 17?
from javalin.github.io.
Not sure, that would have to be looked into. The first thing that comes to mind is that it's maybe possible to refactor ReflectiveVirtualThreadBuilder so that the "unstarted" method gets correctly invoked by GraalVMs native-image compiler. One would have to experiment with different solutions until one finds something that works. How does the other frameworks solve this problem (Spring, Micronaut etc)?
from javalin.github.io.
One possible solution could be to create a single instance of a virtual thread factory (Thread.ofVirtual().factory()
) to create new threads with instead of Thread.Builder.OfVirtual
in ReflectiveVirtualThreadBuilder
. Then you won't have to call the unstarted
method which GraalVM has problems with. It'd probably also be a slightly more efficient implementation than instantiating a new builder for each thread.
from javalin.github.io.
I got around this issue when upgrading to Javalin 6. I can now set the Jetty thread pool directly myself.
Javalin javalin = Javalin.create(config -> {
// Using a custom thread pool for virtual threads instead of the default Javalin LoomUtil.LoomThreadPool.
// This is because it uses reflection to create new threads which causes an error in GraalVM.
config.jetty.threadPool = new ThreadPool() {
@Override
public void join() { /*no-op*/ }
@Override
public int getThreads() {
return 1;
}
@Override
public int getIdleThreads() {
return 1;
}
@Override
public boolean isLowOnThreads() {
return false;
}
@Override
public void execute(@NonNull Runnable command) {
AppConfig.virtualThreadFactory.newThread(command).start();
}
};
});
For reference I previously used the GraalVM SDK to override the use of reflection in Javalin's NamedVirtualThreadFactory. This has now been removed.
import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import io.javalin.util.NamedVirtualThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
@TargetClass(NamedVirtualThreadFactory.class)
public final class Target_io_javalin_util_NamedVirtualThreadFactory {
@Alias
AtomicInteger threadCount;
@Alias
String prefix;
@Substitute
public Thread newThread(Runnable runnable) {
return Thread.ofVirtual()
.name(prefix + "-Virtual-" + threadCount.getAndIncrement())
.unstarted(runnable);
}
}
from javalin.github.io.
I assume this issue can be closed. Maybe the Javalin documentation can be updated with the code above in case someone else tries to use Javalin with GraalVM?
from javalin.github.io.
Related Issues (20)
- Add javalin-rendering to plugin list HOT 2
- List bundled plugins
- [PLUGIN REPORTS] - Javalin Rendering
- Document `WsContext#enableAutomaticPings`
- Java version of simple-frontends-with-javalin-and-vue
- Update HTTP2 docs reference/tutorial HOT 2
- Document Javalin 6 plugin changes
- Jte tutorial provides invalid/confusing examples
- I am running HelloWorld in idea and the following error occurs, I am using Javalin 5.6.1 and jdk 11 HOT 1
- Community tutorials link HOT 2
- Unclear docs
- Server configuration guide references outdated API
- Pin version in past documentation HOT 2
- Javalin modules mentioned in download section are outdated HOT 1
- `javalin-rendering` integration docs are outdated HOT 3
- Enhance Documentation for the section Other web servers HOT 1
- What is the proper Hello World snippet for 6.1.3 HOT 1
- Outdated docs for SSL plugin
- [PLUGIN REPORTS] - Micrometer Plugin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from javalin.github.io.