Download An Introduction to Enterprise Java Beans

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
95-702 Distributed Systems
An Introduction to Enterprise
Java Beans
95-702 Distributed Systems
Master of Information System
Management
1
Separation of Concerns
-  In 1974 Dijkstra coined the term
“Separation of Concerns” to describe
scientific thinking.
-  This has been a very important concept
in building software.
-  In JEE, there is an attempt to separate
business logic from the many nonfunctional, but important,
characteristics.
95-702 Distributed Systems
Master of Information System
Management
2
Some Useful Resources
•  The Java EE 6 Tutorial
http://download.oracle.com/javaee/6/tutorial/doc/
•  Enterprise JavaBeans 3.1
Rubinger and Burke, Published by O’Reilly
•  EJB 3.0 API Documentation
http://java.sun.com/products/ejb/docs.html
•  Java Enterprise In A Nutshell
Farley, Crawford & Flanagan
95-702 Distributed Systems
Master of Information System
Management
3
From The Java EE Tutorial
95-702 Distributed Systems
Master of Information System
Management
4
From The Java EE Tutorial
95-702 Distributed Systems
Master of Information System
Management
5
SOAP and Java EE
Slides from JMS tutorial at Sun Microsystems
95-702 Distributed Systems
Master of Information System
Management
6
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,
authentication, authorization, etc..
•  Portable – may be moved to other
Java EE containers.
95-702 Distributed Systems
Master of Information System
Management
7
Benefits of Enterprise
Beans(2)
•  Scalable – the components may be
distributed across many machines.
•  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).
•  Queuing and persistence.
95-702 Distributed Systems
Master of Information System
Management
8
Main Concept: Interposition
EJB Container
Application
stub
skeleton
95-702 Distributed Systems
Master of Information System
Management
•  examine security credentials
•  start or join transaction
•  call any necessary persistence
functions
•  trigger various callbacks
•  call business logic
•  more work with transactions,
persistence and callbacks
•  send back result or an
exception
9
Major EJB Container Services
•  Security Control
•  Transaction Control
•  Concurrency Management
•  Scalability Management
•  Timer services
•  Interceptors
•  Web service support
•  These services are enlisted through
annotations or configuration files.
95-702 Distributed Systems
Master of Information System
Management
10
Two Types of Enterprise
Java Beans
•  Session Beans
With three subtypes
•  Message-Driven Beans
Used with Message Oriented
Middleware
95-702 Distributed Systems
Master of Information System
Management
11
How does a client get access?
Dependency injection is the simplest way of obtaining an enterprise
bean reference. Clients that run within a Java EE server-managed
environment, JavaServer Faces web applications, JAX-RS web
services, other enterprise beans, or Java EE application clients,
support dependency injection using the javax.ejb.EJB annotation.
Applications that run outside a Java EE server-managed environment,
such as Java SE applications, must perform an explicit lookup.
JNDI supports a global syntax for identifying Java EE components to
simplify this explicit lookup.
These notes are from the Java EE Tutorial.
EJB vendors may use any naming service but are required to support
the CORBA naming service. JNDI supports many naming services.
95-702 Distributed Systems
Master of Information System
Management
12
Session Beans(1)
• 
• 
• 
• 
• 
• 
Are an extension of the client application.
Manage processes or tasks.
Are not persistent.
Implement business logic.
Clients of the bean see an interface.
May be used as a web service.
95-702 Distributed Systems
Master of Information System
Management
13
Session Beans(2)
•  Come in three flavors:
–  Stateless session beans (no memory between calls)
purchase(severalIems,creditCardNo);
In MS .NET, these are called SingleCall objects.
–  Stateful session beans (remember earlier calls)
addToCart(item);
purchase(creditCardNo);
In MS .NET, these are called ClientActivated
objects
–  Singleton session beans (only one exists)
In MS .NET, these are called Singleton objects.
95-702 Distributed Systems
Master of Information System
Management
14
Session Bean(3) Quiz
•  Which session bean promotes loose coupling?
–  Stateless session beans (no memory between calls)
purchase(severalIems,creditCardNo);
–  Stateful session beans (remember earlier calls)
addToCart(item);
purchase(creditCardNo);
Answer: Stateless session beans.
95-702 Distributed Systems
Master of Information System
Management
15
Session Bean(4) Quiz
•  Which session bean causes us to worry most
about concurrent access to shared resources?
•  Stateless beans
•  Stateful beans
•  Singleton beans
Answer: Singletons.
95-702 Distributed Systems
Master of Information System
Management
16
Message-Driven Beans (1)
•  Work in cooperation with Java Messaging System
(JMS).
•  JMS is an abstraction API on top of MessageOriented Middleware (MOM) – like JDBC is an
abstraction API on top of SQL databases or like
JAXR is an abstraction API on different types of
XML registries or like JNDI is an abstraction API on
directories.
•  Each MOM vendor implements things differently.
•  MDB’s 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 (IBM’s MQSeries or Progress’
95-702 Distributed Systems
SonicMQ).
Master of Information System
Management
17
Message-Driven Beans (2)
•  Like session beans, are meant for
business logic.
•  Have no persistent state.
•  Coordinate tasks involving other session
beans.
•  Listen for asynchronous messages.
•  Unlike Session beans, provide no
remote interface describing the
methods that can be called.
95-702 Distributed Systems
Master of Information System
Management
18
Message-Driven Beans (3)
•  Are receivers of MOM messages
coming through the JMS API.
•  Usually take action when a
message is received.
•  Unlike session beans, MessageDriven Beans are not called
directly by a client.
95-702 Distributed Systems
Master of Information System
Management
19
Message-Driven Bean (4)
•  The container will call the onMessage()
method when an asynchronous message
arrives.
•  The bean should be short lived.
•  The bean is stateless.
95-702 Distributed Systems
Master of Information System
Management
20
Message-Driven Bean (5)
•  Two basic messaging-system models
(1) point-to-point model allows messages to
be sent to a message queue to be read by
exactly one message consumer.
(2) publish/subscribe model allows
components to publish messages on a topic to
a server to be read by zero or more
subscribers. Subscribers register for a topic.
95-702 Distributed Systems
Master of Information System
Management
21
In Both Messaging Models
•  The messages hold:
-- a header containing the destination and the
sending time.
-- message properties to allow the receiver to
select which messages they would like to
receive. These may be set by the sender.
-- the message body itself.
95-702 Distributed Systems
Master of Information System
Management
22
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 Distributed Systems
Master of Information System
Management
23
Point-To-Point on the Server
Side
import javax.jms.*;
QueueConnection qCon;
QueueSession qSes;
QueueReceiver qRec;
Through JNDI get access to a QueueReceiver.
Build a MessageListener with an onMessage
method.
95-702 Distributed Systems
Master of Information System
Management
24
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 Distributed Systems
Master of Information System
Management
25
// 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 Distributed Systems
Master of Information System
Management
26
JMS Message Types(1)
The Message Body Contains:
TextMessage
A java.lang.String object (for example,
an XML document).
MapMessage
A set of name/value pairs, with names as
String objects and values as primitive types
in the Java programming language. The
entries can be accessed sequentially by enumerator
or randomly by name.
The order of the entries is undefined.
95-702 Distributed Systems
Master of Information System
Management
27
JMS Message Types(2)
BytesMessage
A stream of uninterpreted bytes. This message type is
for literally encoding a body to match an existing message
format.
StreamMessage
A stream of primitive values in the Java programming
language, filled and read sequentially.
ObjectMessage A Serializable object in the Java
programming language.
95-702 Distributed Systems
Master of Information System
Management
28
Listening to the Queue
public void onMessage(Message inMessage) {
TextMessage msg = null;
try {
if (inMessage instanceof TextMessage) {
msg = (TextMessage) inMessage;
System.out.println("MESSAGE BEAN: Message received: "
+msg.getText());
} else {
System.out.println("Message of wrong type: " +
inMessage.getClass().getName());
}
} catch (JMSException e) {
e.printStackTrace();
mdc.setRollbackOnly();
} catch (Throwable te) {
te.printStackTrace();
}}
95-702 Distributed Systems
Master of Information System
Management
29
Web Services
•  A Web service client can access Java EE
applications in two ways.
•  First, the client can access a Web
service created with JAX-RPC. Behind
the scenes, JAX-RPC uses a servlet to
implement the SOAP Web Service.
•  Second, a Web service client can access
a stateless session bean through the
service endpoint interface of the bean.
Other types of enterprise beans cannot
be accessed by Web service clients.
95-702 Distributed Systems
Master of Information System
Management
30
A Stateless Session Bean as
a Web Service
•  The client need not know that its
interacting with a Java EJB.
•  It calls the bean like it calls any
other web service.
•  Thus, .NET interoperates with Java
EE using XML on the wire.
95-702 Distributed Systems
Master of Information System
Management
31
The Web Service Endpoint
Interface
package helloservice;
import java.rmi.RemoteException;
import java.rmi.Remote;
The client cannot
see that it’s interacting
with an EJB
public interface HelloIF extends Remote {
public String sayHello(String name)
throws RemoteException;
}
95-702 Distributed Systems
Master of Information System
Management
32
The Web Service Session
we added remote and home
BeanIfInterfaces
then this bean could
package helloservice;
import java.rmi.RemoteException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
also be called using in the traditional
manner – with remote references.
No change to the bean would be
necessary.
public class HelloServiceBean implements SessionBean {
public String sayHello(String name) {
This is an old style bean. Newer
beans are even simpler.
return "Hello " + name + "from HelloServiceEJB";
}
public HelloServiceBean() {}
WSDL can be generated and all
public void ejbCreate() {}
of the previous clients will work.
public void ejbRemove() {}
public void ejbActivate() {}
public void ejbPassivate() {}
public void setSessionContext(SessionContext sc) {}
95-702 Distributed Systems
}
33
Master of Information System
Management