Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 19:13
Show Gist options
  • Save gissuebot/39c66f5140fbb8ec9b5b to your computer and use it in GitHub Desktop.
Save gissuebot/39c66f5140fbb8ec9b5b to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 763, comment 1
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment