puniverse / quasar

Fibers, Channels and Actors for the JVM
http://docs.paralleluniverse.co/quasar/
Other
4.56k stars 575 forks source link

How to co-work with cglib #306

Closed yanxinyuan closed 6 years ago

yanxinyuan commented 6 years ago

Dears, As far as i know, Quasar rely on bytecode instrumentation. This can be done at class loading time via a Java Agent, or at compilation time with an Ant task.

But now i have a class which is generated by cglib and i want one of its method can suspend the fiber. Is it possible ? And how should i do?

The code is something like this:

// This class and method is generated by cglib
class Xxx {
    public void yyy() throws SuspendExecution {
        for (int i = 0; i < 10; i++) {
        System.out.println(i);
        Fiber.park();
    }
     }
}

public static void main(String[] args) {
   // using cglib generated 'Xxx' class and 'yyy' method
   Fiber<Void> fiber = new Fiber<Void>() {
    @Override
    protected Void run() throws SuspendExecution, InterruptedException {
                Class<?> clazz = Class.forName("Xxx");
        Object obj = clazz.newInstance();
        Method method = clazz.getMethod("yyy");
        method.invoke(obj);
            return null;
    }   
    }.start();

     while(!fiber.isDone()) {
     Strand.sleep(2000);
     Fiber.unpark(fiber);
     }  
}

And the result should be:

java -jar target\Quasar-Cglib-0.0.1-SNAPSHOT.jar
QUASAR WARNING: Quasar Java Agent isn't running. If you're using another instrumentation method you can ignore this message; otherwise, please refer to the Getting Started section in the Quasar documentation.
0
1
2
yanxinyuan commented 6 years ago

I've created a minimal example project to demonstrate.

yanxinyuan commented 6 years ago

Ok , after i change the cglib Interceptor to throw SuspendExecution, this issue has gone.

yanxinyuan commented 6 years ago

Supplementary explanation, cglib can also co-work with quasar by using java agent instrument and SuspendableClassifier, here is an example.