Java RMI demo--Java RMI Example
This article gives an example of a Java RMI where the registration process is using the registry
This example contains a total of 5 files, namely Helloclient,hello,helloserver,configur,config.properti
/*hello.java*/
/*
* Created on 2004-11-10
*
*/
Package test.rmi;
Import Java.rmi.Remote;
Import java.rmi.RemoteException;
/**
* @author Dogcome
*
* <p> Remote method call interface, define remote invocation method </p>
*/
Public interface Hello extends Remote {
String SayHello () throws remoteexception;
}
/*helloserver*/
/*
* Created on 2004-11-10
*
*/
Package test.rmi;
Import java.rmi.RemoteException;
Import Java.rmi.registry.LocateRegistry;
Import Java.rmi.registry.Registry;
Import Java.rmi.server.UnicastRemoteObject;
/**
* @author Dogcome
*
*<p> server-side, implements the method in the Hello interface to implement the specific business logic of the remote invocation method </p>
*/
public class HelloServer extends UnicastRemoteObject implements Hello {
String name;
Public HelloServer (String s) throws RemoteException {
Super ();
Name=s;
}
Public String SayHello () throws RemoteException {
TODO auto-generated Method Stub
Return "Hello world!";
}
public static void Main (string[] args) {
/** the following sentence to add, you need to authenticate the authority, that is, to increase the. policy file
* and use the following format on the command line
* Java-djava.security.policy=java.policy Test.rmi.HelloServer
*/
/*system.setsecuritymanager (New Rmisecuritymanager ());
Registry Registry = null;
try {
/** starts the registration server and uses this statement to no longer need to be in the command line environment
* Start the Registry service
*/
Registry = Locateregistry.getregistry ();
/* If no connection is obtained, this sentence throws an exception, followed by the relevant processing after capture * *
Registry.list ();
System.out.println ("Register the Exist server!"); $NON-nls-1$
catch (RemoteException re) {
try {
int port = Integer.parseint (Configur
. getString ("Helloserver.registryserverport")); $NON-nls-1$
Registry = Locateregistry.createregistry (port);
System.out.println ("Create Registry server!"); $NON-nls-1$
catch (Exception e) {
E.printstacktrace ();
}
}
try {
HelloServer HelloServer = new HelloServer ("Hello");
Registry
. rebind (
Configur.getstring ("Helloserver.helloservername"), HelloServer); $NON-nls-1$
SYSTEM.OUT.PRINTLN ("HelloServer server start!"); $NON-nls-1$
catch (Exception e) {
E.printstacktrace ();
}
}
}
/*helloclient*/
/*
* Created on 2004-11-10
*
*/
Package test.rmi;
Import java.rmi.*;
Import Java.rmi.registry.LocateRegistry;
Import Java.rmi.registry.Registry;
/**
* @author Dogcome
*
* <p> client, need to register server and use Jndi</p>
*/
public class Helloclient {
public static void Main (string[] args) {
/** the following sentence to add, you need to authenticate the authority, that is, to increase the. policy file
* and use the following format on the command line
* Java-djava.security.policy=java.policy Test.rmi.HelloServer
*/
/*system.setsecuritymanager (New Rmisecuritymanager ());
try {
/* Registration Server * *
String hostname=configur.getstring ("Helloserver.registryservername");
int Port=integer.parseint (configur.getstring ("Helloserver.registryserverport"));
Registry registry=locateregistry.getregistry (Hostname,port);
Hello hello= (Hello) registry.lookup (configur.getstring ("Helloserver.helloservername"));
String Message=hello.sayhello ();
SYSTEM.OUT.PRINTLN (message);
}catch (Exception e) {
E.printstacktrace ();
}
}
}
/*
* Created on 2004-11-12
*
*/
Package test.rmi;
Import java.util.MissingResourceException;
Import Java.util.ResourceBundle;
/**
* @author Dogcome
*
* <p> Get the configuration information required for system operation, the corresponding configuration file name is called config.properties</p>
*/
public class Configur {
private static final String Bundle_name = "Test.rmi.config";
private static final ResourceBundle Resource_bundle = ResourceBundle
. Getbundle (Bundle_name);
Private Configur () {
}
/**
* <p> obtain profile information via key name </p>
* @param key Key Name
* @return String configuration file information
*/
public static string getString (String key) {
try {
return resource_bundle.getstring (key);
catch (MissingResourceException e) {
Return '! ' + key + '! ';
}
}
}
#config. Properties
helloserver.registryserverport=1111
Helloserver.helloservername=helloserver
helloserver.registryservername=127.0.0.1
First use the rmic test.rmi.HelloServer command to generate the required stub and Skel two class files
Then run the HelloServer server Java test.rmi.HelloServer
Last run Helloclient client Java test.rmi.HelloClient
Screen output HelloWorld, so that one of the simplest RMI remote calls succeeded