1. 远程代理
a. 远程接口:
import java.rmi.Remote;
import java.rmi.*;
public interface MyRemote extends Remote {
public String callStart(String from, String to, String content) throws RemoteException;
}
b.远程接口实现:
import java.lang.Exception;
import java.rmi.server.UnicastRemoteObject;
import java.rmi.*;
import java.rmi.server.*;
public class MyRemoteImpl extends UnicastRemoteObject implements MyRemote{
public String callStart(String from, String to, String content) {
return "Hello RemoteCall , I am a Remote Service";
}
public MyRemoteImpl() throws RemoteException {
}
public static void main(String[] args) {
try {
MyRemote service = new MyRemoteImpl();
Naming.rebind("RemoteHello", service);
}catch(Exception ex) {
ex.printStackTrace();
}
}
}
c. 使用rmic命令产生对应的Stub
rmic MyRemoteImpl
d. 启动RMI Registry
rmiregistry &
e. 启动远程服务
java MyRemoteImpl
f. 测试
java MyRemoteTest
import java.lang.Exception;
import java.rmi.*;
public class MyRemoteTest {
public void go() {
try {
MyRemote service = (MyRemote) Naming.lookup("rmi://127.0.0.1/RemoteHello");
String ra = service.callStart("from", "to", "content");
System.out.println(ra);
}catch(Exception ex) {
ex.printStackTrace();
}
}
public MyRemoteTest(){
}
public static void main(String[] args) {
new MyRemoteTest().go();
}
}
2. 动态代理
接口代码部分:
public interface PersonBean {
String getName();
String getGender();
String getInterests();
void setName(String name);
void setGender(String gender);
void setInterests(String interests);
void setHotOrNotRating(int rating);
}
接口的具体实现类:
public class PersonBeanImpl implements PersonBean{
String name;
String gender;
String interests;
int rating;
int ratingCount = 0;
public String getName() {return name;}
public String getGender() {return gender;}
public String getInterests() {return interests;}
public int getRating() {return rating;}
public int getRatingCount() {return ratingCount;}
public void setName(String name) { this.name = name;}
public void setGender(String gender) { this.gender = gender;}
public void setInterests(String interests) { this.interests = interests;}
public void setHotOrNotRating(int rating) { this.rating += rating; ratingCount++;}
}
两个动态转换handler
NonOwnerInvocationHandler:
import java.lang.reflect.*;
import java.lang.IllegalAccessException;
import java.lang.Exception;
public class NonOwnerInvocationHandler implements InvocationHandler {
PersonBean person;
public NonOwnerInvocationHandler(PersonBean person) {
this.person = person;
}
public Object invoke(Object proxy, Method method, Object[] args)
throws IllegalAccessException {
try {
if(method.getName().startsWith("get")) {
return method.invoke(person, args);
} else if(method.getName().startsWith("setHotOrNotRating")) {
return method.invoke(person, args);
} else if(method.getName().startsWith("set")) {
throw new IllegalAccessException();
}
} catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
}
OwnerInvocationHandler:
import java.lang.reflect.*;
import java.lang.IllegalAccessException;
import java.lang.Exception;
public class OwnerInvocationHandler implements InvocationHandler {
PersonBean person;
public OwnerInvocationHandler(PersonBean person) {
this.person = person;
}
public Object invoke(Object proxy, Method method, Object[] args) throws IllegalAccessException {
try {
if(method.getName().startsWith("get")) {
return method.invoke(person, args);
} else if(method.getName().startsWith("setHotOrNotRating")) {
throw new IllegalAccessException();
} else if(method.getName().startsWith("set")) {
return method.invoke(person, args);
}
} catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
}
测试用例:AProxy
import java.lang.reflect.*;
public class AProxy {
public PersonBean getOwnerProxy(PersonBean person) {
return (PersonBean) Proxy.newProxyInstance(
person.getClass().getClassLoader(),
person.getClass().getInterfaces(),
new OwnerInvocationHandler(person));
}
public PersonBean getNonOwnerProxy(PersonBean person) {
return (PersonBean) Proxy.newProxyInstance(
person.getClass().getClassLoader(),
person.getClass().getInterfaces(),
new NonOwnerInvocationHandler(person));
}
public static void main(String[] args) {
AProxy ap = new AProxy();
PersonBeanImpl a = new PersonBeanImpl();
PersonBeanImpl b = new PersonBeanImpl();
PersonBean c, d;
c = ap.getOwnerProxy(a);
d = ap.getNonOwnerProxy(b);
c.setName("TT");
c.setGender("Male");
c.setInterests("cddj");
c.setHotOrNotRating(10);
System.out.println(c.getName());
System.out.println(c.getGender());
System.out.println(c.getInterests());
System.out.println(a.getRating());
System.out.println(a.getRatingCount());
d.setName("High");
d.setGender("FeMale");
d.setInterests("adfsdfs");
d.setHotOrNotRating(15);
d.setHotOrNotRating(20);
System.out.println(d.getName());
System.out.println(d.getGender());
System.out.println(d.getInterests());
System.out.println(b.getRating());
System.out.println(b.getRatingCount());
}
最后来一张非正规”类图“