Introduction to EJB

Enterprise Java beans -is a managed, server-side component architecture for modular construction of enterprise applications. EJB server is a high-level process or application that provides a run-time environment to support the execution of server applications that use enterprise beans. Enterprise beans live in an EJB container (a runtime environment within a J2EE server). The EJB container provides multiple services to support the enterprise beans.

Different types of EJBs
There are three types of enterprise beans: session beans, entity beans, and message-driven beans.

1. Session beans: Session beans are non-persistent enterprise beans. They can be stateful or stateless. A stateful session bean acts on behalf of a single client and maintains client-specific session information (called conversational state) across multiple method calls and transactions. It exists for the duration of a single client/server session. A stateless session bean, by comparison, does not maintain any conversational state. Stateless session beans are pooled by their container to handle multiple requests from multiple clients.

a) Stateless session EJBs
Stateless session EJBs are the preferred type of session EJB, since they
generally scale better than stateful session EJBs. Stateless beans are pooled by
the EJB container to handle multiple requests from multiple clients. In order to
permit this pooling, stateless beans cannot contain any state information specific to a particular client. Because of this restriction, all instances of a stateless bean are equivalent, allowing the EJB container to assign an instance
to any client.
B)Stateful session EJBs
Stateful session EJBs are useful when an EJB client needs to call several
methods and store state information in the session bean between calls. Each
stateful bean instance must be associated with exactly one client, so the
container is unable to pool stateful bean instances.

2. Entity beans: Entity beans are enterprise beans that contain persistent data and that can be saved in various persistent data stores. Each entity bean carries its own identity. Entity beans that manage their own persistence are called bean-managed persistence (BMP) entity beans. Entity beans that delegate their persistence to their EJB container are called container-managed persistence (CMP) entity beans.
A).Container-managed persistence (CMP)
The EJB container handles all database access required by the entity bean. The
bean’s code contains no database access (SQL) calls. As a result, the bean’s
code is not tied to a specific database. Because of this flexibility, even if you
redeploy the same entity bean on different J2EE servers that use different
databases, you do not have to modify or recompile the bean’s code. The
container must provide an object-relational mapping tool to allow a developer or
deployer to describe how the attributes of an entity bean map onto columns in
tables of a database.

B)Bean-managed persistence (BMP)
The developer handles all storage-specific access required by the entity bean.
This allows the developer to use non-relational storage options and features of
relational databases that are not supported by CMP entity beans, such as
complex SQL and stored procedures.

3.) Message-driven beans: Message-driven beans are enterprise beans that receive and process JMS messages. Unlike session or entity beans, message-driven beans have no interfaces. They can be accessed only through messaging and they do not maintain any conversational state. Message-driven beans allow asynchronous communication between the queue and the listener, and provide separation between message processing and business logic.

EJB container
An EJB container is a run-time environment that manages one or more enterprise beans. The EJB container manages the life cycles of enterprise bean objects, coordinates distributed transactions, and implements object security. Generally, each EJB container is provided by an EJB server and contains a set of enterprise beans that run on the server.

pimp it

Different ways of creating object in Java

1. Using new keyword
This is the most common way to create an object in java.In This we will be using the new operator which will allocates the memory space and initialize the field with default value. Then it executes the code inside the specified constructor which normally re-writes the default values with the value inside the constructor.

 
MyObject object new MyObject();

2. Using Class.forName()

If we know the name of the class & if it has a public default constructor we can create an object in this way.

 
MyObject object (MyObject) Class.forName( vinay.abc.MyObject ).newInstance();

vinay.abc.MyObject – is the class name

3. Using clone()

The clone() can be used to create a copy of an existing object. Object.clone() is a native method which translates into instructions for allocating the memory and copying the data.Even if you override the clone method then also you need to call the super.clone() method inside the overridden clone method. In this method a copy instruction is called and which copy the data from original object to clone object.

MyObject anotherObject new MyObject();
MyObject object anotherObject.clone();
            

4. Using object deserialization

Object deserialization is nothing but creating an object from its serialized form. It will uses the ‘new’ operator internally and always calls the default constructor.

 ObjectInputStream inStream new ObjectInputStream(anInputStream );
MyObject object (MyObject) inStream.readObject();

5. Using class loader

one more is through creation of object using classloader
like

this.getClass().getClassLoader().loadClass( com.amar.myobject ).newInstance();

pimp it

Reflection Api in java

The Reflection API allows Java code to examine classes and objects at run time. The new reflection classes allow you to call another class’s methods dynamically at run time. With the reflection classes, you can also examine an instance’s fields and change the fields’ contents.
Reflection is commonly used by programs which require the ability to examine or modify the runtime behavior of applications running in the Java virtual machine. This is a relatively advanced feature and should be used only by developers who have a strong grasp of the fundamentals of the language. With that caveat in mind, reflection is a powerful technique and can enable applications to perform operations which would otherwise be impossible.

Uses of Reflection

Extensibility Features
An application may make use of external, user-defined classes by creating instances of extensibility objects using their fully-qualified names.

Class Browsers and Visual Development Environments
A class browser needs to be able to enumerate the members of classes. Visual development environments can benefit from making use of type information available in reflection to aid the developer in writing correct code.

Debuggers and Test Tools
Debuggers need to be able to examine private members on classes. Test harnesses can make use of reflection to systematically call a discoverable set APIs defined on a class, to insure a high level of code coverage in a test suite.

Drawbacks of Reflection
Reflection is powerful, but should not be used indiscriminately. If it is possible to perform an operation without using reflection, then it is preferable to avoid using it. The following concerns should be kept in mind when accessing code via reflection.

Performance Overhead
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.

Security Restrictions
Reflection requires a runtime permission which may not be present when running under a security manager. This is in an important consideration for code which has to run in a restricted security context, such as in an Applet.

Exposure of Internals
Since reflection allows code to perform operations that would be illegal in non-reflective code, such as accessing private fields and methods, the use of reflection can result in unexpected side-effects, which may render code dysfunctional and may destroy portability. Reflective code breaks abstractions and therefore may change behavior with upgrades of the platform.

References
http://java.sun.com/docs/books/tutorial/reflect/
http://www.javacommerce.com/displaypage.jsp?name=index.sql&id=18272

pimp it