2

EJB 3.x

Consider following EJB implementing remote interface

@Remote
public interface UnusualRemote {
...
}

@Stateless
public class UnusualBean implements UnusualRemote{
...
}

The portable JNDI name exposed for Remote Interaface:

java:global/simplejee7/UnusualBean!com.example.UnusualRemote

My question is why should a client know the implementation class to lookup the bean via jndi. Specifically, why one can't lookup using just the interface name com.example.UnusualRemote

1
  • 1
    For local beans you can use the CDI bean manager to look up beans by their interface. Using a bridge bean you could extend that for remote calls.
    – Mike Braun
    Oct 6, 2014 at 18:42

3 Answers 3

1

I think the key point is that a remote interface can be implemented by several EJB, if the client doesn't specify the implementation class, the Container has no way to identify which one to choose.

1

This is indeed a silly thing. When the portable names were designed someone was clearly sleeping. The naming scheme is designed to name a particular bean implementation, but it didn't take the actual lookup usecase into account, which is always by interface if the bean indeed implements one.

I don't agree with Gabriel's answer. Of course it's possible to look up a bean by its interface. The container does exactly that when you inject into a field that has an interface as its type.

An other thing to take into account is that the "java:" namespace often goes back to the local JVM and can't be used for remote lookups. Remote lookups are very badly specified, but because of JAX-RS nobody wants to fix this anymore.

2
  • I think Gabriel is right. We cannot determine the actual implementation class instance using the interface only. There might be the case that EJB container contains two instances of object having same interface. But to differentiate between them we have to know the actual implementation class.
    – Hansraj
    Sep 16, 2014 at 13:36
  • @Hansraj think about the following: suppose what you said was true, how can the container inject an instance by interface then? Ie how can "@Inject private MyInterface my;" where "MyInterface" is an interface possibly work?
    – Mike Braun
    Oct 6, 2014 at 18:37
-1

This is how i think/what i know:-

In Java for example when you "implement/code to an interface" and when you "invoke a method"; you actually invoke on the object which is 'typed' to an interface; the interface exposing a view on the object.

The most critical meta information here is the identification of the object or the 'reach' to the object and its 'view'; which is precisely what gets stored in the JNDI Directory Service. (The JNDI tree basically stores proxies of the bean object which are castable to the interface name.)

The identification of the object here is the JNDI name you assign to the bean(or default if not assigned) which is a mandate without which you cannot reach/identify the object and the view of the object is the interface name.

Hope that answers. To add to that:-

More generally the syntax for determining the global portable jndi bean name is:- java:global[/<app-name>]/<module-name>/<bean-name>

If no interface name is specified it means the bean implements just one interface or has just one type/view and hence the interface name is optional.

However there can be cases where a bean exposes 'multiple client views/implements multiple interfaces'; in which case the portable JNDI name would look like this:-

java:global[/<app-name>]/<module-name>/<bean-name>/<intf-name>

2
  • thanks for reply but sorry do not agree, if you have to know the implementation detail, what's the point of interface
    – anergy
    Sep 22, 2014 at 11:07
  • maybe you are getting confused with <bean-name> or the "UnusualBean" in your post. We are using that name because no other bean name is specified using any annotation. For example @EJB(name="foobar") could be used and you would lookup that bean using "foobar" instead of your "UnusualBean". As far as your interface name is concerned; if your bean implemented multiple interfaces how would one know which view/interface to cast to or use without specifying it in the lookup.
    – M..
    Sep 22, 2014 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.