Download Chapter Twelve - Distributed Computing

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
George Blank
University Lecturer
CS 602
Java and the Web
Object Oriented Software Development Using Java
Chapter 12, Distributed Computing
Ajay Bajaj, Kunal Mehta, Arun Marioli, Dhairya Shah
J2EE
• Sun offers Java in several different editions. The two
most common versions are the standard edition (J2SE)
and the enterprise edition (J2EE). There is also a micro
edition. In general, the standard edition is for stand-alone
applications while the enterprise edition is for computing
over a network. Actually, as we have seen, most of what
you need for web applets is in J2SE. J2EE adds web
servers, database connectivity, remote method invocation
and enterprise java beans.
The J2EE SDK
• There is a lot of software included in the J2EE SDK,
including NetBeans, the Sun Application Server, and
Apache Tomcat Web Server. All of this is currently
provided without charge, in a package that compares
favorably with development environments that cost
thousands of dollars. Around 1990, I paid $20,000 for
a single user copy of the Bachman Analyst, a front
end case tool that did not do much more than a few
diagrams.
Distributed Multi-tiered Applications
• The J2EE platform uses a multi-tiered distributed
application model for both enterprise applications
• Application logic is divided into “components”
according to function, and the various application
components that make up a J2EE application are
installed on different machines depending on the tier
in the multi-tiered J2EE environment to which the
application component belongs
Distributed Java Technologies
• There are many different ways to create a
distributed application with Java. We have
already looked at Applets, Servlets, JDBC,
and JNLP.
• In this presentation, we will briefly look at
Java Sockets, Remote Method Invocation,
and Enterprise Java Beans.
Socket Example
• Sockets simply open a connection between two
network hosts to pass information between them. We
will look at a simple Java program to implement a
socket connection.
• The example program implements a client,
EchoClient, that connects to the Echo server. The
Echo server simply receives data from its client and
echoes it back. The Echo server is a service built into
most operating systems.
Socket Basics
• Open a socket.
• Open an input stream and output stream to the
socket.
• Read from and write to the stream according to
the server's protocol.
• Close the streams.
• Close the socket.
• Only step 3 differs from client to client, depending
on the server.
Echo Client description
• EchoClient creates a socket and gets a
connection to the Echo server. It reads input
from the user on the standard input stream,
and then forwards that text to the Echo server
by writing the text to the socket. The server
echoes the input back through the socket to
the client. The client program reads and
displays the data passed back to it from the
server.
Echo Client code
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws IOException {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("taranis", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for "
+ "the connection to: taranis.");
System.exit(1);
}
Echo Client code, cont’d
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
Socket Summary
• This client program is straightforward and simple
because the Echo server implements a simple
protocol. The client sends text to the server, and
the server echoes it back. When your client
programs are talking to a more complicated
server such as an HTTP server, your client
program will also be more complicated.
Java Remote Method Invocation
• Java RMI is a distributed object model for the
Java platform.
• RMI extends the Java object model beyond a
single virtual machine address space.
• RMI uses object serialization to convert object
graphs to byte streams for transport.
Remote Method Invocation
• RMI provides the means to invoke methods
remotely.
• RMI allows for applications to communicate and
execute across multiple systems on a network.
• RMI is supported by the java.rmi, java.rmi.server,
and java.rmi.registry
• Enhanced security of Java 2 requires a security
policy implementation.
Parts in a RMI System
• Interface definitions for remote services
•
•
•
•
Implementations of the remote services
Stub and Skeleton files
A server to host the remote services
An RMI Naming service that allows clients to find
the remote services
• A class file provider (an HTTP or FTP server)
RMI process
Java Client
Java Server
Client Method
Called Method
arguments
results
Client Stub
Server Skeleton
Not needed
In Java 2
Network transport
Network Transport
Network
RMI Server, Client, and Registry
• The server process registers the remote
object X with the registry using the
Naming.bind() method.
• The client calls Naming.lookup(), which
contacts the registry and obtains a stub object
for X.
• The client then uses the stub as if it is a local
object.
RMI Registry
• The Registry tracks the addresses of the
remote objects exported by applications
• It is the central management point for RMI
• Does not actually invoke remote methods
• Bind() links the object in the registry
• Rebind() replaces object with a new one
Parameter Passing
• When a remote procedure is executed, the
java.rmi runtime encodes the arguments and
sends them over the network to a server that
decodes them.
• The server then invokes the method, encodes
the results, and sends it back.
• Finally, the client-side java.rmi runtime
decodes the result.
Building RMI Applications
• Define remote interfaces
• Create classes that implement the interfaces
• Create stub and skeleton classes for the
implementation classes.
• Create Security Policy
Steps Involved
• Write The HTML and Java Source Files.
• Compile and Deploy Class Files and HTML
Files.
• Start the Remote Object Registry, Server, and
Applet
Summary
• Java RMI is a distributed object model for the
Java platform.
• RMI extends the Java object model beyond a
single virtual machine address space.
• RMI uses object serialization to convert object
graphs to byte streams for transport.
J2EE Architecture
• J2EE multi-tiered
applications are generally
considered to be three-tiered
applications because they
are distributed over three
different locations
– client machines
– the J2EE server machine
– the database or legacy
machines at the back end
J2EE Architecture
• Three-tiered applications
that run in this way extend
the standard two-tiered client
and server model by placing
a multithreaded application
server between the client
application and back-end
storage
J2EE Containers
• The application server maintains control and
provides services through an interface or
framework known as a container
• There are five defined container types in the
J2EE specification
Server J2EE Containers
• Three of these are server-side containers:
– The server itself, which provides the J2EE runtime
environment and the other two containers
– An EJB container to manage EJB components
– A Web container to manage servlets and JSP pages
Client J2EE Containers
• The other two container types are client-side:
– An application container for stand-alone GUIs, console
– An applet container, meaning a browser, usually with
the Java Plug-in
J2EE Components
• As said earlier, J2EE applications are made
up of components
• A J2EE component is a self-contained
functional software unit that is assembled into
a J2EE application with its related classes
and files and that communicates with other
components
Components
• Client components run on the client machine,
which correlate to the client containers
• Web components -servlets and JSP pages
• EJB Components
Packaging Applications and
Components
• Under J2EE, applications and components
reside in Java Archive (JAR) files
• These JARs are named with different
extensions to denote their purpose, and the
terminology is important
Various File types
• Enterprise Archive (EAR) files represent the
application, and contain all other server-side
component archives that comprise the
application
• Client interface files and EJB components
reside in JAR files
• Web components reside in Web Archive
(WAR) files
Deployment Descriptors
• Deployment descriptors are included in the JARs, along
with component-related resources
• Deployment descriptors are XML documents that describe
configuration and other deployment settings (remember
that the J2EE application server controls many functional
aspects of the services it provides)
• The statements in the deployment descriptor are
declarative instructions to the J2EE container; for example,
transactional settings are defined in the deployment
descriptor and implemented by the J2EE container
EJB Components
• EJB components are server-side, modular, and
reusable, comprising specific units of functionality
• They are similar to the Java classes we create every
day, but are subject to special restrictions and must
provide specific interfaces for container and client
use and access
• We should consider using EJB components for
applications that require scalability, transactional
processing, or availability to multiple client types
EJB Components- Major Types
• Session beans
– These may be either stateful or stateless and are primarily
used to encapsulate business logic, carry out tasks on behalf
of a client, and act as controllers or managers for other beans
• Entity beans
– Entity beans represent persistent objects or business
concepts that exist beyond a specific application's lifetime;
they are typically stored in a relational database
Overview
• Enterprise JavaBeans is a specification for creating
server-side secure, scalable, transactional, multi-user
secure enterprise-level applications.
• These server-side components, called enterprise
beans, are distributed objects that are hosted in
Enterprise Java Bean containers and provide remote
services for clients distributed throughout the
network.
Java Beans vs. EJB
• Can be either visible nonvisible.
• Local Invocation
• Synchronous Invocation
• Decidedly non-visible remote
objects
• Remote and Local Invocation
• Synchronous and
Asynchronous Invocation
• Object Pooling
• Transparent Persistence
• Supports Transactions
• Support Relationships
between entity EJBs
• J2EE Security Features
Advantages of EJB
• Simplifies the development of middleware
components that are secure, transactional,
scalable & portable.
• Simplifies the process to focus mainly on
business logic rather than application
development.
• Overall increase in developer productivity
• Reduces the time to market for mission critical
applications
Purpose of EJBs
• SESSION Beans (verbs of the system):
– Model task or workflow
– Façade for Entity beans
– Maintain conversational state with clients
• ENTITY Beans (nouns of the system):
– Object/Relational (O/R) mapping
– Transparent and implicit persistence with transaction support
• Message Driven Beans:
– Asynchronous communication with MOM
– Conduit for non-J2EE resources to access Session and Entity
Beans via JCA Resource adapters.
Three Tier Architecture Using
EJBs
Presentation
Business
WEB Container
Data
EJB Container
EIS RDBMS
JDBC
EntityEJBwCMP
Servlet
Remote
Interface
SessionEJB
Local
Interface
EIS other
EntityEJBwBMP
MsgDrvEJB
EIS other
JMS
MessageBroker
Queue/Topic
EntityEJBwCMP = Entity Bean with Container Managed Persistence
EntityEJBwBMP = Entity Bean with Bean Managed Persistence
MsgDrvEJB = Message Driven EJB
JMS
JCAResouceAdapter
EJB Container
EJB Client
• Finds EJB container via JNDI.
• Invokes methods on EJB beans.
EJB components
Entity EJB
• CMP (Container Managed Persistence)
– Container maintains persistence transparently using
JDBC calls
• BMP (Bean Managed Persistence)
– Programmer provides persistence logic
– Used to connect to non-JDBC data sources like LDAP,
mainframe etc.
– Useful for executing stored procedures that return
result sets
Message Driven EJB
• Invoked by asynchronously by messages
• Cannot be invoked with local or remote
interfaces
• @MessageDriven annotation with in class
marks the Bean message driven
• Stateless
• Transaction aware
EJB Security Architecture
• Client Security:
– The Enterprise JavaBean (EJB) server automatically
performs the steps necessary to ensure that deployed
enterprise bean applications are only available to
authorized users.
– One of these steps is authenticating clients that
request access to EJB homes, beans, and individual
methods on the beans.
Understanding EJB Security
• Two security measures that client must pass when you add
security to EJB system – Authentication and Authorization.
• Authentication must be performed before any EJB method is
called.
• Authorization occurs at the beginning of each EJB method
call.
Some EJB Servers
Company
•
•
•
•
•
IBM
BEA Systems
Sun Microsystems
Oracle
JBoss
Product
WebSphere
BEA WebLogic
Sun Application Server
Oracle Application Server
JBoss
References (1)
• SUN EJB Specifications
http://java.sun.com/products/ejb/docs.html
• IBM RedBooks
http://www.redbooks.ibm.com/redbooks.nsf/redbooks/
• IBM WebSphere Developer Technical Journal
http://www.ibm.com/developerworks/websphere/
• Oracle Technology Network
http://www.oracle.com/technology/tech/java/index.html
References (2)
• Java.net http://www.java.net/
• JavaWorld
www.javaworld.com/channel_content/jw-ejbsindex.shtml
• TheServerSide
http://www.theserverside.com/
• Richard Monson-Haefel, Enterprise JAVABEANS
• Tate, Clark, Lee, Lisnkey, BITTER EJB
• Feghhi, Jalal, Web developer's guide to JavaBeans
Reading from and Writing to a
Socket in Java
• Let's look at a simple example that illustrates
how a program can establish a connection to
a server program using the Socket class and
then, how the client can send data to and
receive data from the server through the
socket.