Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling CDI Startup inside EJB 3.1

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();      
}   
}
like image 538
tomerne Avatar asked Dec 24 '12 08:12

tomerne


1 Answers

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

like image 109
tomerne Avatar answered Sep 21 '22 07:09

tomerne