Java EE 6:控制具有依赖项的托管Bean的启动:CDI,EJB [英] Java EE 6: controlling startup of managed beans with dependencies: CDI, EJB

查看:66
本文介绍了Java EE 6:控制具有依赖项的托管Bean的启动:CDI,EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚在,因为我在我的应用程序我想知道是否有可能控制或影响启动豆类的方式和时间。



在我的Java EE应用程序中,我使用EJB,CDI和JSF2作为视图技术。通过SPI引导来自第三方的服务,并配置一个作业执行程序,该作业执行程序启动作业并处理其他与计时器相关的内容。作业执行程序完成引导程序后,将立即执行一项作业。该作业使用CDI注入来访问某些bean,并且其中一个bean使用EJB。



现在的问题是,在大多数情况下,Java EE 6服务器(JBoss 7.1.1)启动EJB仍然不可用,因此作业尝试访问它。引发异常,作业失败,并且构建服务以停用该失败的作业。好吧,停用失败的工作似乎还不错。重新启动并运行该作业的唯一解决方案是取消部署并重新部署。不幸的是,这是一项手动任务,无法通过编程方式完成。



而且,更糟糕的是:在极少数情况下,这种情况不会发生。



所以,我现在的问题是:我可以以某种方式控制EJB和CDI bean的初始化和部署,以便可以确保在初始化CDI bean之前初始化所有EJB bean吗? p>

我在EARs application.xml中将initialty-in-order设置为true,并设置EJB的顺序,以便按照我需要的方式对其进行初始化(EJB核心,然后是EJB业务,然后是WAR),但是基于CDI的服务以JAR的形式放在lib文件夹中。

解决方案

摘录自< a href = http://docs.oracle.com/javaee/6/tutorial/doc/gipvi.html rel = noreferrer> Java EE 6教程,进行了一些修改:

  @Singleton 
@Startup
公共类BeanA {...}

@Qualifier
@Target({FIELD,PARAMETER})
@Retention(RUNTIME)
public @interface EjbStarted {}

@Singleton
@Startup
@DependsOn( BeanA, BeanB, BeanC)
公共类LastBean {
@Inject @EjbStarted事件< String>事件;

@PostConstruct
public void startService(){
//此时,PrimaryBean可以使用了。
event.fire( LastBean);
}
}

公共类CDIService {
public void start(@Observes @EjbStarted字符串名称){
if( LastBean .equals(名称)){
startService();
}
}
}

更新:在考虑问题,我以某种方式忘记了您想要CDI bean中的初始化顺序,所以答案有点脱离上下文,对此感到抱歉:)



更新2:添加了如何在EJB之后启动CDI服务


I just read the very nice explanation of the various managed beans and their relations on Java EE 6 @javax.annotation.ManagedBean vs. @javax.inject.Named vs. @javax.faces.ManagedBean and as i face a annoying problem in my application i want to know if it is possible to control or influence the way how and when beans are started.

In my Java EE application I use EJB, CDI and JSF2 as view technology. Via SPI a service from a third party is booted and it configures a job executor which starts jobs and handles other timer relevant stuff. One job is immediately executed when the job executor finished its bootstrapping. This job uses CDI inject to get access to some beans and one of these beans make use of an EJB.

The problem is now that most of the time the Java EE 6 server (JBoss 7.1.1) starts the EJB is still not available then the job tries to access it. An exceptions is thrown and the job fails and the service is build to deactivate that failed job. Well, deactivating a faild job seems to be not too bad. The only solution for the job to get up and running again is to undeploy it and to redeploy it again. Which is a manual task unfortunately and it can not be done programmatically.

And, to make things bad: in rare cases this does not happen.

So, my question now is: can i somehow control the initialisation and deployment of the EJB and CDI beans so that i can ensure that all EJB beans are initialzed before the CDI beans are initialized?

I have set initialize-in-order to true in the EARs application.xml and set the sequence of the EJBs so that they get initialized they way i need them (EJB core, then EJB business, then WAR), but the CDI based service is placed as JAR in the lib folder.

解决方案

Excerpt from Java EE 6 Tutorial with some modifications:

@Singleton
@Startup
public class BeanA { ... }

@Qualifier
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
public @interface EjbStarted {}

@Singleton
@Startup
@DependsOn("BeanA", "BeanB", "BeanC")
public class LastBean {
    @Inject @EjbStarted Event<String> event;

    @PostConstruct
    public void startService() {
        // At this moment PrimaryBean is ready for use
        event.fire("LastBean");
    }
}

public class CDIService {
    public void start(@Observes @EjbStarted String name) {
        if("LastBean".equals(name)) {
            startService();
        }
    }
}

UPDATE: While thinking about the problem, I somehow forgot that you want the initialization order in CDI beans, so the answer is somewhat out of context, sorry about that :)

UPDATE 2: Added how to make a CDI service start after EJBs

这篇关于Java EE 6:控制具有依赖项的托管Bean的启动:CDI,EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆