0% found this document useful (0 votes)
61 views22 pages

EJB Session Presentation

Enterprise Java Beans (EJBs) are reusable server-side components that offer transactional and distributed services to Java applications. EJBs can be executed across multiple servers and provide services such as database access, security, and transaction management. The main EJB types are session beans for business processes, entity beans for database access, and message-driven beans for asynchronous messaging.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views22 pages

EJB Session Presentation

Enterprise Java Beans (EJBs) are reusable server-side components that offer transactional and distributed services to Java applications. EJBs can be executed across multiple servers and provide services such as database access, security, and transaction management. The main EJB types are session beans for business processes, entity beans for database access, and message-driven beans for asynchronous messaging.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

Enterprise Java Beans

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


What is enterprise bean?
 Server side reusable java component
 Offers services that are hard to implement by the
programmer
 Sun: Enterprise Bean architecture is a component
architecture for the deployment and development
of component-based distributed business
applications. Applications written using enterprise
java beans are scalable, transactional and multi-
user secure. These applications may be written
once and then deployed on any server plattform
that supports enterprise java beans specification.
 Enterprise beans are executed by the J2EE server.
Anand Kulkarni - IndicThreads.com Java Meet - May 2006
History
 EJB specifications were launched in 1997.
 First version EJB 1.0 contained session beans
and entity beans.
 In EJB 2.0 Message Driven Beans were
introduced.
 Current EJB specification is EJB 3.0

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Services
 Database management
– Database connection pooling
– DataSource, offered by the J2EE server. Needed to
access connection pool of the server.
– Database access is configured to the J2EE server ->
easy to change database / database driver
 Transaction management
– Distributed transactions
– J2EE server offers transaction monitor which can be
accessed by the client.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


... Services
 Security management
– Authetication
– Authorization
– encryption

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


... Services
 Enterprise java beans can be distributed /replicated
into separate machines.
 Distribution/replication is invisible to the
programmer
 Distribution/replication offers
– Load balancing, load can be divided into separate
servers.
– Failover, if one server fails, others can keep on
processing normally.
– Performance, one server is not so heavy loaded. Also,
for example Weblogic has thread pools for improving
performance in one server.
- Example, how distribution is supported by the Weblogic
J2EE server.
Anand Kulkarni - IndicThreads.com Java Meet - May 2006
When to choose EJB?
 Server will be heavy loaded
– Distribution of servers helps to achieve better
performance.
 Server should have replica for the case of failure
of one server.
– Replication is invisible to the programmer
 Distributed transactions are needed
– J2EE server offers transaction monitor that takes care
of transaction management.
– Distributed transactions are invisible to the
programmer.
Anand Kulkarni - IndicThreads.com Java Meet - May 2006
J2EE server offers
 DataSource.
– Object that can be used to achieve database
connection from the connection pool.
– Can be accessed by the interface DataSource
 Transaction monitor
– Can be accessed by the interface
UserTransaction.
 Java Naming and the Directory Service

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


XML – deployment descriptor
 Information how enterprise bean is deployed
– Session bean -> stateless vs. statefull
– Entity beans-> CMP vs. BMP
 Transaction attributes / security management
information
 Parameters to the enterprise bean (or other beans
or application)
 ejb-jar.xml + server-specific xml- file
 Packed in a jar – file together with bean classes.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Example, ejb-jar.xml
<?xml version=”1.0”?>
<!DOCTYPE ejb-jar PUBLIC
’-//sun microsystems, nc. // DTD Enterprise JavaBeans 1.1//EN’ ’ http://java.sun.com/j2ee/dtds/ejb.jar_1_1.dtd’>
<ejb-jar>
<enterprise-beans>
<session>
<ejb-name>CounterBean</ejb-name>
<home>seminar.example.CounterHome</ejb-name>
<remote>seminar.example.CounterRemote</remote>
<ejb-class>seminar.example.CounterBean<ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<env-entry>
<description>Malli</description>
<env-entry-name>numero</env-entry-name>
<env-entry-type>java.lang.Interger</env-entry-type>
<env-entry-value>1000</env-entry-value>
</env-entry>
</session>
</enterprise-beans>
</ejb-jar>
</xml>

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Session Bean
 Stateless
vs. Statefull
 Developer programs three classes:
– Home interface, contains methods for creating
(and locating for entity beans) bean instances.
– Remote interface, contains business methods
the bean offers.
– Bean class, contains the business logic of the
enterprise bean.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


An example, CounterBean

package seminar.example;
import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface Counter extends EJBObject {


public int countAddition(int num1, int num2) throws
RemoteException;
}

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


An Example, CounterBean
package seminar.example;

import java.rmi.RemoteException;
import javax.ejb.CreteException;

public interface CounterHome extends javax.ejb.EJBHome {


public Counter create() throws RemoteException,
CreateException;
}

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


An example, CounterBean
public class CounterBean implements SessionBean {
public SessionContext sessionContext = null;
public static final boolean DEBUG = true;
public void ejbCreate() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void ejbRemove() {}
public void setSessionContext(SessionContext ctx) {
sessionContext = ctx;
}
public int countAddition(int first, int second) throwRemoteException{
// ... implementation goes here
}
} Anand Kulkarni - IndicThreads.com Java Meet - May 2006
Entity Beans
 Represents one row in the database.
– Easy way to access database
– business logic concept to manipulate data.
 Container
managed persistence vs. bean
managed persistence.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Entity Beans
 Programmer creates three or four classes:
– Home interface for locating beans
– Remote interface that contains business methods for
clients.
– Bean class that implements bean’s behaviour.
– Primary key class – that represents primary key in the
database. Used to locate beans.
 Primary key class is not needed if primary key is a
single field that could be java class (for example,
Integer).

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Entity Beans
 Some application builders can generate
Entity beans from the database tables.
– For example, Jbuilder 6 enterprise edition has
an tool for creating entity beans.
 Entitybeans are effective when application
wants to access one row at a time. If many
rows needs to be fetched, using session
beans can be better alternative.
Anand Kulkarni - IndicThreads.com Java Meet - May 2006
Transactions
 CMP vs. BMP.
 CMP: Transactional behaviour in beans are
defined in transaction attributes of the
methods.
 BMP: Programmer has to write a code that
implements transactional behaviour to the
bean class.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Transactions
 J2EE server offers transaction monitor that
takes care of transactions.
 Transaction context can be accessed by
client or bean by UserTransaction interface.
– Client can fetch reference to the transaction
context by jndi.
– Bean can fetch reference to the transaction
context by Session-/EntityContext – object.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006


Transaction attributes
 Bean managed
– marks that the bean or the method manages it’s own
transactions by using UserTransaction interface.
 Required
– If client has started transaction, bean is associated to
the same transaction. If client has not started a
transaction, creates a new transaction.
 Supports
– If client had started a transaction, bean is associated to
the same transaction. Otherwice method is executed
without transaction.
Anand Kulkarni - IndicThreads.com Java Meet - May 2006
... Transaction attributes
 Requires new
– Creates a new transaction, when method is invoked.
Transaction last only that method call.
 Mandatory
– If transaction has not started before calling the method,
exeption is thrown.
 Never
– Exception is thrown, if method is called in transaction.

Anand Kulkarni - IndicThreads.com Java Meet - May 2006

You might also like