且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

控制EJB 3.1中的CDI启动

更新时间:2023-12-03 17:49:28

之后大量的研究我从IBM的人那里得到了一些帮助,因为我们正在使用WebSphere Application Server,我只需添加一个名为的属性:



com。 ibm.ws.cdi.immediate.ejb.start= true



到管理控制台中的WAS,他会确保一旦我愿意在我创建的@Startup bean中获取EJB @PostConstruct方法CDI容器已经启动并且已经注入。



它可以工作!!



以下是IBM网站中问题和解决方案的链接:



http://www-01.ibm.com/support/docview.wss?uid=swg1PM62774


I'm new in here and also new to the CDI world, and the first task I got in my job was to find out a way to controlled CDI uploading.

We are using both EJB 3.1 and CDI 1.0, and because they are controlled by different containers, we can control when and in what order the EJB Managed Beans will be up by using @Startup and @Singleton annotations.

But the @Inject CDI bean I have declared in my class is coming as null since the CDI Container hasn't started yet.

I have been trying for several days now to look up for solutions and the one I found here did not worked (still came as null).

We are using Java EE 6 and running the application on WebSphere Application Server 8.

Please, if you could help me find a way to control CDI uploading inside and regardless of the EJB?

here is a sample code of it:

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;

@Singleton
@Startup
public class BaseStartupLoader{


/**
 * Default constructor. 
 */
@Inject @MyStartup
BaseStartUp myStartup;

private static Logger m_logger = LoggerFactory.getLogger(BaseStartupLoader.class);
public BaseStartupLoader() {

}

@PostConstruct
public void init(){

    String applicationName = null;

    try {

            applicationName = myStartup.getClass().getName();
            myStartup.load();

    } catch (IllegalAccessException e) {
        m_logger.error("Faild to load data into preload system. "+e);               
    } catch (InstantiationException e) {
        m_logger.error("Faild to load data into preload system. "+e);               
    } catch (ClassNotFoundException e) {
        m_logger.error("Faild to load data into preload system - Class "+ applicationName + "Not found. "+e);               
    }
  }
}

Here is the BaseStartup Interface:

public interface BaseStartUp {
public void load() throws IllegalAccessException, InstantiationException, ClassNotFoundException;
}  

The Qualifier and Implementation:

@Retention(RetentionPolicy.RUNTIME)
@Target ({ElementType.PARAMETER, ElementType.FIELD, ElementType.TYPE, ElementType.METHOD})
@Qualifier 
@Dependent
public @interface MyStartup {   
}


@MyStartup
public class MyStartUpLoader implements BaseStartUp {

    @Inject
    SomeConfigLoader config;

    @Override
    public void load() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
    conifg.init();      
}   
}

After much research I found some help from the guys in IBM, since we are working with WebSphere Application Server I can just add a JVM Property called:

"com.ibm.ws.cdi.immediate.ejb.start" = true

to the WAS in the Admin console, and he will make sure that once I will get to the EJB @PostConstruct method in the @Startup bean I created the CDI Container will already be up and running and already injected.

It Works!!

Here is the link to the Problem and Solution in IBM site:

http://www-01.ibm.com/support/docview.wss?uid=swg1PM62774