Escolar Documentos
Profissional Documentos
Cultura Documentos
Master of Information
Master of Information
Entity beans
Message Driven
Beans
95-702 OCT
Master of Information
95-702 OCT
Master of Information
95-702 OCT
Master of Information
Master of Information
Master of Information
Benefits of Enterprise
Beans(1)
Simplify development of large,
distributed applications.
The developer can concentrate on
business problems.
The EJB container handles
transaction management, security,
and authorization.
Portable may be moved to other
Java EE containers.
95-702 OCT
Master of Information
Benefits of Enterprise
Beans(2)
Scalable the components may be
distributed across many machines.
Support for Location Transparency
the client need not be concerned with
the location of the bean.
Support for Transaction management ensures data integrity (over concurrent
access of shared resources).
Promotes thin clients and web services.
95-702 OCT
Master of Information
stub
skeleton
95-702 OCT
Master of Information
10
Master of Information
11
Server-Side Implications
In order to pull off the RPC trick we need:
A naming service
-- e.g. RMI clients make requests on the
rmiregistry
RPC proxies
-- communications code along with
the appropriate interface
95-702 OCT
Master of Information
12
95-702 OCT
Master of Information
13
EJB Types
Entity Beans
Session Beans
Message-Driven Beans
95-702 OCT
Master of Information
14
EJB Types
Entity Beans
Session Beans
Message-Driven Beans}
95-702 OCT
Master of Information
15
Entity Beans
Represent real world entities (customers,
orders, etc.)
Persistent Objects typically stored in a
relational database using CMP (Container
Managed Persistence) or BMP (Bean
Managed Persistence)
The client sees no difference between CMP
and BMP beans
CMP promotes component portability (more
reliant on the container to handle detail)
CMP uses its own Query Language EJB QL
CMP relies on an Abstract Schema in the
deployment descriptor
95-702 OCT
Master of Information
16
Session Beans
Master of Information
17
Message-Driven Beans
Work in cooperation with Java Messaging System
(JMS).
JMS is an abstraction API on top of Message-Oriented
Middleware (MOM) like JDBC is an abstraction API
on top of SQL databases or like JAXR is an
abstraction API on registries or like JNDI is an
abstraction API on directories.
Each MOM vendor implements things differently.
MDBs allow the developer to program using the
publish-subscribe messaging model based on
asynchronous, distributed message queues.
The MOM vendor need only provide a service
provider for JMS (IBMs MQSeries or Progress
SonicMQ).
95-702 OCT
Master of Information
18
Message-Driven Beans
Are like session beans
Have no persistent state
Coordinate tasks involving other
session beans or entity beans
Listen for asynchronous messages
Unlike Session beans, provide no
remote interface describing the
methods that can be called
95-702 OCT
Master of Information
19
Message-Driven Beans
Are receivers of MOM messages
coming through the JMS API.
Usually take action when a
message is received.
Unlike session and entity beans,
Message-Driven Beans expose no
remote or local view. They are not
called directly by a client.
95-702 OCT
Master of Information
20
95-702 OCT
Master of Information
21
Master of Information
22
Master of Information
23
95-702 OCT
Master of Information
24
95-702 OCT
Master of Information
25
95-702 OCT
Master of Information
26
95-702 OCT
Master of Information
27
Point-to-point on the
Client Side
import javax.jms.*;
QueueConnection qCon;
QueueSession qSes;
QueueSender qSen;
Through JNDI get access to a QueueSender.
Build messages and send them to the queue.
The queue is responsible for transporting the
message to another queue on the server.
95-702 OCT
Master of Information
28
95-702 OCT
Master of Information
29
Master of Information
30
Relationships
OrderEJB
1
CusomerEJB
Many
Many
LineItemEJB
ProductEJB
Many
Master of Information
31
Client initiates
Container
initiates
unsetEntityContext
Pool of available
instances
Client calls
ejbPassivate
ejbActivate
create and
container calls
-- Ready to have
ejbCreate
business methods called
and ejbPostCreate
-- Has an identity
Client calls
remove and
container calls
ejbRemove
95-702 OCT
Master of Information
32
95-702 OCT
Master of Information
33
A Home Interface
import javax.ejb.*;
// From Eckel
import java.util.Collection;
import java.rmi.RemoteException;
public interface MovieHome extends EJBHome {
public Movie create(Integer id, String title)
throws RemoteException, CreateException;
public Movie findByPrimaryKey(Integer id)
throws RemoteException, FinderException;
}
95-702 OCT
Master of Information
34
A Component Interface
import javax.ejb.*;
import java.rmi.RemoteException;
// From Eckel
/**
* A movie, with id and title.
*
* Note that there is no setId() method in the
* interface, to prevent clients from arbitrarily
* changing a movie's primary key.
*/
public interface Movie extends EJBObject {
public Integer getId() throws RemoteException;
public String getTitle() throws RemoteException;
public void setTitle(String title)
throws RemoteException;
}
95-702 OCT
Master of Information
35
95-702 OCT
Master of Information
36
import javax.ejb.CreateException;
import javax.ejb.EntityBean;
import javax.ejb.EntityContext;
// From Eckel
Master of Information
37
// From Eckel
public void ejbPostCreate(Integer id, String title) {}
public void ejbLoad() {}
public void ejbStore() {}
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setEntityContext(EntityContext ctx) {}
public void unsetEntityContext() {}
// Called by
// container
95-702 OCT
Master of Information
38
Deployment Descriptor
<ejb-jar>
// From Eckel
<enterprise-beans>
<entity>
<ejb-name>Movie</ejb-name>
<home>javatheater.ejb.MovieHome</home>
<remote>javatheater.ejb.Movie</remote>
<ejb-class>
javatheater.ejb.implementation.MovieBean
</ejb-class>
<persistence-type>Container</persistence-type>
<prim-key-class>java.lang.Integer</prim-key-class>
<reentrant>False</reentrant>
<cmp-version>2.x</cmp-version>
<abstract-schema-name>Movie</abstract-schema-name>
<cmp-field><field-name>id</field-name>-</cmp-field>
<cmp-field><field-name>title</field-name>-</cmp-field>
<primkey-field>id</primkey-field>
</entity>
</enterprise-beans>95-702 OCT
</ejb-jar> Master of Information
39
95-702 OCT
Master of Information
40
95-702 OCT
Master of Information
41
95-702 OCT
Master of Information
42
Master of Information
43
ejbRemove
setSessionContext
ejbCreate
Ready
95-702 OCT
Master of Information
44
95-702 OCT
Master of Information
45
Master of Information
46
java.io.Serializable;
java.rmi.RemoteException;
javax.ejb.CreateException;
javax.ejb.EJBHome;
Master of Information
47
Master of Information
48
95-702 OCT
Master of Information
49
Master of Information
50
95-702 OCT
Master of Information
51
95-702 OCT
Master of Information
52
From Sun
95-702 OCT
Master of Information
53
On The Client
// locate the connection factory and queue
connectionFactory =
(ConnectionFactory) jndiContext.lookup
("java:comp/env/jms/MyConnectionFactory");
destination =
(Queue)
jndiContext.lookup("java:comp/env/jms/QueueName");
95-702 OCT
Master of Information
54
// Next, the client creates the queue connection, session, and sender:
connection = connectionFactory.createConnection();
session = connection.createSession(false,
Session.AUTO_ACKNOWLEDGE);
messageProducer = session.createProducer(destination);
// Finally, the client sends several messages to the queue:
message = session.createTextMessage();
for (int i = 0; i < NUM_MSGS; i++) {
message.setText("This is message " + (i + 1));
System.out.println("Sending message: " +
message.getText());
messageProducer.send(message);
}
95-702 OCT
Master of Information
55
Master of Information
56
95-702 OCT
Master of Information
57
Master of Information
58
95-702 OCT
Master of Information
59
Master of Information
60
95-702 OCT
Master of Information
61
95-702 OCT
Master of Information
62
package helloservice;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
95-702 OCT
Master of Information
63