RMI限java之间,且使用Stream 技术(serialize)传输。WebService是通过XML来传输数据,可用http等协议因此可在异构系统间传递,并且可以穿过防火墙,可在公网上远程调用。
(1)远程调用接口
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface ComputerEngineRemote extends Remote {
public Object excuteTask(Task task) throws RemoteException;
}
(2)远程调用接口实现
public class ComputerEngine extends UnicastRemoteObject implements
ComputerEngineRemote {
/**
*
*/
private static final long serialVersionUID = 1L;
protected ComputerEngine() throws RemoteException {
super();
}
public Object excuteTask(Task task) throws RemoteException {
return task.excute();
}
}
(3)定义业务接口
public interface Task extends Serializable {
Object excute();
}
(2)业务接口实现
public class TaskImpl implements Task {
private static final long serialVersionUID = 1L;
public Object excute() {
return "Successful!";
}
}
(4)将远程对象注册到 RMI 的注册表
public class Bootstrap {
/* "引导"过程(Bootstrap)。引导程序负责创建和注册远程对象。*/
public static void main(String[] args) throws Exception {
String name="ComputerEngine";
//System.setProperty("java.rmi.server.hostname","172.31.3.33");
//System.setProperty( "java.security.policy", "client.policy" );
//System.setSecurityManager(new java.rmi.RMISecurityManager());
LocateRegistry.createRegistry(1099);
ComputerEngine server = new ComputerEngine();
Naming.rebind("rmi://127.0.0.1:1099/ComputerEngine", server);
System.out.println("Ready to accept tasks");
}
}
(5)远程调用接口
public class Client {
public static void main(String[] args) throws Exception {
String name="rmi://127.0.0.1:1099/ComputerEngine";
try{
//定义接口
ComputerEngineRemote engineRemote=(ComputerEngineRemote) Naming.lookup(name);
System.out.println(engineRemote.excuteTask(new TaskImpl()));
}catch(ConnectException e){
e.printStackTrace();
System.out.println("No listener!");
}
}
}
(6)java.policy
permission java.net.SocketPermission "127.0.0.1:1099","connect,resolve";
permission java.security.AllPermission;
Java应用程序环境的安全策略,详细说明了对于不同的代码所拥有的不同资源的许可,它由一个Policy对象来表达。为了让applet(或者运行在 SecurityManager下的一个应用程序)能够执行受保护的行为,例如读写文件,applet(或 Java应用程序)必须获得那项操作的许可,安全策略文件就是用来实现这些许可。
grant {
//对系统和用户目录“读”的权限
permission java.util.PropertyPermission “user.dir", “read";
permission java.util.PropertyPermission “user.home", “read";
permission java.util.PropertyPermission “java.home", “read";
permission java.util.PropertyPermission “java.class.path", “read";
permission java.util.PropertyPermission “user.name", “read";
//对线程和线程组的操作权限
permission java.lang.RuntimePermission “modifyThread";
permission java.lang.RuntimePermission “modifyThreadGroup";
//操作Socket端口的各种权限
permission java.net.SocketPermission “-", “listen";
permission java.net.SocketPermission “-", “accept";
permission java.net.SocketPermission “-", “connect";
permission java.net.SocketPermission “-", “read";
permission java.net.SocketPermission “-", “write";
//读写文件的权限
permission java.io.FilePermission “-", “read";
permission java.io.FilePermission “-", “write";
//退出系统的权限,例如System.exit(0)
permission java.lang.RuntimePermission “exitVM";
};
(7)远程调用,得到图片文件
Remote:
public Object excute() {
String inputFile="D:/images/img.bmp";
FileInputStream source=null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
source = new FileInputStream(new File(inputFile));
int bytes_read;
byte[] buffer = new byte[1024];
while (true) {
bytes_read = source.read(buffer);
if (bytes_read == -1) {
break;
}
bos.write(buffer, 0, bytes_read);
}
byte[] data = bos.toByteArray();
bos.close();
bos = null;
return data;
}catch(Exception e){
}finally{
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
bos = null;
}
}
return null;
}
Client:
public static void main(String[] args) throws Exception {
String name="rmi://127.0.0.1:1099/ComputerEngine";
try{
ComputerEngineRemote engineRemote=(ComputerEngineRemote) Naming.lookup(name);
File outputFile = new File("D:/images/receive.bmp");
FileOutputStream target = new FileOutputStream(outputFile);
target.write((byte[])engineRemote.excuteTask(new TaskImpl()));
System.out.println("OK!");
}catch(ConnectException e){
e.printStackTrace();
System.out.println("No listener!");
}
}