Guice& EJB注入(拦截器?)

时间:2012-05-15 01:35:38

标签: dependency-injection ejb guice interceptor

我已经调试了很多很多小时但我无法理解为什么代码不能正常工作。 以相同方式注释的两个服务类显示不同的拦截器行为。

堆栈跟踪#1:

Daemon Thread [http-thread-pool-8080(1)] (Suspended (breakpoint at line 120 in UserService))    
    UserService.saveUserOnLogin(UserBE) line: 120   
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
    Method.invoke(Object, Object...) line: 601  
    EJBSecurityManager.runMethod(Method, Object, Object[]) line: 1052   
    EJBSecurityManager.invoke(Method, boolean, Object, Object[]) line: 1124 
    StatelessSessionContainer(BaseContainer).invokeBeanMethod(EjbInvocation) line: 5388 
    EjbInvocation.invokeBeanMethod() line: 619  
    AroundInvokeChainImpl.invokeNext(int, InterceptorManager$AroundInvokeContext) line: 800 
    EjbInvocation.proceed() line: 571   
    _GuiceInterceptor_Serializable(GuiceInterceptor).injectByGuice(InvocationContext) line: 24  
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
    Method.invoke(Object, Object...) line: 601  
    AroundInvokeInterceptor.intercept(InterceptorManager$AroundInvokeContext) line: 861 
    AroundInvokeChainImpl.invokeNext(int, InterceptorManager$AroundInvokeContext) line: 800 
    EjbInvocation.proceed() line: 571   
    _SystemInterceptorProxy_Serializable(SystemInterceptorProxy).doAround(InvocationContext, Method) line: 162  
    _SystemInterceptorProxy_Serializable(SystemInterceptorProxy).aroundInvoke(InvocationContext) line: 144  
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
    Method.invoke(Object, Object...) line: 601  
    AroundInvokeInterceptor.intercept(InterceptorManager$AroundInvokeContext) line: 861 
    AroundInvokeChainImpl.invokeNext(int, InterceptorManager$AroundInvokeContext) line: 800 
    InterceptorManager.intercept(InterceptorManager$InterceptorChain, InterceptorManager$AroundInvokeContext) line: 370 
    StatelessSessionContainer(BaseContainer).__intercept(EjbInvocation) line: 5360  
    StatelessSessionContainer(BaseContainer).intercept(EjbInvocation) line: 5348    
    EJBLocalObjectInvocationHandler.invoke(Class, Method, Object[]) line: 214   
    EJBLocalObjectInvocationHandlerDelegate.invoke(Object, Method, Object[]) line: 88   
    $Proxy209.saveUserOnLogin(UserBE) line: not available   
    __EJB31_Generated__UserService__Intf____Bean__.saveUserOnLogin(UserBE) line: not available  
    LoginUserHandler.saveUser(UserDTO) line: 165    
    LoginUserHandler.loginUser(UserDTO) line: 123   
    LoginUserHandler.loginWithOAuth(String, String, String, String) line: 158   
    LoginUserHandler.execute(LoginUser, ExecutionContext) line: 103 
    LoginUserHandler.execute(Action, ExecutionContext) line: 1  
    GuiceDispatch(AbstractDispatch).doExecute(A, ExecutionContext) line: 81 
    GuiceDispatch(AbstractDispatch).execute(A) line: 68 
    HupaDispatchServlet(AbstractSecureDispatchServlet).execute(String, Action<?>) line: 29  
    HupaDispatchServlet.execute(String, Action<?>) line: 56 
    NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
    NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
    DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
    Method.invoke(Object, Object...) line: 601  
    RPC.invokeAndEncodeResponse(Object, Method, Object[], SerializationPolicy, int) line: 569   
    HupaDispatchServlet(RemoteServiceServlet).processCall(String) line: 208 
    HupaDispatchServlet(RemoteServiceServlet).processPost(HttpServletRequest, HttpServletResponse) line: 248    
    HupaDispatchServlet(AbstractRemoteServiceServlet).doPost(HttpServletRequest, HttpServletResponse) line: 62  
    HupaDispatchServlet(HttpServlet).service(HttpServletRequest, HttpServletResponse) line: 688 
    HupaDispatchServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 770 
    ServletDefinition.doService(ServletRequest, ServletResponse) line: 263  
    ServletDefinition.service(ServletRequest, ServletResponse) line: 178    
    ManagedServletPipeline.service(ServletRequest, ServletResponse) line: 91    
    FilterChainInvocation.doFilter(ServletRequest, ServletResponse) line: 62    
    ManagedFilterPipeline.dispatch(ServletRequest, ServletResponse, FilterChain) line: 118  
    GuiceFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 113    
    ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 256  
    ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 217  
    StandardWrapperValve.invoke(Request, Response) line: 279    
    StandardContextValve.invoke(Request, Response) line: 175    
    StandardPipeline.doInvoke(Request, Response, boolean) line: 655 
    StandardPipeline.invoke(Request, Response) line: 595    
    StandardHostValve.invoke(Request, Response) line: 161   
    CoyoteAdapter.doService(Request, Request, Response, Response) line: 331 
    CoyoteAdapter.service(Request, Response) line: 231  
    ContainerMapper$AdapterCallable.call() line: 317    
    ContainerMapper.service(Request, Response) line: 195    
    ProcessorTask.invokeAdapter() line: 849 
    ProcessorTask.doProcess() line: 746 
    ProcessorTask.process(InputStream, OutputStream) line: 1045 
    DefaultProtocolFilter.execute(Context) line: 228    
    HttpProtocolChain(DefaultProtocolChain).executeProtocolFilter(Context, int) line: 137   
    HttpProtocolChain(DefaultProtocolChain).execute(Context, int) line: 104 
    HttpProtocolChain(DefaultProtocolChain).execute(Context) line: 90   
    HttpProtocolChain.execute(Context) line: 79 
    ProtocolChainContextTask.doCall() line: 54  
    ProtocolChainContextTask(SelectionKeyContextTask).call() line: 59   
    ProtocolChainContextTask(ContextTask).run() line: 71    
    FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).doWork() line: 532   
    FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).run() line: 513  
    HttpWorkerThread(Thread).run() line: 722    

堆栈跟踪#2

Daemon Thread [http-thread-pool-8080(2)] (Suspended (entry into method synchronizeHeaders in MessageService))   
__EJB31_Generated__MessageService__Intf____Bean__(MessageService).synchronizeHeaders(String) line: 93   
FetchMessagesHandler.executeInternal(FetchMessages, ExecutionContext) line: 80  
FetchMessagesHandler.executeInternal(Action, ExecutionContext) line: 1  
FetchMessagesHandler(AbstractSessionHandler<A,R>).executeWithRetries(A, ExecutionContext, int) line: 127    
FetchMessagesHandler(AbstractSessionHandler<A,R>).execute(A, ExecutionContext) line: 97 
GuiceDispatch(AbstractDispatch).doExecute(A, ExecutionContext) line: 81 
GuiceDispatch(AbstractDispatch).execute(A) line: 68 
HupaDispatchServlet(AbstractSecureDispatchServlet).execute(String, Action<?>) line: 29  
HupaDispatchServlet.execute(String, Action<?>) line: 56 
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: 57  
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: 43  
Method.invoke(Object, Object...) line: 601  
RPC.invokeAndEncodeResponse(Object, Method, Object[], SerializationPolicy, int) line: 569   
HupaDispatchServlet(RemoteServiceServlet).processCall(String) line: 208 
HupaDispatchServlet(RemoteServiceServlet).processPost(HttpServletRequest, HttpServletResponse) line: 248    
HupaDispatchServlet(AbstractRemoteServiceServlet).doPost(HttpServletRequest, HttpServletResponse) line: 62  
HupaDispatchServlet(HttpServlet).service(HttpServletRequest, HttpServletResponse) line: 688 
HupaDispatchServlet(HttpServlet).service(ServletRequest, ServletResponse) line: 770 
ServletDefinition.doService(ServletRequest, ServletResponse) line: 263  
ServletDefinition.service(ServletRequest, ServletResponse) line: 178    
ManagedServletPipeline.service(ServletRequest, ServletResponse) line: 91    
FilterChainInvocation.doFilter(ServletRequest, ServletResponse) line: 62    
ManagedFilterPipeline.dispatch(ServletRequest, ServletResponse, FilterChain) line: 118  
GuiceFilter.doFilter(ServletRequest, ServletResponse, FilterChain) line: 113    
ApplicationFilterChain.internalDoFilter(ServletRequest, ServletResponse) line: 256  
ApplicationFilterChain.doFilter(ServletRequest, ServletResponse) line: 217  
StandardWrapperValve.invoke(Request, Response) line: 279    
StandardContextValve.invoke(Request, Response) line: 175    
StandardPipeline.doInvoke(Request, Response, boolean) line: 655 
StandardPipeline.invoke(Request, Response) line: 595    
StandardHostValve.invoke(Request, Response) line: 161   
CoyoteAdapter.doService(Request, Request, Response, Response) line: 331 
CoyoteAdapter.service(Request, Response) line: 231  
ContainerMapper$AdapterCallable.call() line: 317    
ContainerMapper.service(Request, Response) line: 195    
ProcessorTask.invokeAdapter() line: 849 
ProcessorTask.doProcess() line: 746 
ProcessorTask.process(InputStream, OutputStream) line: 1045 
DefaultProtocolFilter.execute(Context) line: 228    
HttpProtocolChain(DefaultProtocolChain).executeProtocolFilter(Context, int) line: 137   
HttpProtocolChain(DefaultProtocolChain).execute(Context, int) line: 104 
HttpProtocolChain(DefaultProtocolChain).execute(Context) line: 90   
HttpProtocolChain.execute(Context) line: 79 
ProtocolChainContextTask.doCall() line: 54  
ProtocolChainContextTask(SelectionKeyContextTask).call() line: 59   
ProtocolChainContextTask(ContextTask).run() line: 71    
FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).doWork() line: 532   
FixedThreadPool$BasicWorker(AbstractThreadPool$Worker).run() line: 513  
HttpWorkerThread(Thread).run() line: 722    

似乎没有为消息服务类调用拦截器。但是,它确实适用于用户服务类。为什么会这样?

消息服务类:

@Stateless
@Interceptors(GuiceInterceptor.class)
public class MessageService {

    /**
     * The gmail folder default.
     */
    private static final String GMAIL_ALL_MAIL = "[Gmail]/All Mail";

    /**
     * The IMAP store cache.
     */
    @Inject
    private IMAPStoreCache imapStoreCache;

    /**
     * The EJB resolver.
     */
    @Inject
    private EJBResolver ejbResolver;

用户服务类:

@Stateless
@Interceptors(GuiceInterceptor.class)
public class UserService {

    /**
     * The entity manager.
     */
    @PersistenceContext(unitName = "hupa")
    private EntityManager em;

    /**
     * The session provider.
     */
    @Inject
    private Provider<HttpSession> sessionProvider;

最后是拦截器:

public class GuiceInterceptor {

    @EJB
    private GuiceInjectorHolder injectorHolder;

    @AroundInvoke
    @AroundTimeout
    public Object injectByGuice(InvocationContext invocationContext) throws Exception {
        Injector injector = injectorHolder.getInjector();
        injector.injectMembers(invocationContext.getTarget());
        return invocationContext.proceed();
    }
}

希望有专家对此,我很沮丧;)

2 个答案:

答案 0 :(得分:3)

问题在于我不能在课堂的任何地方使用final修饰符,否则它将无法与Guice一起使用。甚至没有辅助辅助方法。

答案 1 :(得分:0)

您能否详细说明谁在调用MessageService以及他们如何获取对它的引用?

我的第一个猜测是,调用MessageService的任何人都直接引用了实例本身,并且没有通过通过@EJB MessageService service;注入或JNDI查找获得的容器创建的代理调用bean

请参阅this answer关于使用代理的重要性以及它与使用this或类似的bean实例的类似直接引用的不同之处。