Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should be EJB Stateless Session Bean correctly injected into the web module?

Being completely new to Java EE (but not to Java itself) I'm trying to build a very simple "Enterprise Application" with Hibernate as JPA provider and JSF as the actual UI framework. For this purposes I'm using the NetBeans 7 with GlassFish 3.1.

{ApplicationName}-ejb:

I've accomplished to generate entity classes from database and local sesssion beans for these entities. Beans.xml is in place.

@Stateless
public class QuestFacade extends AbstractFacade<Quest> implements QuestFacadeLocal {
    // some methods here as well as EntityManager injection ...
}

{ApplicationName}-war:

I've created a simple POJO as a backing bean for the JSF page. I've annotated it with javax.inject.@Named and javax.enterprise.context.@SessionScoped. This backing bean is now accessible from the JSF page as well as being injected when the actual page is accessed. Beans.xml is in place as well.

@Named
@SessionScoped
public class QuestBean implements Serializable {

    @EJB
    protected QuestFacade questFacade;

    // several methods delegating lookups to the questFacade ...
}

Having this deployed and page accessed, I'm, however, getting an error from GlassFish that the QuestFacade cannot be looked up by the JNDI.

The stacktrace is quite long but the initial cause could be enough:

Caused by: javax.naming.NamingException: Lookup failed for 'model.session.QuestFacade#model.session.QuestFacade' in SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NameNotFoundException: model.session.QuestFacade#model.session.QuestFacade not found]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:518)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:455)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at com.sun.ejb.EjbNamingReferenceManagerImpl.resolveEjbReference(EjbNamingReferenceManagerImpl.java:173)
    ... 74 more
Caused by: javax.naming.NameNotFoundException: model.session.QuestFacade#model.session.QuestFacade not found
    at com.sun.enterprise.naming.impl.TransientContext.doLookup(TransientContext.java:248)
    at com.sun.enterprise.naming.impl.TransientContext.lookup(TransientContext.java:215)
    at com.sun.enterprise.naming.impl.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:77)
    at com.sun.enterprise.naming.impl.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:119)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:505)
    ... 78 more

I understand that I'm persuading GlassFish to inject an EJB from a different module within the same application. Should the @Remote interface be used instead? I've also tried to explicitely specify the name for both @Stateless and @EJB annotation but without any success.

I believe that I'm doing something fundamentaly wrong, but I cannot find out what.

Any suggestion or would be greatly appreciated!

like image 200
merxbj Avatar asked Jun 08 '11 00:06

merxbj


People also ask

What is stateless session bean in EJB with example?

A stateless session bean is a type of enterprise bean, which is normally used to perform independent operations. A stateless session bean as per its name does not have any associated client state, but it may preserve its instance state.

How does stateless session bean work?

Stateless Session Beans A stateless session bean does not maintain a conversational state with the client. When a client invokes the methods of a stateless bean, the bean's instance variables may contain a state specific to that client but only for the duration of the invocation.

What is stateless and stateful session bean in EJB?

An instance of a stateful session bean has a unique identity that is assigned by the container at create time. Stateless: A stateless session bean does not maintain conversational state. Instances of a stateless session bean have no conversational state.

What is the life cycle of session bean stateless session bean is initiated by?

Like a stateless session bean, a singleton session bean is never passivated and has only two stages, nonexistent and ready for the invocation of business methods, as shown in Figure 22-5. The EJB container initiates the singleton session bean lifecycle by creating the singleton instance.


1 Answers

I believe that I'm doing something fundamentaly wrong, but I cannot find out what.

What you're doing wrong is that if you implement a business interface (either @Local or @Remote), then you must declare the variable where injection takes place as having the type of that interface, not of the actual bean class.

So in your case:

@Named
@SessionScoped
public class QuestBean implements Serializable {

    @EJB
    protected QuestFacadeLocal questFacade;

    // several methods delegating lookups to the questFacade ...
}

However, a business interface is not required in EJB when you're doing local (in-jvm) communication. As you discovered, if you don't specify a business interface at all for your EJB, you can inject the bean class itself. This is because you then automatically get the so-called no-interface view.

If you want, you can optionally declare that you want BOTH the local view and the no-interface view. In that way, you can inject your bean class in places whether either the bean type itself is declared or its business interface. For this you use the @LocalBean.

@Stateless
@LocalBean
public class QuestFacade extends AbstractFacade<Quest> implements QuestFacadeLocal {
    // some methods here as well as EntityManager injection ...
}

Injection can thus happen in two ways now:

@Named
@SessionScoped
public class QuestBean implements Serializable {

    @EJB
    protected QuestFacadeLocal questFacade; // possible because of local view
    @EJB
    protected QuestFacade questFacadeN; // possible because of no-interface view

    // several methods delegating lookups to the questFacade ...
}

In practice I didn't found much use for having both methods available at the same time though, but maybe this adds to your understanding.

like image 178
Arjan Tijms Avatar answered Nov 15 '22 21:11

Arjan Tijms