Download Creating Applications Using RMI

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
Creating Applications Using RMI
Pre-assessment Questions
1.
Which of the following refers to the component-based approach of creating
distributed applications?
a.
Two-tier architecture
b.
Three-tier architecture
c.
Monolithic architecture
d.
Single-tier architecture
2.
Which of the following is NOT true for RMI?
a.
RMI is a specification that enables one JVM to invoke methods of an
object located in another JVM.
b.
RMI is implemented on the middle-tier of the three-tier architecture
framework.
c.
RMI application run on cross-platform specification.
d.
RMI supports cross-language specification.
Network and Distributed Programming in Java
Lesson 1B / Slide 1 of 19
Creating Applications Using RMI
Pre-assessment Questions
3. RMI registry is a _____.
a.
Server
b.
Client
c.
Middle-tier
d.
Service
4.
Which of the following services is provided by the transport layer in the RMI
architecture?
a.
Creating the connection between the client and server
b.
Creating the server objects
c.
Registering the server object
d.
Invoking the remote method
Network and Distributed Programming in Java
Lesson 1B / Slide 2 of 19
Creating Applications Using RMI
Pre-assessment Questions
5.
Which system property controls the lease time?
a. java.rmi.dgc.lease.clean
b. javax.rmi.dgc.leaseValue
c. java.rmi.dgc.leaseValue
d. java.rmi.dgc.lease.dirty
Network and Distributed Programming in Java
Lesson 1B / Slide 3 of 19
Creating Applications Using RMI
Solutions to Pre-assessment Questions
1.
2.
3.
4.
5.
b. Three-tier Architecture
d. RMI supports cross-language specification.
d. Service
a. Creating the connection between the client and server
c.java.rmi.dgc.leaseValue
Network and Distributed Programming in Java
Lesson 1B / Slide 4 of 19
Creating Applications Using RMI
Objectives
In this lesson, you will learn about:
•
•
Transmit files using RMI
Create application using client-side callbacks
Network and Distributed Programming in Java
Lesson 1B / Slide 5 of 19
Creating Applications Using RMI
Demonstration-Transmitting Files
Using RMI
•
Problem Statement
•
Create a distributed application using RMI, where an RMI client
can download a file (plain text or binary) from the RMI server.
Network and Distributed Programming in Java
Lesson 1B / Slide 6 of 19
Creating Applications Using RMI
Demonstration-Transmitting Files
Using RMI (Contd.)
•
Solution
•
The steps to create the distributed application using RMI are:
1. Create the FileRemote interface for the RMI server.
2. Create the FileRemoteImpl class that implements the
FileRemote interface.
3. Create the RMIFileServer class.
4. Create the RMIFileClient class.
5.
Run the application.
Network and Distributed Programming in Java
Lesson 1B / Slide 7 of 19
Creating Applications Using RMI
Client-Side Callbacks
•
In client-side callbacks:
•
•
•
The RMI server invokes the method of the RMI client.
The RMI client acts as an RMI server.
The RMI client extends the UnicastRemoteObjcet class.
Network and Distributed Programming in Java
Lesson 1B / Slide 8 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
•
Creating an Application using Client-Side Callbacks
1.
2.
3.
4.
5.
6.
7.
Create a remote interface for the client
Create a remote interface for the server
Implement the server interface
Implement the client interface
Create an RMI server
Create an RMI client
Run the client and server applications
Network and Distributed Programming in Java
Lesson 1B / Slide 9 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
1.
Creating Remote Interface for the Client
•
The code to create a remote interface for the RMI client is:
public interface CRemote extends Remote
{
public void displayValue(String s) throws
RemoteException;
}
Network and Distributed Programming in Java
Lesson 1B / Slide 10 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
2.
Creating Remote Interface for the Server
•
The code to create a remote interface for the RMI server is:
public interface SRemote extends Remote
{
public void registerClient(CRemote c) throws
RemoteException;
public CRemote getCallback() throws
RemoteException;
}
Network and Distributed Programming in Java
Lesson 1B / Slide 11 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
3.
Implementing the Server Interface
•
The code snippet to implement the server interface is:
public class SRemoteImpl extends SRemote
{
public void registerClient(CRemote c) throws
RemoteException
{
.. .. ..
}
public CRemote getCallback() throws
RemoteException
{
.. .. ..
}
}
Network and Distributed Programming in Java
Lesson 1B / Slide 12 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
4.
Implementing the Client Interface
•
The code snippet to implement the client interface is:
public class CRemoteImpl extends CRemote
{
public void displayvalue(String s) throws
RemoteException
{
.. .. ..
}
}
Network and Distributed Programming in Java
Lesson 1B / Slide 13 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
5.
Creating an RMI Server
•
The code snippet to create the RMI server is:
public class CallbackServer
{
public static void main (String args []) throws
Exception
{
SRemote b = new SRemoteImpl();
UnicastRemoteObjcet.exportObjcet(b);
Naming.rebind(“callback”, b);
}
}
Network and Distributed Programming in Java
Lesson 1B / Slide 14 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
6.
Creating an RMI Client
•
The code snippet to create the RMI client is:
public class CallbackClient
{
public static void main (String args []) throws
Exception
{
CallbackS =
(SRemote)Naming.lookup(“rmi://192.168.0.52/callback”);
CallbackC = callbackS.getCallback();
UnicastRemoteObjcet.exportObjcet(callbackC);
callbackS.registerClient(callbackC);
}
}
Network and Distributed Programming in Java
Lesson 1B / Slide 15 of 19
Creating Applications Using RMI
Client-Side Callbacks (Contd.)
7.
Running the Application
•
The steps to run an RMI application using client-side callbacks are:
1.
Compile all the Java source files.
javac *.java
2.
Generate the stub and skeleton for the RMI server.
rmic SRemoteImpl
3.
Generate the stub and skeleton for the RMI client.
rmic CRemoteImpl
4.
Start the RMI registry.
start rmiregistry
5.
Run the server-side application.
java CallbackServer
6.
Run the client-side application.
java CallbackClient
Network and Distributed Programming in Java
Lesson 1B / Slide 16 of 19
Creating Applications Using RMI
Demonstration-Implementing a Chat
Application
•
Problem Statement
•
Create a chat application using RMI where an RMI server receives
a message from one client and sends that message to all other
clients.
Network and Distributed Programming in Java
Lesson 1B / Slide 17 of 19
Creating Applications Using RMI
Demonstration-Implementing a Chat
Application (Contd.)
•
Solution
•
The
1.
2.
3.
4.
5.
steps to create the chat application using RMI are:
Create the ServerRemote interface for the chat server
Create the ClientRemote interface for the chat client.
Create the ChatServer class that implements the
ServerRemote interface.
Create the ChatClient class that implements the
ClientRemote interface.
Run the chat application.
Network and Distributed Programming in Java
Lesson 1B / Slide 18 of 19
Creating Applications Using RMI
Summary
•
In this lesson, you learned that:
•
•
•
You use serialization to convert a set of objects into byte streams. The
byte streams can be sent from one JVM to another JVM.
When the RMI server calls back a remote method, such as progress
feedback, or an alert message to the RMI client, these method calls are
called client-side callbacks.
The RMI client calls the exportObject() method of
java.rmi.server.UnicastRemoteObject class to allow the RMI server to
perform client side callbacks.
Network and Distributed Programming in Java
Lesson 1B / Slide 19 of 19