Misra’s blog

EJB interview questions

Posted by mtwinkle on May 18, 2006

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/Session2.html
To construct session bean , you need the following code:
1. Session bean class
2. Home interface
3. Remote interface

The session bean class must have:
a. It implements the SessionBean interface.
b. The class is defined as public.
c. The class cannot be defined as abstract or final.
d. It implements one or more ejbCreate methods.
e. It implements the business methods.
f. It contains a public constructor with no parameters.
g. It must not define the finalize method.

The SessionBean interface extends the EnterpriseBean interface, which in turn extends the Serializable interface. The SessionBean interface declares the ejbRemove, ejbActivate, ejbPassivate, and setSessionContext methods. The Session Bean class doesn’t use these methods, but it must implement them because they’re declared in the SessionBean interface. Consequently, these methods are empty in the Session Bean class.

Because an enterprise bean runs inside an EJB container, a client cannot directly instantiate the bean. Only the EJB container can instantiate an enterprise bean.

a. The client invokes a create method on the home object:
Cart shoppingCart = home.create(“Duke DeEarl”,”123″);

b. The EJB container instantiates the enterprise bean.
c. The EJB container invokes the appropriate ejbCreate method in CartBean:

Q. What is an EJB?
Ans. EJB is a server-side component architecture for the development and deployment of distributed object systems for the Java platform. Applications written using the EJB architecture are scalable, transactional, and multi-user secure.

Q. OOP vs Component based programming
Ans. OOP allows reuse of classes
CBP allows reuse of functionalities

Java –> Write once Run anywhere
EJB –> Write once Deploy anywhere

Q. EJB’s – How do they work?
Ans.
1. Client never talks directly to the bean.
2. It must get reference to the remote interface which is retrieved using Home interface which is got via JNDI lookup.
3. Container checks everything (security, transaction etc.)
4. Container instantaites (or retrieve frm pool) the bean
5, Container calls the bean method

Q. Bean development process
1. Code the bean class
2. Code the two interfaces (Home and Remote)
3. Create a DD (ejb-jar.xml)- tells what the bean is and how to handle it
4. Package in an ejb jar file (must have DD and beans)
5. Deploy on the server

Q. Code the bean class:
1. Choose the bean type and implement the associated interface
2. Implement container callback methods of the interface (allows container to handle bean lifecycle)
3. Implement ejbCreate() method
4. Create business logic methods

Q. Home interface
1. extends EJBHome interface
2. holds a create() method which throws javax.ejb.CreateException and java.rmi.RemoteException

Q. Remote interface
1. Extends EJBObject interface
2. Holds BL methods throw java.rmi.RemoteException

Q. Steps to access a bean frm the client:
1. get a reference to the JNDI intial context
2. use initial context to lookup for the Home interface
3. call the create() method on the home object to get a reference to the Remote interface of the bean
4. call the business method

Q. EJB Architecture
1. Bean Home

a. Session Beans and Entity Beans have a Home. MDB’s do not have a Home, as they have no client.
b. There is only one Home Object for each bean type, no matter how many EJBObjects are created

Q. Stateless vs Stateful session beans
Ans. Stateless dosen’t remember a conversational state while stateful remember.
Stateless have only one create() method while stateful can have multiple create() methods.

Q. EJB roles
Ans. Bean provider, application assembler, deployer and container provider

Q. Creation process of Stateless session beans
Ans.
Developer creates Remote and Home interfaces
Container creates the EJBObject and its stub and EJBHome object and its stub.
There is one Home object and each client has stub to it
There is one EJBObject and each client has stub to it.
Stateless session beans reside in pools. They may serve different clients but never at the same time.
client calls create() on the home stub
stub sends create to the EJBHome object
EJBObject is created
Bean is instantiated and linked to EJBObject
EJBObject stub is returned.

Q. Creation process of Stateful session beans
Ans.
Developer creates Remote and Home interfaces
Container creates the EJBObject and its stub and EJBHome object and its stub.
There is one Home object and each client has stub to it
Each client gets its own bean and EJBObject

Context ic = new InitialContext(); //get the initial context
MyBeanHome home = ic.lookup(“foo/MyBean”);
MyBeanRemote remote = home.create(); // returns stub to EJBObject
String s = remote.businessmethod();

Q. Explain isIdentical method?
Ans.
Stateless session beans are identical if both references are retrieved from the same Home.
Stateful session beans are never identical
Entity beans are identical if the two entities have the same primary key.

Q. Passivation
Ans. Passivation occurs when the bean stays inactive for a set period of time. Passivation occurs only to the stateful session beans. When a bean is passivated, its instance variables must be serialized to be retrieved at activation time.

Message Driven Bean
1. SimpleMessageClient: A J2EE application client that sends several messages to a queue.
2. SimpleMessageEJB: A message-driven bean that asynchronously receives and processes the messages that are sent to the queue.

SimpleMessageClient
—————————–
queueConnectionFactory = (QueueConnectionFactory)
jndiContext.lookup
(“java:comp/env/jms/MyQueueConnectionFactory”); // lookup connectionfactory
queue = (Queue)
jndiContext.lookup(“java:comp/env/jms/QueueName”); // lookup Queue
queueConnection =
queueConnectionFactory.createQueueConnection(); //create queue connection
queueSession =
queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE); // create queue session
queueSender = queueSession.createSender(queue); // create queue sender

message = queueSession.createTextMessage();

for (int i = 0; i SessionBean interface extends the EnterpriseBean interface, which in turn extends the Serializable interface. The SessionBean interface declares the ejbRemove, ejbActivate, ejbPassivate, and setSessionContext methods. The CartBean class doesn’t use these methods, but it must implement them because they’re declared in the SessionBean interface.
The ejbCreate Methods
Because an enterprise bean runs inside an EJB container, a client cannot directly instantiate the bean. Only the EJB container can instantiate an enterprise bean. During instantiation, the example program performs the following steps.
The client invokes a create method on the home object:
Cart shoppingCart = home.create(“Duke DeEarl”,”123″);
shoppingCart.addBook(“The Martian Chronicles”);
The EJB container instantiates the enterprise bean.
The EJB container invokes the appropriate ejbCreate method in CartBean:
Typically, an ejbCreate method initializes the state of the enterprise bean
To indicate a system-level problem, such as the inability to connect to a database, a business method should throw the javax.ejb.EJBException.
Because EJBException is a subclass of RuntimeException, you do not need to include it in the throws clause of the business method.

Home Interface
import java.io.Serializable;
import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;

public interface CartHome extends EJBHome {
Cart create(String person) throws
RemoteException, CreateException;
Cart create(String person, String id) throws
RemoteException, CreateException;
}
The signatures of the ejbCreate and create methods are similar,

Remote Interface
import java.util.*;
import javax.ejb.EJBObject;
import java.rmi.RemoteException;

public interface Cart extends EJBObject {

public void addBook(String title) throws RemoteException;
public void removeBook(String title) throws
BookException, RemoteException;
public Vector getContents() throws RemoteException;
}

Q. When should I adopt BMP and when I should use CMP?
You can use CMP and BMP beans in the same application… obviously, a bean can be BMP or CMP, not both at the same time (they are mutually exclusive).

There is a common approach that is normally used and considered a good one. You should start developing CMP beans, unless you require some kind of special bean, like multi-tables, that cannot be completely realized with a single bean. Then, when you realize that you need something more or that you would prefer handling the persistence (performanbce issue are the most common reason), you can change the bean from a CMP to a BMP.

Q. Can I map more than one table in a CMP?
Ans. No, you cannot map more than one table to a single CMP Entity Bean. CMP has been, in fact, designed to map a single table.

Q. Does Stateful Session bean support instance pooling?
Ans. Stateful Session Bean conceptually doesn’t have instance pooling.

Both Stateless and Stateful Session Bean implement javax.ejb.SessionBean

Q. What is the difference between Context, InitialContext and Session Context? How they are used?

Q. Can I develop an Entity Bean without implementing the create() method in the home interface?
Ans. As per the specifications, there can be ‘ZERO’ or ‘MORE’ create() methods defined in an Entity Bean. In cases where create() method is not provided, the only way to access the bean is by knowing its primary key, and by acquiring a handle to it by using its corresponding finder method.

Q. What is the need of Remote and Home interface. Why cant it be in one?
Ans. The main reason is because there is a clear division of roles and responsabilities between the two interfaces.
The home interface is your way to communicate with the container, that is who is responsable of creating, locating even removing one or more beans.
The remote interface is your link to the bean, that will allow you to remotely access to all its methods and members.

Q. Services provided by the EJB container to the bean.
Ans.
1. Transaction Management
2. Security
3. Life cycle management
4. Client session management
5. Database connection pooling
6. Persistence

Q. What is a Entity Bean?
Ans. An entity bean represents a business object in a persistent storage mechanism such as database.
For eg. An entity bean could represent a customer which might be stored as a row in the customer table of a relational database.

Entity bean should have:
Class attributes, business methods, primary key and finder methods.

Q.

One Response to “EJB interview questions”

  1. hello, your website is really good. We do appreciate your give good results 252231

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: