0

I have multiple existing Stateful Session Beans. I want to use a new library/framework that instantiates objects (outside the container manager). This framework does not conceptually have 'sessions'. I'm trying to develop a proof of concept to route calling of beans in different session contexts based on a map of session identifiers to session contexts.

Below are 2 handlers that are instantiated by the library. The first handler creates new BoundSessionContexts and maps them to generated keys. The second handler is suppose to be passed the key and based on it propagate to the appropriate corresponding sessionContext, lookup the beans in that context, and call those beans.

public class Handler1 implements Handler1Interface{
    
    //**1**
    WeldManager weldManager = (WeldManager) CDI.current().getBeanManager();
    BoundSessionContext boundSessionCtx;
    
    //**2**
    Map<String, Object> sessionMap = new HashMap<>();
    
    public Handler1(){
    
    }
    
    //**3**
    @Override
    public long start(){
        long sessionId = new Random().nextLong();

        //**4**
        boundSessionCtx = weldManager.instance().select(BoundSessionContext.class, BoundLiteral.INSTANCE).get();
        
        //**5**
        //boundSessionCtx.associate(sessionMap);
        
        //Make certain the sessionId isn't already in use.
        while(SessionMapper.get(sessionId)!=null){
            sessionId = new Random().nextLong();
        }
            
        //**6**
        SessionMapper.put(sessionId, boundSessionCtx);
        
        return sessionId;        
    }
    
    //**7**
    @Override
    public void stop(long sessionId){
        SessionMapper.remove(sessionId);
    }
}
public class Handler2 implements Handler1Interface{
    
    //**8**
    @Inject
    EJB1 ejb1;
    
    //**9**
    @Inject
    EJB2 ejb2;
    
    BeanManager beanManager;
    
    BoundSessionContext boundSessionCxt;
    
    //**10**
    Map<String, Object> sessionMap = new HashMap<>();
    
    public Handler2(){
        
    }
    
    @Override
    public Object process(long sessionId, Object input){
        lookupEJBs(sessionId);
        
        //**11**
        ejb1.method();
        Object result = ejb2.method();
        
        
        return result;
                
    }
    
    //**12**
    private void lookupEJBs(long sessionId) {
        boundSessionCxt = SessionMapper.get(sessionId);
        boundSessionCxt.associate(sessionMap);
        boundSessionCxt.activate();
        
        beanManager = CDI.current().getBeanManager();
        
        //**13**
        TypeLiteral<EJB1> typeLiteral = new TypeLiteral<EJB1>() {};
        Set<Bean<?>> beans = beanManager.getBeans(typeLiteral.getType());
        Bean<?> bean = beanManager.resolve(beans);

        ejb1 = bean.create(boundSessionCxt);
        
        //**14**
        TypeLiteral<EJB2> typeLiteral2 = new TypeLiteral<EJB2>() {};
        beans = beanManager.getBeans(typeLiteral2.getType());
        bean = beanManager.resolve(beans);

        ejb2 = bean.create(boundSessionCxt);
        
        
}

I've never been able to call ejb2.method(). While I have used EJBs for many years this is my first attempt at manipulating contexts. I am definitively feeling lost. It's one thing to use the magic, it's another to warp that magic to your whim.

Questions (In no particular order): A) Is what I'm trying to do 'reasonably' acheivable? Or is this a pipe dream? NOTE: I am currently using WELD-API 3.0.SP4, but I am not married to it.

B) I have never truly understood the reason for the map(10) that is associated with a context(12). B1) What is it's purpose? B2) How does it's use affect what I'm trying to do here? B3) Am I correct in that I would want to associate and activate the context inside the object where I want to use the context beans?

C) Am I correct that @Inject (8 and 9) is pointless as the handler2 object is not instantiated/Injected by the bean manager.

Many thanks to anyone who can help me understand EJB/CDI session contexts better. :)

Lini
  • 345
  • 3
  • 9

0 Answers0