Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

why EJB's narrowed using PortableRemoteObject

why EJB's narrowed in the following mannor:

InitialContext ctx = new InitialContext();
Object obj = ctx.lookup(SomeObject);
SpecificObject so = (SpecificObject) PortableRemoteObject.narrow(obj, SpecificObject.class);
Avatar of girionis
girionis
Flag of Greece image

Because EJB must also support CORBA reference objects that cannot be cast by Java native casting.
Avatar of chaitu chaitu

ASKER

i didnt getu;

can u explain in detail
EJB obejcts can be transported by using RMI over IIOP protocol (RMI/IIOP). The RMI is used to transport Java object where Interent Inter-ORB Protocol is used to transport CORBA objects. IIOP is a generic protocol and does not have the concept of casting as it is in Java. Therefoer you will need to cast the references you receive over IIOP somehow. That's why you use the PortableRemoteObject.narror() method. For more information you might want to take a look here: http://www.jguru.com/faq/view.jsp?EID=734137
Avatar of vikraman_b
vikraman_b



we can either do a context.lookup or a PortableRemoteObject.narrow.
In which situations would u use one over the other.  Which is the best solution to use?

Let's start to clarify understanding EJB.

We can access EJB in two ways. One is by local interface and another is by remote interface. The different between local and remote interface is listed below

The benefit of remote interface is that it allow you to invoke a method on EJB over the network by using RMI(Remote Method Invocation). Sometime the invoked EJB is deployed on the same machine with the invoking EJB. So it does not make sense to call it over RMI which will have overhead in calling. To optimize this, Local interface allow us to access to an EJB by bypassing RMI. But we should keep in mind that local interface only work when EJB is located in the same application so it disallows load-balancing and failover feather.

Ok. back to your question.
We use PortableRemoteObject.narrow() when we write a client program(java application or another ejb) to access local objects(local interface is a subinterface of EJBLocalObject)

Here is an example for local interface.

HelloWorldLocalHome home =
  (HelloWorldLocalHome)ctx.lookup("HelloWorldLocal");
HelloWorldLocal hello = home.create();
System.out.println(hello.getMessage());

And Here is an example for remote interface.

HelloWorldHome home =
  (HelloWorldHome)PortableRemoteObject.narrow(ctx.lookup("HelloWorld"), HelloWorldHome.class);
HelloWorld hello = home.create();
System.out.println(hello.getMessage());


you will have clear idea...
http://www.cs.unc.edu/Courses/jbs/lessons/enterprise/ejb_jndi/

> We use PortableRemoteObject.narrow() when we write a client program(java
> application or another ejb) to access local objects(local interface is
> a subinterface of EJBLocalObject)

You mean *remote* objects I guess.
ASKER CERTIFIED SOLUTION
Avatar of drjustin
drjustin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial