2016-09-11 5 views
0

私のエントリポイントは@Singletonが1分に2回実行されるようにスケジュールされています。EntityManagerが@Stateless EJBに挿入されていない、アプリケーションを再起動したとき

@Startup 
@Singleton 
public class MyScheduledProcess { 

    @Resource 
    protected TimerService timerService; 

    @Inject 
    private OutgoingMessageProvider provider; 

    // If I inject the em here, it is always well injected. 
    // @PersistenceContext 
    // EntityManager em; 

    /** 
    * Default startup method that will be called automatically to schedule the task. 
    */ 
    @PostConstruct 
    public void onStartup() { 
     // To erase all timers which may already exist... 
     stopAll(); 
     // Schedule 
     timerService.createCalendarTimer(getScheduledExpression("* * */30"), new TimerConfig()); 
    } 

    @Timeout 
    protected void onTimeout(final Timer timer) { 
     process(timer); 
    } 

    /** 
    * Business logic called when the timer is triggered. 
    * @param timer 
    */ 
    public void process(Timer t) { 
     logger.info("Start process"); 

     // When em is injected here, there is no problem. 
     // OutgoingMessage outMsg = em.find(OutgoingMessage.class, 3L); 
     // logger.info("found:" + outMsg); 

     logger.info("provider :" + provider); 
     List<OutgoingMessage> findAll = provider.findByStatus("CREATED"); 
     logger.info("Nb in list : " + findAll.size()); 

    } 

    public static ScheduleExpression getScheduledExpression(String stringRepresentation) { 
     ScheduleExpression expression = new ScheduleExpression(); 

     // [....] 

     return expression; 
    } 

    public void stopAll() { 
     for(Timer timer : timerService.getTimers()) { 
      logger.debug("Stopping " + timer); 
      timer.cancel(); 
     } 
    } 
} 

このシングルトンでは、Stateless BeanであるOutgoingMessageProviderを挿入します。

@Stateless 
public class OutgoingMessageProvider { 

    @PersistenceContext(unitName= "DefaultPU") 
    protected EntityManager em; 

    public List<OutgoingMessage> findByStatus(String status) { 

     logger.debug("Value of em : " + em); 
     return this.em.createQuery("SELECT m FROM OutgoingMessage m WHERE m.status = :status") 
      .setParameter("status", status) 
      .getResultList(); 
    } 

    public OutgoingMessage findByMessageId(String messageId) { 
     return ........ 
    } 


    public void create(OutgoingMessage entity) { 
     em.persist(entity); 
    } 

     .... 

} 

私はweblogic(12.1.3)に自分の戦争をアップロードして展開すると、最初の起動時にすべてが正常で動作します。 しかし、Weblogic Admin Consoleでアプリを停止してもう一度起動すると、「NoSuchEJBException:Beanは既にデプロイされていません」と表示されます。 :すべては、私は私のアプリを展開するときに正常に動作し、私が再起動したときにうまくいかない理由

22:22:16.216 [[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)'] INFO com.poc.schedul.MyScheduledProcess - provider :[email protected]6bebf1 

<11-Sep-2016 22:22:16 o'clock CEST> <Error> <EJB> <BEA-011088> <The following error occurred while invoking the ejbTimeout(javax.ejb.Timer) method of EJB MyScheduledProcess(Application: poc2, EJBComponent: poc.war). 
javax.ejb.EJBException: EJB Exception: : com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke public java.lang.Object org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(javax.interceptor.InvocationContext) throws java.lang.Exception on bean class class org.jboss.weld.ejb.SessionBeanInterceptor with args: [LifecycleEventCallbackInvocationContext(962794227)] 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeLifecycleMethod(Jsr250Metadata.java:379) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:115) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:144) 
at com.oracle.pitchfork.intercept.InterceptionMetadata.invokeTimeoutMethod(InterceptionMetadata.java:531) 
at weblogic.ejb.container.injection.EjbComponentCreatorImpl.invokeTimer(EjbComponentCreatorImpl.java:71) 
at weblogic.ejb.container.injection.InjectionBasedEjbComponentCreator.invokeTimer(InjectionBasedEjbComponentCreator.java:152) 
at weblogic.ejb.container.manager.BaseEJBManager.invokeTimeoutMethod(BaseEJBManager.java:176) 
at weblogic.ejb.container.timer.TimerImpl.timerExpired(TimerImpl.java:340) 
at weblogic.timers.internal.TimerImpl.run(TimerImpl.java:304) 
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548) 
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311) 
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263) 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.GeneratedMethodAccessor927.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeLifecycleMethod(Jsr250Metadata.java:377) 
... 11 more 
Caused by: com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke timeout method protected void com.poc.schedul.MyScheduledProcess.onTimeout(javax.ejb.Timer) on bean class class com.poc.schedul.MyScheduledProcess_szpi74_Impl with args: [[EJB Timer] id: 44 pk: 1 info: null isAutoCreated: false timer: 1473625336209.6(0) state: 2 ejb: MyScheduledProcess(Application: poc2, EJBComponent: poc.war) Thread: Thread[[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads]]] 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeTimeoutMethodInternal(Jsr250Metadata.java:364) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:120) 
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:49) 
... 15 more 
Caused by: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeTimeoutMethodInternal(Jsr250Metadata.java:362) 
... 17 more 
Caused by: javax.ejb.NoSuchEJBException: Bean is already undeployed. 
at weblogic.ejb.container.manager.BaseEJBManager.ensureDeployed(BaseEJBManager.java:131) 
at weblogic.ejb.container.manager.BaseEJBManager.preInvoke(BaseEJBManager.java:136) 
at weblogic.ejb.container.manager.StatelessManager.preInvoke(StatelessManager.java:138) 
at weblogic.ejb.container.internal.BaseLocalObject.getBeanInstance(BaseLocalObject.java:148) 
at weblogic.ejb.container.internal.BaseLocalObject.preInvoke(BaseLocalObject.java:105) 
at weblogic.ejb.container.internal.BaseLocalObject.__WL_preInvoke(BaseLocalObject.java:70) 
at weblogic.ejb.container.internal.SessionLocalMethodInvoker.invoke(SessionLocalMethodInvoker.java:22) 
at com.poc.schedul.OutgoingMessageProvider_ek022o_NoIntfViewImpl.findByStatus(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.jboss.weld.util.reflection.SecureReflections$13.work(SecureReflections.java:267) 
at org.jboss.weld.util.reflection.SecureReflectionAccess.run(SecureReflectionAccess.java:52) 
at org.jboss.weld.util.reflection.SecureReflectionAccess.runAsInvocation(SecureReflectionAccess.java:137) 
at org.jboss.weld.util.reflection.SecureReflections.invoke(SecureReflections.java:263) 
at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:115) 
at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56) 
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:105) 
at com.poc.schedul.OutgoingMessageProvider$Proxy$_$$_Weld$Proxy$.findByStatus(OutgoingMessageProvider$Proxy$_$$_Weld$Proxy$.java) 
at com.poc.schedul.MyScheduledProcess.process(MyScheduledProcess.java:90) 
at com.poc.schedul.MyScheduledProcess.onTimeout(MyScheduledProcess.java:111) 
... 22 more 

at weblogic.ejb.container.internal.EJBRuntimeUtils.throwEJBException(EJBRuntimeUtils.java:88) 
at weblogic.ejb.container.internal.BaseLocalObject.handleSystemException(BaseLocalObject.java:503) 
at weblogic.ejb.container.internal.BaseLocalObject.handleSystemException(BaseLocalObject.java:446) 
at weblogic.ejb.container.internal.BaseLocalObject.postInvoke1(BaseLocalObject.java:251) 
at weblogic.ejb.container.internal.BaseLocalObject.postInvoke(BaseLocalObject.java:431) 
Truncated. see log file for complete stacktrace 
Caused By: com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke public java.lang.Object org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(javax.interceptor.InvocationContext) throws java.lang.Exception on bean class class org.jboss.weld.ejb.SessionBeanInterceptor with args: [LifecycleEventCallbackInvocationContext(962794227)] 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeLifecycleMethod(Jsr250Metadata.java:379) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:115) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:144) 
at com.oracle.pitchfork.intercept.InterceptionMetadata.invokeTimeoutMethod(InterceptionMetadata.java:531) 
at weblogic.ejb.container.injection.EjbComponentCreatorImpl.invokeTimer(EjbComponentCreatorImpl.java:71) 
Truncated. see log file for complete stacktrace 
Caused By: java.lang.reflect.InvocationTargetException 
at sun.reflect.GeneratedMethodAccessor927.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeLifecycleMethod(Jsr250Metadata.java:377) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:115) 
Truncated. see log file for complete stacktrace 
Caused By: com.oracle.pitchfork.interfaces.LifecycleCallbackException: Failure to invoke timeout method protected void com.poc.schedul.MyScheduledProcess.onTimeout(javax.ejb.Timer) on bean class class com.poc.schedul.MyScheduledProcess_szpi74_Impl with args: [[EJB Timer] id: 44 pk: 1 info: null isAutoCreated: false timer: 1473625336209.6(0) state: 2 ejb: MyScheduledProcess(Application: poc2, EJBComponent: poc.war) Thread: Thread[[ACTIVE] ExecuteThread: '15' for queue: 'weblogic.kernel.Default (self-tuning)',5,Pooled Threads]]] 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeTimeoutMethodInternal(Jsr250Metadata.java:364) 
at com.oracle.pitchfork.intercept.LifecycleEventCallbackInvocationContext.proceed(LifecycleEventCallbackInvocationContext.java:120) 
at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:49) 
at sun.reflect.GeneratedMethodAccessor927.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
Truncated. see log file for complete stacktrace 
Caused By: java.lang.reflect.InvocationTargetException 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.oracle.pitchfork.inject.Jsr250Metadata.invokeTimeoutMethodInternal(Jsr250Metadata.java:362) 
Truncated. see log file for complete stacktrace 
Caused By: javax.ejb.NoSuchEJBException: Bean is already undeployed. 
at weblogic.ejb.container.manager.BaseEJBManager.ensureDeployed(BaseEJBManager.java:131) 
at weblogic.ejb.container.manager.BaseEJBManager.preInvoke(BaseEJBManager.java:136) 
at weblogic.ejb.container.manager.StatelessManager.preInvoke(StatelessManager.java:138) 
at weblogic.ejb.container.internal.BaseLocalObject.getBeanInstance(BaseLocalObject.java:148) 
at weblogic.ejb.container.internal.BaseLocalObject.preInvoke(BaseLocalObject.java:105) 
Truncated. see log file for complete stacktrace 

だから、誰かが私に説明できますか?アプリケーションの起動時または停止時にアプリケーションサーバではどうなりますか?

ログに表示されているように、Singletonはプロバイダを知っているので、プロバイダがエンティティマネージャを知らないと想像できるだけです...間違っていますか?しかし、シングルトンにEntityManagerを注入しようとすると、すべて正常に動作します。それで、entityManagerがアプリケーションサーバーによってうまく管理されていることを意味します...なぜステートレスBeanに注入されないのですか?

私は(...と@PersistenceContextを取り除く)PostConstructを追加することにより、プロバイダに自分自身にEntityManagerをインスタンス化しようとした:

@PostConstruct 
public void init() 
{ 
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("DefaultPU"); 
    em = emf.createEntityManager(); } 
    logger.info("ENTITY MANAGER HAS BEEN SET : " +em); 
} 

しかし、この方法は、一度だけ呼ばれる、と私は何の痕跡を持っていませんアプリを再起動するとログに記録されます。

正常ですか?

答えて

1

問題は私のシングルトンに@EJBで@Injectを置き換えることで解決されました:

@Startup 
@Singleton 
public class MyScheduledProcess { 

    @Resource 
    protected TimerService timerService; 

    @EJB 
    private OutgoingMessageProvider provider; 
... 

はい、@EJBで動作することをロジックらしいです。

しかし、私は@Injectで説明した動作を理解していません。最初に(デプロイ後)正常に動作しますが、戦争をやり直して失敗すると注入が失敗します。誰かが分かっている場合は、気軽に共有してください:)

関連する問題