ฉันใช้ Spring 3.0.5 กับมุมมองแบบ Round
มุมมอง @Around ทำงานได้อย่างสมบูรณ์แบบ นิพจน์ AOP กำหนดเป้าหมายอินเทอร์เฟซของกลุ่มถั่ว
ด้านดำเนินการตรรกะบางอย่างก่อนและหลังการเรียกใช้:
@Around(...)
public Object monitor(ProceedingJoinPoint pjp) throws Throwable {
// some code
Obj o = pjp.proceed();
// some code
}
ไม่ใช่เรื่องใหญ่.
ตอนนี้ ฉันกำลังพยายามสร้างอีกแง่มุมหนึ่งที่ทำให้เกิดข้อยกเว้น หากวิธีการสกัดกั้นใช้เวลานานเกินไป
private static ExecutorService executor = Executors.newCachedThreadPool();
@Around(...)
public Object monitor(ProceedingJoinPoint pjp) throws Throwable {
Object obj = null;
Callable<Object> task = new Callable<Object>() {
public Object call() {
return pjp.proceed();
}
};
Future<Object> future = executor.submit(task);
try {
obj = future.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException ex) {
...
} catch (InterruptedException e) {
// we ignore this one...
} catch (ExecutionException e) {
throw e.getCause(); // rethrow any exception raised by the invoked method
} finally {
future.cancel(true); // may or may not desire this
}
return obj;
}
เมื่อฉันรันโค้ดโดยใช้เฉพาะด้านนี้ ฉันจะได้รับข้อยกเว้นต่อไปนี้:
java.lang.RuntimeException: java.lang.IllegalStateException: ไม่พบ MethodInvocation: ตรวจสอบว่าการเรียกใช้ AOP อยู่ระหว่างดำเนินการ และ ExposeInvocationInterceptor อยู่ใน interceptor chain
จากเอกสาร Spring ฉันอ่านว่า:
"คลาส ExposeInvocationInterceptor
Interceptor ที่เปิดเผย MethodInvocation ปัจจุบันเป็นวัตถุ thread-local
ดูเหมือนว่าเป้าหมายจะหายไปเนื่องจากโดยพื้นฐานแล้วฉันเริ่มเธรดใหม่และเธรดใหม่ไม่สามารถเข้าถึงเธรดในเครื่องได้ มีวิธีแก้ไขปัญหานี้หรือแนวทางที่ดีกว่านี้หรือไม่?
ขอบคุณ