Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EJB - Home/Remote and LocalHome/Local interfaces

Revising some past exam papers for an exam mainly focus on component-oriented design and J2EE, I have come across the following question:

A preliminary investigation of scenario 3: “Exchange Request” suggests that two EJBs will provide a suitable solution: a session bean called EnterExchangeRequest to control the processing and an entity bean called ExchangeRequest to represent the persistent properties of the request. Discuss the role of the following interfaces:

  • Home
  • Remote
  • LocalHome
  • Local

and how they would provide access to the services of the EJBs described above.

I could try to explain how Home and Remote interfaces would fit into the picture. I have also heard the lecturer say one could replace Home by LocalHome, and Remote by Local (why?), but why are they asking me to discuss the role of all four at the same time?


Do I get it right when I say, the EJB container (the application server) would see that an interface is Home or Remote and then decide that the bean can 'live' on any machine in the cluster, while in the case the interfaces are LocalHome and Local the container will know that the beans can't be distributed across multiple machines and will therefore keep them 'alive' in one machine only?


I am totally lost in this enterprise Java jungle. I am experiencing a BeanOverflow. Could you please tell me which of my assumptions are wrong, point out my misconceptions and blunders.

Thank you all who are willing to help me with these EJB interfaces.

P.S. Note that I am not asking you to answer the question from the past exam paper. Just curious if you have any thoughts as to what could they be after when asking this.

like image 397
Peter Perháč Avatar asked May 26 '09 23:05

Peter Perháč


People also ask

What is the difference between local and remote interface in EJB?

If an EJB component has a local interface, then local clients in the same application server instance can use it instead of the remote interface. Using the local interface is more efficient, since it does not require argument marshalling, transportation, and un-marshalling.

What is home interface in EJB?

The home interface is a contract between an EJB component class and its container, which defines construction, destruction, and lookup of EJB instances. An EJB home interface extends the interface javax. ejb. EJBHome , which defines base-level functionality for a home interface.

What is remote EJB?

javax.ejbDeclares the remote business interface(s) for a session bean. The Remote annotation is applied to the session bean class or remote business interface to designate a remote business interface of the bean.

What is home interface in Java?

The home interfaces are used to specify what methods a client uses to create or retrieve an entity bean instance. The home interface must contain a create method, which the client invokes to create the bean instance. The entity bean can have zero or more create methods, each with its own defined parameters.


2 Answers

Home is responsible for the creation of the Remote (kind of like its constructor) and LocalHome and Local have the same relationship.

In each case the container is giving you a proxy that references the real EJB class that you write.

If I had to guess, what the question was looking for was the use of remote for the session bean and local for the entity bean.

Anyway, although these concepts can still exists, things have been much better simplified in EJB3.

EDIT: In response to the comment, with EJB3, the bean class itself can implement the remote and the home interfaces directly (for the session beans). They are made EJB's with a single annotation. Stateful beans have a couple more annotations to deal with state issues. Entity beans do not have a Home interface, and do not need a local interface, you can interact with the java object directly. There is an EntityManager that retrieves the right entity beans based on a query, and that EntityManager is injected via an annotation.

That kind of sums it up in a paragraph. There are great tutorials on the web for this stuff, but EJBs in general solve a class of problem that is hard to appreciate unless you deal with the problem. They aren't the only way to solve it, but unless you deal with this type of programming, just reading about it won't really help you relate to it.

like image 71
Yishai Avatar answered Oct 18 '22 22:10

Yishai


As pointed out by Yishay, Home/Remote and LocalHome/Local are tied together and the Home interface functions as a constructor.

Local beans are tied to the JVM they live in, you can not access them from the outside. Remote beans can be accessed from other JVMs.

I use a similar approach: I always deploy ears. Beans for the ear I make local beans, Beans meant for use by other ears I make remote. But it is possible to use the local beans in other ears, as long as the are deployed in the same JVM

like image 36
Salandur Avatar answered Oct 18 '22 22:10

Salandur