Professional Documents
Culture Documents
Session 1 : Introduction
Session 2 : Session Beans
Session 3 : Entity Beans
Session 4 : MDBeans and Services
Stub The client calls a stub, which is a client-side proxy object. This
stub is responsible for masking networking communications from the
client. The stubs know how to call over the network using sockets,
messaging parameters as necessary into their network
representation.
EJBs have stubs and skeletons that are generated for marshalling.
Session Beans
--Stateless Beans
--Stateful Beans
Entity Beans
--Bean Managed Persistent Entity Beans
--Container Managed Persistent Entity Beans
Local Interfaces The clients call Enterprise Beans through their local
objects rather than remote EJB Objects. These local objects
implement local interface rather than a remote interface.
Enterprise Bean class This is the class which contains all the logic.
This is a simple java class that confirms to a well-defined interface
and obeys certain rules.
Primary Key The primary key is a very simple class that provides a
pointer into a database; Only entity beans need a primary key. This
class must implment java.io.Serializable (so the entity bean can
automatically be sent to persistent storage)
Vendor Specific Files Each EJB Server vendor may require that you
include additional files specific to that vendor, such as XML files, text
files or binary files.
EJB Jar File An EJB jar file is compressed file that contains everything
that we have described. It is the unit of deployment and is given to the
application server.
Primary key class is not needed if primary key is a single field that could
be java class (for example, Integer).
Enterprise Databases
EJB Client Java Bean
Invoke Business
Remote EJB
methods Object
of EJB Interface
Naming Service
Service (JTE)
Transaction
Messaging
Security
(JNDI)
Ejb home object is a lot like the Ejb object. Its another class that’s
generated automatically when you install an enterprise bean in a
container.It implements all the methods defined by the home interfaces
and is responsible for locating,creating and removing enterprise beans.
Server & Container Provider In most of the times the server and
container would be provided by a single vendor. The EJB container
(Application Server) is the runtime environment in which the beans live.
The container supplies middleware services to the beans and manage
them.
Stateless
bean
The create() method will not have any parameters , as the stateless
Does not
exist
ejbRemove
Method -ready
pool
Does not
exist
ejbPassivate
Method ready Passivate
ejbActivate
For example a container may swap beans out after some timeout
passes
passivation is the process of swapping a bean to storage
package learningpatterns.ejb.calculator;
import javax.ejb.*;
The bean class must define all the methods in the remote interface
They must have the same signature
RemoteException
These methods do the actual work of your bean
package learningpatterns.ejb.calculator;
import javax.ejb.*;
The bean class must define all the create methods in the home
interface
The name of these methods must be ejbCreate
They must be public and have the same arguments as in the home
interface
They must return void
package learningpatterns.ejb.calculator;
import javax.ejb.*;
that contains
A standard EJB deployment descriptor
The bean class, home interface class, and the remote interface
class
The second phase consists of deploying the standard EJB jar file
property file
Can describe many runtime attributes of a bean such as security
container
XML files are text files that include markup tags similar to HTML tags
Tags are matched with an end tag, and look like this
<TAG> Content </TAG>
We will not go very deeply into the structure of XML, as it is fairly
complex
We’ll cover enough to write deployment descriptors for EJB
<?xml version="1.0"?>
<remote>learningpatterns.ejb.calculator.Calculator</remote>
<ejb-class>
learningpatterns.ejb.calculator.CalculatorBean
</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>calculator</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
<home>
The fully qualified class name of the home interface
<remote>
The fully qualified class name of the remote interface
<ejb-class>
The fully qualified class name of the bean class (that implements
the business methods).
<session-type>
For a session bean describes if it is stateless or stateful
<transaction-type>
Defines the transaction management type of a bean (Container or
Bean)
<assembly-descriptor>
Defines how a bean is used in an application. Describes the
security roles and transactional attributes of a bean
<container-transaction>
Defines which transactional attributes apply to which methods
deployment descriptors
What these tools are like and the internal representation that a
The jar file packages all the classes and files that make up a bean
The home, remote and bean file
The Java SDK comes with a jar program to create jar files
<?xml version="1.0"?>
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD
WebLogic 5.1.0 EJB//EN'
'http://www.bea.com/servers/wls510/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>calculator</ejb-name>
<caching-descriptor>
<max-beans-in-free-pool>100</max-beans-in-free-pool>
</caching-descriptor>
<jndi-name>JavaConference.CalculatorHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
containing the original EJB code, the generated code, the standard EJB
deployment descriptor and the Weblogic-specific deployment descriptor
Use the lookup method of the Context interface to get a home interface
reference
public Object lookup(String name);
We have set up the following JNDI name in our Calculator bean jar
file
JavaConference.CalculatorHome
PortableRemoteObject.narrow(calcObject,
CalculatorHome.class);
Behind the scenes, the client is really talking to a stub for the
The EJB’s remote interface defines the methods that can be called from
a client
In the case of the calculator example, the interface contains an add and
a sub method:
public int add(int i1, int i2) throws
RemoteException;
public int sub(int i1, int i2) throws
RemoteException;
Sample Code fragment:
System.out.println(“6 + 5 = “ + calc.add(6,5));
System.out.println(“6 - 5 = “ + calc.sub(6,5));
Entity Beans are EJBs that present an object view of persistent data
and are
shared by multiple clients
transactional
Remote interface
Client’s view
Create methods
Finder methods
A remote interface
Business methods
A primary key
The home interface is still the bean “factory” for entity beans
But now you also have to take into account that there is
underlying data
For entity beans, create methods in the home interface add a new row
to the underlying data
They return a reference (EJBObject) to the bean just like with
session beans
They will likely take data which is used to create the bean
The name of each finder method prefix must start with find
ProcessingErrorException;
public double getBalance() throws
RemoteException;
public java.util.Date getOpenDate() throws
RemoteException;
public String getName() throws RemoteException;
}
You may also use a single specific field in the bean (starting with
EJB 1.1)
The home and primary key determine an object’s identity within a
container
The primary key class can be any serializable class
In general, the bean writer defines the primary key class, and is
getPrimaryKey() method
In EJB1.1, there are ways for the bean writer to defer defining the
package learningpatterns.ejb.bank;
import javax.ejb.*;
For CMP, the primary key class is tied to the bean implementation
Class must be public, and all fields in class must be public
They are public, have the same arguments as in the home interface
void return
For BMP
Should actually create row in database
For CMP
Should initialize the instance variables from the passed in
arguments
Doesn’t go to database
For BMP
There is a corresponding method in the bean class
This method starts with the prefix ejbFind and has the same suffix
part and arguments as in the home interface
The return value is a PrimaryKey or enumeration of
PrimaryKeys
The container constructs the EJBObjects from the primary key
and returns them to the client
public class AccountBean implements EntityBean {
// …
public Enumeration ejbFindByMinBalance (double
minBalance)
throws RemoteException, FinderException {}
}
For CMP
The implementations are written by the container
reacquire resources
setEntityContext should save the passed in context for future use
unsetEntityContext unsets the context
Sets it to null
the bean
When the container wants store the data in the bean into the
For CMP
You can put here any cleanup code, or release any resources that
For BMP
You would put database code to actually remove the bean
// … and so on
}
ejbCreate(…) insert
ejbfindByxxx(…) select
ejbRemove() delete
ejbStore() update
ejbLoad() select
The mapping between table columns and field names in the bean
weblogic-ejb-jar.xml
weblogic-cmp-rdbms-jar.xml
<assembly-descriptor>
...
</assembly-descriptor>
</ejb-jar>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>account</ejb-name>
<caching-descriptor>
<max-beans-in-cache>1000</max-beans-in-cache>
</caching-descriptor>
<persistence-descriptor>
<persistence-type>
<type-identifier>WebLogic_CMP_RDBMS</type-
identifier>
<type-version>5.1.0</type-version>
<type-storage>META-INF/weblogic-cmp-rdbms-
jar.xml</type...>
</persistence-type>
<persistence-use>
<type-identifier>WebLogic_CMP_RDBMS</type-
identifier>
<type-version>5.1.0</type-version>
</persistence-use>
</persistence-descriptor>
<jndi-name>JavaConference.AccountHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
<weblogic-rdbms-bean>
<pool-name>BankPool</pool-name>
<table-name>Account</table-name>
<attribute-map>
<object-link>
<bean-field>m_id</bean-field>
<dbms-column>ID</dbms-column>
</object-link>
<object-link>
<bean-field>m_name</bean-field>
<dbms-column>Name</dbms-column>
</object-link>
...
</attribute-map>
<finder-list>
<finder>
<method-name>findByMinBalance</method-name>
<method-params>
<method-param>double</method-param>
</method-params>
<finder-query><![CDATA[(>= m_balance $0)]]></finder-
query>
</finder>
</finder-list>
<options>
<use-quoted-names>false</use-quoted-names>
</options>
</weblogic-rdbms-bean>
Deployment descriptor
weblogic.properties file
Then you need to create a reference to it in the DD
files
Then you access it in your bean through JNDI using the reference
name in the DD
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>account</ejb-name>
….
<persistence-type>Bean</persistence-type>
<resource-ref>
<res-ref-name>jdbc/BankPool</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
Describes a resource reference
<res-ref-name>
JNDI name you will use to look up resource with
<res-type>
Fully qualified class name of the resource
<res-auth>
Authorization - container or bean
I.e. - will the bean writer need to pass in info like user/password, or
<?xml version="1.0"?>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>account</ejb-name>
<caching-descriptor>
<max-beans-in-cache>1000</max-beans-in-cache>
</caching-descriptor>
<reference-descriptor>
<resource-description>
<res-ref-name>
jdbc/BankPool
</res-ref-name>
<jndi-name>
weblogic.jdbc.jts.BankPool
</jndi-name>
</resource-description>
</reference-descriptor>
<jndi-name>JavaConference.AccountHome</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
<reference-descriptor>
Sets up a resource reference
<resource-description>
Sets up a particular resource
<res-ref-name>
Name of resource being set up
<jndi-name>
JNDI name to map this resource to
ctx.lookup("java:comp/env/jdbc/BankPool");
ctx.close();
con = ds.getConnection();
}
catch(SQLException sqle) {
throw sqle;
}
catch(Exception e) {
throw new SQLException(e.getMessage());
}
return con;
}
We’ll create a refresh method that loads the data given a primary key
This can be used from multiple methods
Name,
OpenDate from Account where ID = " + pk.m_id);
if (rs.next()) {
m_id = pk.m_id;
m_balance = rs.getDouble(1);
m_name = rs.getString(2);
m_open_date = rs.getDate(3);
}
else {
throw new FinderException ("Refresh: AccountBean (" +
pk.m_id + ") not found");
}
}catch (SQLException sqe) {
throw new RemoteException
(sqe.getMessage());
}
finally {
try {
if (stm != null)
stm.close();
if (con != null)
con.close();
}
catch (Exception ignore) {
}
}
}
Message Domains:
Publish/Subscribe: There can be many message producers
talking to many message consumers.Subscribers register their
interest in particular event topic.Providers create messages that are
distributed to all of the subscribers.
Point-to-Point: In this domain there will be only a single message
consumer for each messages. Multiple Producers can send
messages to the queue,but each message is delivered only to a single
consumer.
does not
exist
ejbRemove()
A middle man sits between the client and the server. And the client
does not need to wait when performing a request.
There can be more than one message producers as well as message
consumers.
Message Driven Beans do not have return Values.
They can not send exceptions back to clients.
import javax.naming.*;
import javax.jms.*;
public class Client{
public static void main(String args[]) throws Exception{
Context ctx = new InitialContext(System.getProperties());
queueConnectionFactory = (queueConnectionFactory) ctx.lookup
(“javax.jms.MyQueueConnectionFactory");
queueConnection =
queueConnectionFactory.createQueueConnection();
queueSession = queueConnection.createQueueSession(false,
Session.AUTO_ACKNOWLEDGE);
queue = (queue)
jndiContext.lookup("java:comp/env/jms/QueueName");
queueSender = queueSession.createSender(queue);
message = queueSession.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) { message.setText("This is
message " + (i + 1)); System.out.println("Sending message: " +
message.getText()); queueSender.send(message);
}
}
} Enterprise Java Beans
Bean Class
import javax.ejb.*;
Import javax.kms.*;
public class SimpleMessage implements
MessageDrivenBean,MessageListener{
protected MessageDrivenContext ctx;
Public void setMessageDrivenContext(MessageDrivenContext ctx){
this.ctx=ctx;}
public void ejbCreate(){}
public void onMessage(Message inMessage) {
TextMessage msg = null;
try {
if (inMessage instanceof TextMessage)
{msg = (TextMessage) inMessage;
System.out.println(" Message received: " + msg.getText()); }
else {
System.out.println ("wrong type: " +
inMessage.getClass().getName()); }
}
catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly(); }
catch (Throwable te) {
te.printStackTrace();
}
} public void ejbRemove(){}
}
}
<?xml version="1.0"?>
<!DOCTYPE ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD Enterprise
JavaBeans 1.1//EN' 'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<enterprise-beans>
<message-driven>
<ejb-name>SimpleBean</ejb-name>
<ejb-class>examples.SimpleBean,/ejb-class>
<transaction-type>container</transaction-type>
<message-driven-destination>
<destination-type>javax.jms.queue<destination-type>
</message-driven-destination>
</message-driven>
</enterprise-beans>
</ejb-jar>
Session 5 : Services
Database management
Database connection pooling
Transaction management
Distributed transactions
the client.
Security management
Authetication
Authorization
encryption
Authentication
Persistence
Naming
Concurrency
Load Balancing
Isolation Level:
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>SecureEJB</ejb-name>
...
<security-role-ref>
<role-name>InternalTrusted</role-name>
<role-link>TrustedUser</role-link>
</security-role-ref>
</entity>
</enterprise-beans>
<assembly-descriptor>
<security-role>
<role-name>TrustedUser</role-name>
</security-role>
<method-permission>
<role-name>TrustedUser</role-name>
<method>
<ejb-name>SecureEJB</ejb-name>
<method-name>doSecureStuff</method-name>
</method>
</assembly-descriptor>
</ejb-jar>
Connection pools
Load Balancing
Maximizes the usage of the computing resources of all of your
server machines
Essential to ensuring that the application is scalable
Fail over
A request may be redirected from one server to another because of
OC4J
EJB EJB
home EJB container
home
interface object
EJB client SB
bean
EJB EJB
comp. Local/
Entity
interface remote
JMS client bean
object
Database
JMS
Listener MDB Or
Deployment bean
descriptors External
Apps
Enterprise Services
Naming, Transactions, Security