2016-11-25 104 views
1

我对JBoss和注解颇为陌生。我有以下代码示例。不相关的细节被删除。EJB Singleton服务在部署时失败

@Singleton 
@Startup 
public class SomeBean { 

    @Resource 
    TimerService timerService; 

    @Inject 
    AnotherSingleton anotherOne; 

    Timer timer; 

    @PostConstruct 
    private void ejbCreate() { 
     timer = timerService.createIntervalTimer(0, interval, tc); 
    } 

    @Timeout 
    public void run() throws Exception { 
    } 
} 

@Singleton 
public class AnotherSingleton { 

    @Inject 
    Repository rep; 
} 

有情况下,当战争在JBoss部署失败与库生产异常(在另一个JBoss的服务不可用)。

Caused by: java.lang.IllegalStateException: WFLYEE0042: Failed to construct component instance 

所以过程与

WFLYCTL0186: Services which failed to start:  service jboss.deployment.unit."someservices-view.war".component.SomeBean.START 

我有什么选择结束? 我可以告诉JBoss在启动时不要@注入bean,但是当代码由定时器执行时? 我能以某种方式捕捉异常吗? @Schedule没有问题因为我需要配置Timer。

回答

1

注射由CDI specification处理,其提供“包装”注射的功能,因为它是,like so

@Inject 
Instance<AnotherSingleton> anotherOneInstance; 

这基本上创造了一个围绕AnotherSingleton的代理,你可以在你需要的时候延迟获得对它的实际引用。

AnotherSingleton anotherOne = anotherOneInstance.get(); 

这应该允许部署成功,你的计时器如果此刻您尝试使用anotherOne和存储库不可用,代码将仍然有一个例外打破初始化,但当然。

或者,你可以始终做一个manual lookup through the BeanManager不必依赖任何形式的依赖注入,但这应该永远是最后的手段,因为它只会导致繁琐的代码。

+0

感谢提示。实例<>的解决方案似乎工作得很好。 – Eruanno

+0

@Eruanno如果此答案解决了您的问题,请通过选中答案旁边的复选标记来接受此问题。 –