1:文件Service.java ServiceImpl.java ServiceServer.java ServiceClient.java client.policy
结构
src
net.loocky.rmi/ *.java
client.policy
文件如下:
Service.java是个interface
package net.loocky.rmi;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface Service extends Remote {
public String getName() throws RemoteException;
public void setName(String name) throws RemoteException;
}
ServiceImpl.java Service的实现
package net.loocky.rmi;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
public class ServiceImpl extends UnicastRemoteObject implements Service {
private static final long serialVersionUID = 1L;
private String name;
public ServiceImpl(String name) throws RemoteException {
this.name = name;
}
public String getName() throws RemoteException {
return name;
}
public void setName(String name) throws RemoteException {
this.name = name;
}
}
ServiceServer
package net.loocky.rmi;
import java.rmi.Naming;
public class ServiceServer {
/**
* @param args
*/
public static void main(String[] args) {
try {
ServiceImpl s1 = new ServiceImpl("my name is john");
ServiceImpl s2 = new ServiceImpl("my name is loocky");
Naming.rebind("john", s1);
Naming.rebind("loocky", s2);
System.out.println("waiting for clients...");
} catch (Exception e) {
e.printStackTrace();
}
}
}
ServiceClient
package net.loocky.rmi;
import java.rmi.Naming;
import java.rmi.RMISecurityManager;
public class ServiceClient {
/**
* @param args
*/
public static void main(String[] args) {
System.setSecurityManager(new RMISecurityManager());
String url = "rmi://127.0.0.1:1099/";
try {
Service s1 = (Service) Naming.lookup(url + "john");
Service s2 = (Service) Naming.lookup(url + "loocky");
System.out.println(s1.getName());
s2.setName("my name is not loocky");
System.out.println(s2.getName());
} catch (Exception e) {
e.printStackTrace();
}
}
}
client.policy
grant
{
permission java.security.AllPermission;
// permission java.net.SocketPermission "127.0.0.1:1099","connect";
// permission java.net.SocketPermission "127.0.0.1:80","connect";
};
有了这些文件下面要做的事情是
1:进入bin/ rmic net.loocky.rmi.ServiceImpl 生成STUB
2:dos下面 rmiregistry
3:java net.loocky.rmi.ServiceServer
4: java -Djava.security.policy=client.policy net.loocky.rmi.ServiceClient
5:查看控制台的结果吧!
posted on 2006-05-26 11:10
小小程序程序员混口饭吃 阅读(384)
评论(0) 编辑 收藏 所属分类:
java