-
1. Re: Session bean injection
lafr Jan 13, 2013 12:20 PM (in response to ran_inc)As far as I know, this is not possible. Because of this I continued to go with EJB for EJB-injection.
From http://www.adam-bien.com/roller/abien/entry/inject_vs_ejb:
Sometimes it may be necessary to use a "self" reference to an EJB. You could inject a reference to "self" with @EJB, but not @Inject.
-
-
3. Re: Session bean injection
ran_inc Jan 14, 2013 4:14 AM (in response to lafr)when I use @ejb - I get org.jboss.as.server.deployment.DeploymentUnitProcessingException: JBAS014544: No EJB found with interface of type 'javax.enterprise.inject.Instance' for binding com.windriver.dsm.labmanagement.ejb.session.GeneralSessionBean/generalSessionInstance.
-
4. Re: Session bean injection
sfcoy Jan 14, 2013 4:29 AM (in response to ran_inc)I think it would help to know exactly what you're trying to accomplish here.
-
5. Re: Session bean injection
ran_inc Jan 14, 2013 4:44 AM (in response to sfcoy)I want to inject a session bean to another session bean.
specificly I inject the self session bean in order to call another method in a different transaction attribute.
but anyways why does the inject fail?
-
6. Re: Session bean injection
sfcoy Jan 14, 2013 4:54 AM (in response to ran_inc){code:java}
@Stateless
@Local(GeneralSessionLocal.class)
@Remote(GeneralSession.class)
public class GeneralSessionBean extends CRUDSessionBase
{
@Resource
private SessionContext sessionContext;
// There's no point in this being the remote interface
private GeneralSessionLocal self;
@PostConstruct
void initialise() {
self = sessionContext.getBusinessObject(GeneralSessionLocal.class);
}
}
{code}
This "trick" has been around at least ten years I think....
-
7. Re: Session bean injection
sfcoy Jan 14, 2013 4:56 AM (in response to ran_inc)ran haim wrote:
but anyways why does the inject fail?
It fails because CDI beans and EJBs have different life cycle management.
-
8. Re: Session bean injection
ran_inc Jan 14, 2013 5:17 AM (in response to sfcoy)Thanks Stephen!
I have a follow up questions (which might be considered to be one question):
1. If I want to initialize the session bean only In a sepecific method - this is why I used Instance<GeneralSession>.
2. Also How can I inject a different session bean (not self), and also make sure it's injected only when I use it (like Instance capabliblities)?
-
9. Re: Session bean injection
sfcoy Jan 14, 2013 5:27 AM (in response to ran_inc)ran haim wrote:
...
1. If I want to initialize the session bean only In a sepecific method - this is why I used Instance<GeneralSession>.
You can invoke
self = sessionContext.getBusinessObject(GeneralSessionLocal.class);
from any method you like. It doesn't have to be the way I did it.
But if you do it my way it's guaranteed to be set up whenever you need it and the cost is tiny. There's no chance of your code generating NPEs this way. Always initialise variables as soon as you can.
2. Also How can I inject a different session bean (not self), and also make sure it's injected only when I use it (like Instance capabliblities)?
You can't inject it (using the APIs currently available). You need to perform a JNDI lookup to get EJB instances dynamically.
Typically you would do this when using remote EJBs that may be unavailable for some reason.
-
10. Re: Session bean injection
ran_inc Jan 14, 2013 5:55 AM (in response to sfcoy)I am pretty sure that
@Inject
can inject any managed bean and is a part of the new CDI specification (since Java EE 6) - which should include session beans.Am I wrong?
-
11. Re: Session bean injection
nickarls Jan 14, 2013 5:59 AM (in response to ran_inc)Can you do a plain @Inject @New (without Instance<>)?
-
12. Re: Session bean injection
sfcoy Jan 14, 2013 6:07 AM (in response to ran_inc)In retrospect (caused by going back and reading the spec again) maybe this should work. Does your deployment contain a META-INF/beans.xml file?
Also, try injecting the local interface as I suspect that trying to inject a remote is being hopeful.
-
13. Re: Session bean injection
lafr Jan 14, 2013 6:11 AM (in response to ran_inc)I can confirm that using self references works with @EJB for JBoss AS7.
I use this in a number of beans.
@Stateless
public class MessageEraser extends EraserBase
{
@EJB
private MessageEraser messageEraser;
@Timeout
public void ejbTimeout( final Timer timer ) {
...
this.messageEraser.removeEntity( id );
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void removeEntity( final Integer key )
{
...
}
}
Before this, I had to use Context.
getBusinessObject().
-
14. Re: Session bean injection
sfcoy Jan 14, 2013 6:11 AM (in response to lafr)I would have thought that this would recurse to death.
Is this behaviour a happy accident or is it spec defined somewhere?