Lecture14

advertisement
CS441
CURRENT TOPICS IN
PROGRAMMING LANGUAGES
Lecture 14
George Koutsogiannakis/ Summer 2011
1
Topics
• Enterprise Java Beans (EJB)
– Definitions.
– Types of EJBs.
– Application Servers/ EJB Servers.
– EJB Containers.
– EJB Application Example.
– Session Beans.
– Packaging Web Modules.
– Deploying Web Modules.
2
Enterprise Java Beans
• Creating a complete enterprise web application using the approach using
discreet APIs is difficult (i.e using the servlets API, the RMI API, the JDBC
API etc).
– It requires a lot of work using the fragmented technologies (servlets,
RMI etc.) to implement:
• Session.
• Security.
– Administrative versus manager versus user
• Transaction management (database data).
• Dynamic content geared to various users.
3
Enterprise Java Beans
• Presentation layer components that are different for
each user (dynamic presentation layer).
• Issues with deployment.
• Issues with distribution.
• All of the above issues can be:
– unified under one roof with Enterprise Java Beans bringing all
technologies together.
– Easy implementation of various functionalities using one development
environment (such as NetBeans).
– Easy deployment and distribution.
– Free developer from creating deployment descriptor file (i.e.web.xml
and others needed).
4
Enterprise Java Beans
• An Enterprise Java Bean (EJB) is a server side component that
encapsulates the business logic of an application.
• Or another definition is:
An architecture for the development and deployment of
transactional, distributed object applications based on server side
software components
– EJB s run in the EJB container of an application server (such as the Sun
Java System Application Server or GlassFish server of NetBeans).
Therefore, in that respect, they are different than plain Java Beans.
• Java Beans can be self sustained components that can be run outside a container.
– The EJB container provides certain services such as security, transactions
and other services.
– The container therefore is similar to the concept of a container for servlets in a web
server.
– A container is housed inside a Component Transaction Monitor (CMT).
– CMT is part of the Application Server (i.e. GlassFish).
5
Enterprise Java Beans
• EJB s are used to create distributed business objects. As such
they handle:
–
–
–
–
Persistence (saving of the state of the business object)
Security.
Concurrency.
Transaction Management.
• EJB s should run on any Operating System (at least
theoretically although in practice it may not be 100% correct).
• EJBs are reusable , scalable, can be deployed on any
Application Server.
• EJBs are part of J2EE and supported by package:
– javax.ejb
6
Enterprise Java Beans
• Advantages of EJB s:
– Developer can concentrate on solving business problems since a
number of the services are offered by the container.
– The client is independent of the business logic and the developer for
the client can concentrate on presentation only.
– Thinner clients can be developed.
– A new EJB can be constructed from existing EJBs .
– They can be distributed amongst a number of machines (locality
independent). Thus, scalability can be achieved.
– Remote clients can locate EJBs ( a similar task that an RMI client has
when it does a look up on the Naming Service to get information
about a remote object)
7
Enterprise Java Beans
– Organizations can build their own EJB components or purchase
components from a third party.
– Development of an EJB component is based on a specification.
Vendors of EJB servers use a common platform in designing the
servers thus ensuring portability amongst different servers.
8
Enterprise Java Beans
• Types of EJB s are:
– Entity Beans
• They model: products, customers, data.
• They have a persistent state.
• They have been replaced with the Java Persistence API
entities in EE 6 (they exist in previous versions, prior to EE5).
– Session Beans
• They model: processes, coordinate the activities of EJBs
• Do not have a persistent state.
– Message Driven Beans ( these beans require usage of
Java Message Service API – JMS).
9
Enterprise Java Beans
• Enterprise JavaBeans technology supports
both transient and persistent objects.
• A transient object is called a session bean,
• and a persistent object is called an entity
bean.
10
Enterprise Java Beans
• A session bean exists NORMALLY only for the duration of a
single client/server session.
• A session bean CAN perform operations such as accessing a
database or CAN perform calculations.
• Session beans can be transactional, but normally are not
recoverable following a system crash.
• Session beans can be stateless,
• or they can maintain conversational state across methods and
transactions.
• A session bean must manage its own persistent data.
11
Enterprise Java Beans
• An entity bean is an object representation of
persistent data maintained in a permanent
data store, such as a database.
• An entity object can manage its own
persistence (called Bean Managed Persistence
-BMP)
• or it can delegate its persistence to its
container (called Container Managed
Persistence- CMP).
12
Enterprise Java Beans
• Types of Entity Beans:
Entity Beans
Entity Beans
that implement
container
managed
persistence
These beans are defined
Independent of the database
that they use
Entity Beans
that implement
bean managed
persistence
These beans use explicit SQL
statements relating to the
database they use
13
Enterprise Java Beans
• Types of session beans:
Session Beans
Stateless Session
Beans:
Any client can use
them
Stateful Session
Beans:
Specific to a
client. Maintain
information about
the client.
14
Application Server Services
• The Application Server provides (such as GlassFish in NetBeans):
– Persistence service.
• Ability to save the data (state) after execution.
– Transaction processing service.
• Ability to execute a group of operations atomically.
– Concurrency Control.
• Simultaneous execution of a number of requests.
– Events using Java Message Service API (JMS).
• Ability to send messages to other components.
–
–
–
–
–
Naming and Directory service.
Security via JCE and JAAS APIs.
Deployment of components.
Remote Procedure calls using RMI or RMI over IIOP
Exposure of business logic components to web services.
• XML messages over SOAP protocol.
• A Deployment Descriptor in XML describes the EJBs residing in the server
15
EJB SERVERS
• AN EJB SERVER (combination of web server and Application Server)
USUALLY CONSISTS OF:
–
A WEB CONTAINER: IT HANDLES THE EXECUTION OF SERVLETS AND JSP COMPONENTS (LIKE
TOMCAT).
– EJB CONTAINER (APPLICATION SERVER PART): IT HANDLES THE EXECUTION ENVIRONMENT OF
THE EJBS
– APPLICATION CLIENT CONTAINER: HANDLES THE EXECUTION OF CLIENT COMPONENTS.
• PROVIDES SERVICES TO A CLIENT APPLICATION:
– PERSISTENCE
– SECURITY
– WEB SERVICES CLIENT SIDE SERVICES
– APPLET CONTAINER: PROVIDES RUN TIME ENIVIRONMENT FOR APPLETS
(BROWSER, JAVA PLUG-IN).
– DATABASE SERVER: PROVIDES A DATABASE.
• NOTE THAT THE ABOVE CAN VARY FROM VENDOR TO VENDOR (SOME ARE
ALL INCLUSIVE OR PARTS OF THE ABOVE)
16
EJB SERVERS
• SOME AVAILABLE EJB SERVERS ARE (PURE JAVA SERVERS):
– SUN JAVA SYSTEM APPLICATION SERVER PLATFORM EDITION 9 –OPEN
SOURCE (SJSAS-9) –NOW CALLED: Sun GlassFish Enterprise Server
– APATCHE GERONIMO- AN EXTENSION OF APATCHE TOMCAT WEB
SERVER TO PROVIDE EJB EXECUTION ENVIRONMENT- OPEN SOURCE
– JBoss – OPEN SOURCE
– JFox- OPEN SOURCE
– EASY BEANS- OPEN SOURCE
– OpenEJB – OPEN SOURCE
– WebLogic EJB Server- NOT FREE (VENDOR: ORACLE/BEA)
– IBM WEBSPHERE – NOT FREE (VENDOR:IBM)
17
EJB SERVERS
• To choose the EJB application server that is right for your project, you'll
need to take a variety of issues into account, such as:
– Level of support for J2EE technologies (Java JDK, Servlets, JSP, JNDI, and EJB)
– Support for all (critical) platforms used within your organization
– Ability to deploy and integrate into your existing technical environment, particularly support
for JMX
– Development tool support
– Cost, including both initial and ongoing operational and support costs
– The market share of the vendor
– The long-term viability of the vendor
– The long-term viability of the product itself
– Availability of technical support from the vendor
– Availability of server source code (open source vs. closed source)
– Availability of developers (to your organization) experienced with the product
– Availability of training and mentoring services
– Availability of books, Internet newsgroups, and so on
• Consider starting with a free EJB server such as GlassFish.
18
EJB Containers
• Containers provide the run time environment for EJBs within the
Application Server. They act as an intermediary between the EJB and the
Application Server
Application
Server
Instance
of
container
EJB
#1
EJB
#2
Instance
of
container
EJB
#3
19
EJB Containers
• Services provided by the container are:
–
–
–
–
–
–
Activation and deactivation of EJB.
Caching the state of an Entity Bean.
Committing and rolling back transactions ( a group of SQL statements).
Maintains a pool of EJB instances ready for incoming requests.
Ensures that threading conditions within the Bean are satisfied.
Synchronizes data between an Entity Bean and persistent storage (the
database).
20
EJB Containers
CONTAINER
TRANSACTION MANAGEMENT
PERSISTENCE
SECURITY
CLIENT
REQUEST
EJB CONTEXT
EJB
JNDI
SERVICES
PROVIDED
DURING
RUNTIME
BEAN INTERACTIONS
WITH
THE CONTAINER
CALLBACKS
EJB CONTEXT: CAN GET A REFERENCE TO THE CONTAINER METHODS SO THAT THE BEAN CAN INTERACT WITH THE CONTAINER
JNDI: ALLOWS ACCESS BY THE BEAN TO OTHER RESOURCES LIKE JDBC CONNECTIONS, OTHER BEANS, PROPERTIES SPECIFIC TO THE BEAN
CALLBACKS: THE CONTAINER USES CALLBACKS TO INFORM THE BEAN OF VARIOUS EVENTS.
21
EJB Containers
– MOST CONTAINERS USE THE RMI OR COBRA OR RMI OVER IIOP
PROTOCOLS.
• THE PREVIOUS SLIDE THEN SHOWS THAT THE EJB CONTAINER
PROVIDES:
– Beginning, committing, and rolling back transactions as necessary.
– Maintaining pools of enterprise bean instances ready for incoming
requests and moving these instances between the inactive pools and
an active state, ensuring that threading conditions within the bean are
satisfied.
– Most importantly, automatically synchronizing data in an entity bean's
instance variables with corresponding data items stored in persistent
storage.
– Note: Persistent Storage refers to the saving of data in a database
22
EJB Containers
• For each enterprise bean, the container is responsible for :
– registering the object,
– providing a remote interface for the object,
– creating and destroying object instances,
– checking security for the object,
– managing the active state for the object,
– and coordinating distributed transactions.
– Optionally, the container can also manage all persistent data within
the object.
23
Example EJB Architecture
------Middle Tier-----
Client
HTML
or
Applet
Client Tier
(Presentation
Layer)
-----------EIS Tier---------------------DB1
Servlet
Web
Server
JSP
Session
Bean
Entity
Bean
DB2
Entity
Bean
Beans are located in
Distributed
Application Servers
Internet
Web Tier
WHERE WEB
MODULES ARE DEPLOYED
Business Logic Tier
Data Tier
24
Deploying The Enterprise
Application
• The next slide shows how the previous architecture
can be deployed.
• Notice that we have combined and renamed the
tiers as:
– Client tier
– Middle Tier
– Enterprise Information System (EIS) tier (it has the existing
applications, files, and databases).
• Notice that the EIS tier can be accessed by a client in a
number of different ways!!
25
Deploying The Enterprise Application
26
APIs Supported by the Servers
• The following application program interfaces (APIs) are available for J2EE
client applications, web servers, and EJB servers:
– JMS: The Java Message Service is the API for sending and receiving messages via
enterprise messaging systems like IBM MQ Series and TIBCO Rendezvous.
– JAAS: The Java Authentication and Authorization Service is used to authenticate and
authorize access controls on users.
– JAXP: The Java API for XML Parsing provides support for the industry standard SAX and
DOM APIs for parsing XML documents, as well as support for XSLT transform engines.
– JDBC: The JDBC API is for accessing databases from Java using a call-level interface.
– SQLJ: The SQLJ API is for accessing databases from Java using embedded SQL.
– JDO: The JDO API is for accessing databases from Java using a transparent persistence
interface.
27
APIs Supported by the Servers
• The following APIs are not available for J2EE client
applications, but are available for J2EE web servers and EJB
servers:
• JTA: The Java Transactions API is the API for managing and
coordinating transactions across heterogeneous systems.
• JavaMail: This is the API for sending and receiving email.
• JAF: JavaMail uses the JavaBeans Activation Framework.
• Connectors: These connect to non-J2EE system
28
More on Session Beans
• In a stateful session bean the instance variables values of the
object that represents the bean are saved for the duration
that the bean is alive (the client’ s session). When client
removes the session the state is lost.
– The client makes invocations on the methods of the bean which can
change the state. The new state is saved during the time that the bean
is waiting for a new invocation.
– Another name for the state of a stateful bean is “conversation state”.
– Dedicated to a specific client
• A stateless bean does not retain the state. It gives up the
state as soon as the method (of the bean) that has been
invoked terminates.
– Can support many clients
29
More on Session Beans
• Three types of a client access to a session bean:
• Remote access
– The session bean offers location transparency to the client. It can be run
by the same JVM as the client or by a different JVM.
– The client can be a servlet ( a web component) or a java application or
another EJB.
– The client accesses the bean via interfaces that define methods that
define the client’ s view of the bean.
@Remote
public interface MyAccount{
public void deposit(double d);
public double withdraw(double w);
}
@Remote
public class BankAccount implements MyAccount{ ……….. }
30
More on Session Beans
• Local access:
–
–
–
–
Client must run in same JVM as the bean.
Client can be a web component (i.e. servlet ) or another bean.
Location of the bean is not transparent to the client.
It is the default access type if the access type is not specified in the
interface.
@Local
public interface MyInterface {…….. }
@Local
public class MyBean implements MyInterface {…………. }
31
More on Session Beans
• Web Service access
– This kind of access is devoted to clients which are web services.
– The web services clients do not have to be written in Java.
– It is beyond the scope of this course!
32
More on Session Beans
• To Develop a Session Bean the following files are needed:
Enterprise Bean class: This is the class that implements the
methods of the interface.
Business Interface: Defines the business methods that the
client can invoke.
Helper classes: Any other classes needed by the bean.
• The files must be packaged in a EJB jar file.
Note: An EJB application consists of a number of EJB jar files that
are assembled into an EAR (Enterprise Archive) file.
THE EAR COMBINES THE EJB JAR FILES AND THE WEB MODULE JAR FILES
(WEB TIER FILES LIKE SERVLETS/JSP)
33
Life Cycle of a Session Bean
• Stateful Session Bean:
– Does Not Exist state – Client brings it to Ready state by getting a
reference of the bean.
– Ready State--- Client can invoke its business methods.
– Passive State---- Container can place the bean in the passive state
which means that it transfers the bean to secondary storage.
Container can bring the bean back to Ready state if the client invokes
one of its methods.
• Stateless Session Bean:
– Does Not Exist
– Ready
• Message Driven Bean: Similar to Stateless Session Bean states
34
Packaging WEB MODULES
• A WEB APPLICATION CAN BE BUILT USING AN ENVIRONMENT LIKE
NETBEANS OR IT CAN BE DEVELOPED MANUALLY.
– IT COULD BE DEVELOPED OUTSIDE NETBEANS AND IMPORTED INTO
NETBEANS OR
– IT CAN BE TREATED INDEPENDENT OF NETBEANS
• A WAR FILE IS NEEDED TO PACKAGE THE WEB APPLICATION (SIMILAR TO
THE WAR FILE IN TOMCAT).
– THE WAR FILE CAN BE CREATED WITH NETBEANS DURING THE BUILD PROCESS
OR
– IT CAN BE CREATED MANUALLY BY USING THE ANT TOOL (PART OF THE SUN
JAVA SYSTEM APPLICATION SERVER-9) OR
– BY MANUALLY USING THE JAR COMMANDS.
35
Deploying WEB MODULES
• YOU NEED A DEPLOYMENT DESCRIPTOR (SIMILAR TO web.xml in Tomcat)
– THE DEPLOYMENT DESCRIPTOR IN THE APPLICATION SERVER IS CALLED: sun-web.xml
– THE DEPLOYMENT DESCRIPTOR CONTAINS CONFIGURATION INFORMATION INCLUDING
THE NAME OF THE CONTEXT ROOT OF YOUR APPLICATION.
– THE FILE sun-web.xml RESIDES IN THE WEB-INF FOLDER OF YOUR ROOT CONTEXT (NOT
ANY DIFFERENT THAN TOMCAT)
– IT CAN BE CREATED AND EDITED IN NETBEANS OR MANUALLY.
• DEPLOYMENT CAN BE ACCOMPLISHED EITHER VIA NETBEANS OR
– BY USING THE ADMINISTRATIVE CONSOLE OF THE JAVA APPLICATION SERVER OR
– BY USING THE ANT TOOL OF THE JAVA APPLICATION SERVER
• ONCE THE APPLICATION IS DEPLOYED IT CAN BE VIEWED BY A BROWSER
(IF THE BEAN HAS A WEB CLIENT).
http://localhost:8080/contextrootname/
36
Glass Fish
• Glass Fish is a combination of web modules
environment and application server for EJBs
– It takes the place of the Java System Application
Server Platform 9 (it includes it) now called GlassFish
– It is part of NetBeans.
– Provides centralized monitoring and management,
Clustering for scalability and availability. It is part of
the Sun GlassFish Portfolio open Web application
platform that combines open-source software and
support in a single package.
– It includes a web server (DO NOT START TOMCAT IF
GLASSFISH IS THE SERVER USED).
37
Glass Fish
• Both Web Modules and EJB Modules can be developed
using NetBeans
• They get deployed in GlassFish (or earlier versions of it
like Java System Application Server Platform 9).
• You need to register a local instance of GlassFish / Sun
Java System Application Server with the NetBeans IDE.
The GlassFish application server is bundled with the
NetBeans IDE. If you installed the application server as
an option when you installed the NetBeans IDE, the
server should already be registered with the IDE.
38
Administration of Glass Fish
• Sun GlassFish Enterprise Server administration
includes many tasks such as
– deploying applications,
– creating and configuring domains, server instances and
resources,
– controlling (starting and stopping) domains and server
instances,
– managing profiles and clusters,
– monitoring and managing performance,
– and diagnosing and troubleshooting problems.
39
Administration of Glass Fish
• Administration of Glass Fish can be
accomplished via one of the following
methods:
– Admin Console
– Command-line Interface (asadmin Utility)
– JConsole
40
Administration of Glass Fish
• Admin Console
• The Admin Console is a browser-based tool that features an easy-tonavigate interface and online help.
• The administration server (also called the Domain Administration Server
or DAS) must be running to use the Admin Console.
• To launch the Administration Console, you must know the administration
server hostname and port number. When the Enterprise Server was
installed, you chose a port number for the server, or used the default port
of 4848. You also specified a user name and master password.
• To start the Admin Console, in a web browser type:
• http://hostname:port i.e http://localhost:4848/
41
Administration of Glass Fish
• The installation program creates the default administrative
domain (named domain1) with the default port number 4848,
as well as an instance separate from the domain
administration server (DAS).
• After installation, additional administration domains can be
created.
• Each domain has its own domain administration server, which
has a unique port number.
• When specifying the URL for the Admin Console, be sure to
use the port number for the domain to be administered.
42
Administration of Glass Fish
• Command-line Interface (asadmin Utility)- Primarily for UNIX/Linux
Systems.
• The asadmin utility is a command-line interface for the Sun GlassFish
Enterprise Server. Use the asadmin utility and the commands associated
with it to perform the same set of administrative tasks offered by the
Admin Console.
• To start the asadmin utility, go to the as-install/bin directory and enter:
• $ ./asadmin To list the commands available within asadmin:
• asadmin> help It is also possible to issue an asadmin command at the
shell’s command prompt:
• $ asadmin help To view a command’s syntax and examples, type help
followed by the command name. For example:
• asadmin> help create-jdbc-resource
43
Administration of Glass Fish
• JConsole
• JConsole is used to monitor the Sun GlassFish Enterprise
Server. You can use either the JConsole remote tab, or the
advanced tab to connect to the Enterprise Server.
• Remote Tab: identify the username, password, administration
server host, and JMS port number (8686 by default), and
select Connect.
• Advanced Tab: identify the JMXServiceURL as
service:jmx:rmi:///jndi/rmi://host:jms-port/jmxrmi and select
Connect. The JMXServerURL is printed in the server.log file as
well as output in the command window of the domain
creation command.
44
Glass Fish Domains
• The Enterprise Server consists of one or more domains.
• A domain is an administrative boundary or context. Each
domain has an administration server (also called Domain
Administration Server or DAS) associated with it and consists
of zero or more standalone instances and/or clusters.
• Each cluster has one or more homogeneous server instances.
A server instance is a single Java Virtual Machine (JVM) that
runs the Application Server on a single physical machine.
Server instances (whether standalone or clustered) in a
domain can run on different physical hosts.
45
Glass Fish Domains
• A domain is a group of instances that are administered together.
• An application server instance can belong to just one domain.
• In addition to the administration boundary, a domain provides /facilitates:
– the basic security structure whereby different administrators can
administer specific groups (domains) of application server instances.
– by grouping the server instances into separate domains, different
organizations and administrators can share a single Enterprise Server
installation.
– each domain has its own configuration, log files, and application
deployment areas that are independent of other domains. If the
configuration is changed for one domain, the configurations of other
domains are not affected.
46
Glass Fish Domains
• The Sun GlassFish Enterprise Server installer creates the
default administrative domain (named domain1).
• It also creates an associated domain administration server
(named server). You must provide the administration server
port number. The default administration server port is 4848.
The installer also queries for the administration username and
master password. After installation, additional administration
domains can be created.
• Each domain has its own Domain Administration Server (DAS)
with a unique port number. The Admin Console
communicates with a specific DAS to administer the
associated domain. Each Admin Console session allows you to
configure and manage the specific domain.
47
Glass Fish Domains
• The DAS authenticates the administrator,
• accepts requests from administration tools,
• and communicates with server instances in the domain to carry out the
requests.
• The DAS is sometimes referred to as the admin server or default server. It
is referred to as the default server because it is the only server instance
that gets created on Sun GlassFish Enterprise Server installation and can
be used for deployments. The DAS is simply a server instance with
additional administration capabilities.
• Each Admin Console session allows you to configure and manage a single
domain. If you created multiple domains, you must start an additional
Admin Console session to manage the other domains. When specifying
the URL for the Admin Console, be sure to use the port number of the DAS
associated with the domain to be administered.
48
Glass Fish Domains
• Every administrative domain is associated with a
usage profile, which identifies the capabilities of that
domain. Enterprise Server provides the following
profiles:
– Developer
– Cluster (A cluster is a named collection of server instances
sharing the same set of applications, resources, and
configuration information)
– Enterprise
Note: Use Developer or Enterprise for your work in this course.
Note: Security settings vary between profiles.
49
Glass Fish Domains
• In a development environment you can use
different application server instances to test
different Enterprise Server configurations, or
to compare and test different application
deployments. Because you can easily add or
delete an application server instance, you can
use them to create temporary sandbox area
for experimentation purposes.
50
Client’s Ports
• Remote clients of enterprise beans (EJB
components) access the beans through the
IIOP listener.
– The IIOP port for the remote clients is : 3700
(default)
– Secured IIOP post is :3820
– HTTP Port is : 8080 (unless there is a conflict)
– Secured HTTP port is:8181
– Java Message Services Port: 7676
51
STUDY GUIDE
• Review Chapters 1 and 2 of Java EE 6 Tutorial
• Java EE 6 Tutorial- chapters 14,15,16
52
Download