Code Monkey home page Code Monkey logo

Comments (5)

tipsy avatar tipsy commented on August 18, 2024

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.

pompiuses avatar pompiuses commented on August 18, 2024

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.

pompiuses avatar pompiuses commented on August 18, 2024

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.

pompiuses avatar pompiuses commented on August 18, 2024

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.

pompiuses avatar pompiuses commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.