vipx / google-guice

Automatically exported from code.google.com/p/google-guice
Apache License 2.0
0 stars 0 forks source link

something is weird, using thread pool in AOP #763

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Description of the issue:
using thread pool in AOP: fixedPool.submit
there is weird that fixedPool.submit called a lots times

Steps to reproduce:
let:

final ExecutorService fixedPool = Executors.newFixedThreadPool(10);

@AsyncDBCall
public void ssss(){
    System.out.println("ssss");
}

when calling:

@Test
public void aa2222(){
    instance.ssss();
}

if I wrote a interceptor (1) or (2), I got:
AsyncDBCall
AsyncDBCall
AsyncDBCall
AsyncDBCall
AsyncDBCall
...
...
...
AsyncDBCall
AsyncDBCall
AsyncDBCall
AsyncDBCall
ssss

(1) bindInterceptor(Matchers.any(), Matchers.annotatedWith(AsyncDBCall.class), 
new MethodInterceptor() {
    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        fixedPool.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                try {
                    System.out.println("AsyncDBCall");
                    return invocation.proceed();
                } catch (Throwable throwable) {
                    throw Throwables.propagate(throwable);
                }
            }
        });
        return null;
    }
});

(2) bindInterceptor(Matchers.any(), Matchers.annotatedWith(AsyncDBCall.class), 
new MethodInterceptor() {
    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        fixedPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("AsyncDBCall");
                    invocation.proceed();
                } catch (Throwable throwable) {
                    throw Throwables.propagate(throwable);
                }
            }
        });
        return null;
    }
});

using:
/usr/lib/jvm/java-6-openjdk-amd64/bin/java

Original issue reported on code.google.com by chenwei...@gmail.com on 14 Aug 2013 at 2:33

Attachments:

GoogleCodeExporter commented 9 years ago
Description of the issue:
using thread pool in AOP: fixedPool.submit
there is weird that fixedPool.submit called a lots times

Steps to reproduce:
let:

final ExecutorService fixedPool = Executors.newFixedThreadPool(10);

@AsyncDBCall
public void ssss(){
    System.out.println("ssss");
}

when calling:

@Test
public void aa2222(){
    instance.ssss();
}

if I wrote a interceptor (1) or (2), I got:
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
...
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
AsyncDBCall1
AsyncDBCall2
ssss11

(1)         bindInterceptor(Matchers.any(), 
Matchers.annotatedWith(AsyncDBCall.class), new MethodInterceptor() {
            @Override
            public Object invoke(final MethodInvocation invocation) throws Throwable {
                System.out.println("AsyncDBCall1");
                return fixedPool.submit(new Callable<Object>() {
                    @Override
                    public Object call() throws Exception {
                        try {
                            System.out.println("AsyncDBCall2");
                            return invocation.proceed();
                        } catch (Throwable throwable) {
                            throw Throwables.propagate(throwable);
                        }
                    }
                });
            }
        });

(2) bindInterceptor(Matchers.any(), Matchers.annotatedWith(AsyncDBCall.class), 
new MethodInterceptor() {
    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        System.out.println("AsyncDBCall1");
        fixedPool.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("AsyncDBCall2");
                    invocation.proceed();
                } catch (Throwable throwable) {
                    throw Throwables.propagate(throwable);
                }
            }
        });
        return null;
    }
});

using:
/usr/lib/jvm/java-6-openjdk-amd64/bin/java

Original comment by chenwei...@gmail.com on 14 Aug 2013 at 2:35

Attachments:

GoogleCodeExporter commented 9 years ago
AOP in Guice does not allow offloading the invocation to a separate thread.

Original comment by sberlin on 14 Aug 2013 at 2:44

GoogleCodeExporter commented 9 years ago
thank you very much

Original comment by chenwei...@gmail.com on 14 Aug 2013 at 2:52