Unit 3 RMI
Unit 3 RMI
Inside the server program, a remote object is created and reference of that
object is made available for the client (using the registry).
The client program requests the remote objects on the server and tries to
invoke its methods.
To write an RMI Java application, you would have to follow the steps given
below −
We need to implement the remote interface created in the earlier step. (We can
write an implementation class separately or we can directly make the server
program implement this interface.)
An RMI server program should implement the remote interface or extend the
implementation class. Here, we should create a remote object and bind it to
the RMIregistry.
To develop a server program −
Create a client class from where you want invoke the remote object.
Create a remote object by instantiating the implementation class as shown
below.
Export the remote object using the method exportObject() of the class
named UnicastRemoteObject which belongs to the
package java.rmi.server.
Get the RMI registry using the getRegistry() method of
the LocateRegistry class which belongs to the package java.rmi.registry.
Bind the remote object created to the registry using the bind() method of
the class named Registry. To this method, pass a string representing the
bind name and the object exported, as parameters.
import java.rmi.registry.Registry;
import java.rmi.registry.LocateRegistry;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
registry.bind("Hello", stub);
System.err.println("Server ready");
} catch (Exception e) {
System.err.println("Server exception: " + e.toString());
e.printStackTrace();
}
}
}
Write a client program in it, fetch the remote object and invoke the required
method using this object.
Create a client class from where your intended to invoke the remote
object.
Get the RMI registry using the getRegistry() method of
the LocateRegistry class which belongs to the package java.rmi.registry.
Fetch the object from the registry using the method lookup() of the
class Registry which belongs to the package java.rmi.registry.
To this method, you need to pass a string value representing the bind
name as a parameter. This will return you the remote object.
The lookup() returns an object of type remote, down cast it to the type
Hello.
Finally invoke the required method using the obtained remote object.
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
Or,
Open the folder where you have stored all the programs and compile all
the Java files as shown below.
Javac *.java
Java Server
java Client
Verification − As soon you start the client, you would see the following output
in the server.
JavaBean
//Employee.java
package mypack;
public class Employee implements java.io.Serializable{
private int id;
private String name;
public Employee(){}
public void setId(int id){this.id=id;}
public int getId(){return id;}
public void setName(String name){this.name=name;}
public String getName(){return name;}
}
To access the JavaBean class, we should use getter and setter methods.
package mypack;
public class Test{
public static void main(String args[]){
Employee e=new Employee();//object is created
e.setName("Arjun");//setting value to the object
System.out.println(e.getName());
}}
JavaBean Properties
A JavaBean property is a named feature that can be accessed by the user of the
object. The feature can be of any Java data type, containing the classes that you
define.
ad, write, read-only, or write-only. JavaBean features are accessed through two
methods in the JavaBean's implementation class:
1. getPropertyName ()
For example, if the property name is firstName, the method name would be
getFirstName() to read that property. This method is called the accessor.
2. setPropertyName ()
For example, if the property name is firstName, the method name would be
setFirstName() to write that property. This method is called the mutator.
Advantages of JavaBean
Disadvantages of JavaBean
Introspection
Purpose of Introspection
A growing number of Java object repository sites exist on the Internet in answer
to the demand for centralized deployment of applets, classes, and source code in
general. Any developer who has spent time hunting through these sites for
licensable Java code to incorporate into a program has undoubtedly struggled
with issues of how to quickly and cleanly integrate code from one particular
source into an application.
Introspection API
The JavaBeans API architecture supplies a set of classes and interfaces to
provide introspection.
Each class represented in this group describes a particular attribute of the bean.
For example, the isBound method of the PropertyDescriptor class indicates
whether a PropertyChangeEvent event is fired when the value of this property
changes.
To open the BeanInfo dialog box, expand the appropriate class hierarchy to the
bean Patterns node. Right-click the bean Patterns node and choose BeanInfo
Editor from the pop-up menu. All elements of the selected class that match
bean-naming conventions will be displayed at the left in the BeanInfo Editor
dialog box as shown in the following figure:
Select one of the following nodes to view and edit its properties at the right of
the dialog box:
BeanInfo
Bean
Properties
Methods
Event Sources
Special symbols (green and red) appear next to the subnode to indicate whether
an element will be included or excluded from the BeanInfo class.
If the Get From Introspection option is not selected, the node's subnodes are
available for inclusion in the BeanInfo class. To include all subnodes, right-
click a node and choose Include All. You can also include each element
individually by selecting its subnode and setting the Include in BeanInfo
property. If the Get From Introspection option is selected, the setting the
properties of subnodes has no effect in the generated BeanInfo code.
The following attributes are available for the nodes for each bean, property,
event sources, and method:
For Event Source nodes, the following Expert properties are available:
Unicast (read-only)
In Default Event Set
Introspection Sample
The following example represents code to perform introspection:
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
This example creates a non-visual bean and displays the following properties
derived from the BeanInfo object:
class
name
size
Note that a class property was not defined in the SimpleBean class. This
property was inherited from the Object class. To get properties defined only in
the SimpleBean class, use the following form of the getBeanInfo method:
The Bean Developer Kit (BDK), available from the Java Soft site, is a simple
example of a tool that enables you to create, configure, and connect a set of
Beans. There is also a set of sample Beans with their source code. This section
provides-step-by-step instructions for installing and using this tool.
‘In this chapter, instructions are provided for a Windows 95/98/NT environment.
The procedure» for a.UNIX platform are similar, some of the commands arc
different.
Installing the BDK
The JDK must be installed on your machine for the DDKto work. Confirm that
the JOK tools are accessible from your environment. The’BDK can then be
downloaded from the Java Soft site’ (http://java.sun.com).Itis packaged as one
file that is a self-extracting archive. Follow the instructions to install it on tour
machine.
Starting the BDK
To start the DDK, follow these steps:
1. Change to the directory c:\bdk\beanbox.
2. Execute the batcl(·file called run.bat. This causes the BDK to display the
three windows shown Toolbox lists allof the different Beans that have been
included with the BDK. Bean Box provides an area to layout and connect the
Beans selected from the Too Box. Properties .
Using the BDK
This section describes how to create an application by using some of the Beans
provided with the BDK. First, the Molecule Bean displays at three-dimensional
view of a molecule. It may be configured to present one of the following
molecules: hy ironical. acid, benzene, counterintelligence, anticyclone, ethane, or
water. This compos net also has methods that allow the molecule to be rotated in
space along its X or Y axis. -Second, the Our Bean provides a push-button
functionality. We will have one.
Create and Configure an Instance of the ,Molecule Bean
Follow these steps to create o,.md configu.re an instance of the Molecule Bean.
1. Position the cursor on ‘the Tool Box entry labeled Molecule and click the left
mouse button. You should see the cursor change to a cross.
2. Move the cursor to the Bean Box display area and click the left mouse button
in
approximately the area where you wish the Bean to be displayed. You should
see a rectangular region appear that contains a display of a molecule. This
areas surrounded by a hatched. border, indicating that it is currently selected.
3. You can preposition the Molecule Bean by positioning the cursor over one of
the
hatched borders and dragging the Bean.
4. You can change the molecule that is displayed by changing the selection in the
Properties window. Notice that the Bean display changes immediately when
you change the selected molecule.
There are two key benefits of using a JAR file. The first is the ability to
aggregate hundreds, if not thousands, of different files that make up an
application. The second is the means to compress all of the contained files,
greatly reducing the size of the application, and making it easier to move the
JAR file over a network and between environments.
Since a JAR file uses a standard compression algorithm, opening a JAR file is
as simple as changing its extension from .jar to .zip and extracting the
contents using a standard decompression tool.
Opening a
JAR file and exploring the contents
For example, the JAR utility command to extract the contents of a JAR file
named my_java_app.jar looks as follows:
package theserverside;
import javax.swing.*;
If this code was packaged in a JAR file, the JAR file would execute it because it
contains code that can be run directly by the JRE. However, to run the
executable JAR file, you don't use the JAR utility of the JDK. Instead, you use
the special java.exe utility, and, thus, the name of the jar file is specified.
The syntax to run an executable JAR file named my_java_app.jar using the
JDK's java.exe utility is as follows:
Test.
To create a bean, type the above code and save it as SimpleBean.java. Comiple
the above code using the javac.exe compiler as under:
javac SimpleBean.java
Create a manifest file with a .mf extension as under. The Manifest file is a text
file which contains the name of the class file.
SimpleBean.mf
Name: SimpleBean.class
Java-Bean: True
As you are aware that javac.exe creates a separate .class file for every class
defined in the program. Hence, if a program has five classes, the compiler will
create five class file each with their respective name and a .class extension.
This proved to be drawback as the applet loader makes separate connections for
each file while loading the applet. To overcome this problem, the engineers at
Java Soft, the Sun Microsystems subsidiary for Java introduced the idea fo
compressing all the files together which can be unzipped at the applet's client
machine when the applet executes.
Jar is a zip or compression utility tailored to Java's needs. Pass the names of all
the files in the project to the JAR utility, which compresses all of them together
and creates a compressed file with a .jar extension.
Creating a JAR file for the example code created eariler on.
This will create a jar file:SimpleBean.jar. Copy this file in the jars directory
under the BDK folder and start the Bean Box. The ToolBox of the Bean Box
now displays the bean created underline.