Wednesday, August 27, 2008

EJB Interview Questions

1. What is EJB

EJB is a enterprise component, part of J2EE framework, which allows to create independant components to implement business logic or represent Data.

2. What are the Advantages of EJB
  • Component Driver architecture, so plug and play to any project
  • With local , remote interfaces present, the EJB itself does not have to know who the client is whether an RMI or local Servlet or something else. Developer have to focus on writing business logic, rest everthing like Remote access, Session management , Security is taken care by the framework.
3. Different Types of EJB
  • Session Beans - It has two types : Statefull and Statless session beans
    Stateless Session Beans : They behave like POJO classes, just write methods, they do not maintain session or state across the requests from the user.
Following is a Simple Example of EJB3.0 Stateless Session bean
Step1 : Write the Local and Remote Interfaces
package com.abc; import javax.ejb.Remote; @Remote public interface CalcBeanRemote { public int getRate(); }

Step2 : Write the Stateless Session Bean
package com.abc; import javax.ejb.Stateless;
@Stateless public class CalcBean implements CalcBeanRemote, CalcBeanLocal { /** * Default constructor. */ public CalcBean() { // TODO Auto-generated constructor stub } public int getRate(){ return 75; } }
  • Entity Beans : Provides the domain as well as DAO layer to the enterprise application. In EJB2.0 with CMP and BMP they were too complicated, Hence Hibernate was used for the database Interaction.
  • With EJB3, JPA has been introduced which is even better than Hibernate as it uses annotations. There is a good article on Java world about JPA. http://www.javaworld.com/javaworld/jw-01-2008/jw-01-jpa1.html
  • Message Driven Beans (MDB) : They execute the business logic on the OnMessage(Message obj) method , they listen to JMS Queues to get invoked. In the Deployment Descriptor(ejb-jar.xml , we mention the Queue to listen to)
4. How to invoke an EJB from a client e.g Servlet or Struts Action class or any other POJO.
Following are the steps to invoke a Ejb method
  • 1.Get hold of EJB HOME through JNDI Lookup.
EJBHome home = ctx. loopup("java:comp//ejbname);
Note: EJBHome has all the create methods to create the EJBObject

  • 2. Create instance of EjbObject from Home
EJBObject ejbObject = home.create();
Note: ejbObject is an interface and just provides the signatures of the all the methods in EJB itself.

  • 3. Invoke the method on the EJB.
Object result = ejbObject.convertDegreeToCentri("20);


5. What are Local and Remote interfaces in EJB and their importance..

Since EJBObjects are the interfaces which expose themselves to the client, for a remote client, we need a Remote Interface. And for local clients like Servlets we need Local Interfaces.
We two different because, we do not want have a overload of a Remote interface if the invocation is loca.

6. What version of EJB you have used?

I have used EJB2.x, lates is EJB 3.0 which makes heavy use of Annotations, thus eliminating the need of Home and EJBObject interfaces.

7. What is the Directory Structure of EJB Jar.
EjbProject --> EjbHome.java
EjbObject.java
Ejb.java
ejb-jar.xml (Deployment Descriptor which metions various settings like EJB Type, connection params, Listening queue etc)

Wednesday, August 13, 2008

Core Java Interview Questions - Differene Questions

1. Difference between Vector and ArrayList
2. Difference between Array and ArrayList




Answers
1. Vector and ArrayList?
  • All methods in vector are synchronized while in Arraylist they are not synchronized(not threadsafe)
  • When Vector reaches the end of its size, it doubles its size to accomdate new objects. whereas Arraylist grows by 50% of initial size.
2. Array and ArrayList?
  • Array is can store only a set similar objects and its size is fixed
  • ArrayList is internally an error, but can grow dynamically (so size is not fixed), its not synchronized.