RMIアプリケーション
3233 ワード
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Date;
/**
* Description:<br>
* 1. , java.rmi.Remote
* 2. ,
* 3. : rmiregistry
* 4. : ,
* @author JOJO
* @version 0.1
*/
public interface HelloService extends Remote
{
public String echo (String msg) throws RemoteException;
public Date getTime () throws RemoteException;
}
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
public class HelloServiceImpl extends UnicastRemoteObject implements HelloService
{
private String name;
protected HelloServiceImpl(String name) throws RemoteException
{
this.name = name;
/*
* UnicastRemoteObject exportObject(Remote obj,int port)。
* obj , , 。
* port 。 , UnicastRemoteObject
* exportObject(Remote obj,int port)。
*/
// UnicastRemoteObject.exportObject(this, 0);
}
public String echo (String msg) throws RemoteException
{
System.out.println(name + ": echo() 。");
return "echo:" + msg + " from " + name;
}
public Date getTime () throws RemoteException
{
System.out.println(name + " getTime() 。");
return new Date();
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Description:<br>
* @author JOJO
* @version 0.1
*/
public class SimpleServer
{
public static void main (String[] args)
{
try
{
HelloService service1 = new HelloServiceImpl("service1");
HelloService service2 = new HelloServiceImpl("service2");
// port Registry 。
Registry registry = LocateRegistry.createRegistry(1099);
registry.rebind("HelloService1", service1);
registry.rebind("HelloService2", service2);
System.out.println(" HelloService 。");
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
/**
* Description:<br>
*
* @author JOJO
* @version 0.1
*/
public class SimpleClient
{
public static void main (String[] args)
{
try
{
// host port Registry 。
Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1099);
HelloService service1 = (HelloService) registry.lookup("HelloService1");
HelloService service2 = (HelloService) registry.lookup("HelloService2");
System.out.println(service1.echo("hello"));
System.out.println(service2.getTime());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}