views:

643

answers:

3

Say I have the following EJB (using ejb3):

@Stateless(name="Queries")
@Remote(Queries.class)
@Local(Queries.class)
public final class QueriesEJB implements Queries {
    ...
}

The class is available through both a local and a remote interface.

How can I inject the local interface for this EJB in another part of the app?

Specifically, I'm not sure how to create an @EJB annotation that selects the local interface. For example, is the following sufficient?

@EJB(name="Queries") private Queries queries;

In particular I want to avoid creating separate local and remote interfaces simply for the purpose of distinguishing via @EJB's 'beanInterface' property.

+1  A: 

When a EJB is deployed, the container looks at the interfaces and identifies local and remote interfaces. I would say that the EJB container already uses the local interface in your example. It simply does not make sence to use a remote interface in this case because the container has the choice to use the local one.

If you want to be sure try to use the JNDI name of the local interface as parameter of the @EJB annotation.

@EJB(name="java:comp/env/ejb/EntitySupplierLocal")

In the example above I added local to the interface name. In your case you have to take a look at the JNDI context to get the right name or you even know it ;).

Generally I recommend to use a base interface that has the business methodes defined and extend a local and a remote interface. So you do not have to duplicate methodes and you are able to extend functionality for local and remote access seperatly.

public interface Queries () { .. }

@Local
public interface QueriesLocal extends Queries () { .. }

@Remote
public interface QueriesRemote extends Queries () { .. }
Timo
I tried this in jBoss and the remote interface was selected - go figure...
johnstok
+1  A: 

According to the spec you cannot have an interface that is Remote and Local at the same time. However, you create a super-interface, put all methods there, and then create 2 sub-interfaces. Having done that, simply use @EJB. This way you need to maintain only one interface at all.

EDIT: See section 3.2 in "EJB3 spec simplified" at http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html

Antonio
Do you have the spec reference for that?
johnstok
1. Go to http://jcp.org/aboutJava/communityprocess/final/jsr220/index.html2. Click "Click on the button below to download the Specification for evaluation or to build an application that uses the Specfication"3. Download ejb-3_0-fr-spec-simplified.pdf4. Goto page 16, item 3.2
Antonio
A: 

Solutions from previous comments are not fully compatibile with EJB 3.0 on Jboss.

You can easy get this error org.jboss.ejb3.common.resolvers.spi.NonDeterministicInterfaceException: beanInterface specified, Queries, is not unique within EJB QueriesEJB

Create only this

public interface Queries () //Local by default

@Remote public interface QueriesRemote extends Queries () {

It works