小Ken老師提供方法,就是使用RMI來實作這段.以下是在正式使用前對RMI的測試.
Environment:
=============================================
OS : Windows Vista Bussiness
DEV platform: Eclipse(3.4) + net.genady.rmi.feature_2.1.0.v20081001 (rmi的plug-in)
Implementation:
=============================================
Step 1:產生一個Service 的Interface 並extends javax.rmi.Remote, 程式碼如下:
package com.cenoq.rmi.common;
import java.rmi.Remote;
import java.rmi.RemoteException;
public interface PrintService extends Remote {
public String simpleRemoteMethod(String arg) throws RemoteException;
}
Step2:implements Step1的interface並extends java.rmi.server.UnicastRemoteObject, 程式碼如下:
package com.cenoq.rmi.server;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import com.cenoq.rmi.common.PrintService;
public class PrintServiceImpl extends UnicastRemoteObject implements PrintService {
//這個constructor是一定要滴.
protected PrintServiceImpl() throws RemoteException {
super();
}
public String simpleRemoteMethod(String arg) throws RemoteException {
System.out.println("function called!!");
return "OK";
}
/**
* @param args
*/
public static void main(String[] args) {
try {
Registry r = LocateRegistry.getRegistry();
r.bind("PrintServer", new PrintServiceImpl());
} catch (Exception e) {
e.printStackTrace();
}
}
}
這個Service的implementaion需要指定codebase,可設定如下:
file:/C:/Users/boyce/workspace/RMIServerDemo/bin/ <-- p="" rintserviceimpl.java="">-->
file:/C:/Users/boyce/workspace/RMICommonDemo/bin/ <-- nterface="" p="" rintservice.java="">-->
Step3:產生Client,來呼叫rmi service
package com.cenoq.rmi.client;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import com.cenoq.rmi.common.PrintService;
public class RmiClient {
public static void main(String[] args) {
System.setSecurityManager(new RMISecurityManager());
try {
//注意PrintServer需與bind在rmi registry的名稱相同
PrintService ps = (PrintService) Naming.lookup("rmi://localhost/PrintServer");
String rtn = ps.simpleRemoteMethod("abc");
System.out.println(rtn);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (RemoteException e) {
e.printStackTrace();
} catch (NotBoundException e) {
e.printStackTrace();
}
}
}
Client另外需要一個(java.security.policy)這個Permission file來認証access user, file(seucrity.policy) 如下:
// This file was generated by the RMI Plugin for Eclipse.
///////////////////////////////////////////////////////////////
// This is a sample policy file that grants the application all permissions.
// A policy file is needed by the RMISecurityManager and your application might
// not work after installing the RMISecurityManager unless you provide a
// security policy file at launch.
//
// You can configure the security policy of a launched application using either
// the RMI Launcher or by manually setting the java.security.policy property.
//
// SECURITY NOTE: This security policy is good for development. For deployment
// you may need a stricter security policy.
//
// For more information see:
// http://java.sun.com/docs/books/tutorial/rmi/running.html
// http://java.sun.com/j2se/1.5.0/docs/guide/security/PolicyFiles.html
//
grant {
permission java.security.AllPermission;
// Other options:
// permission java.net.SocketPermission "127.0.0.1:1024-", "accept, connect, listen, resolve";
// permission java.net.SocketPermission "localhost:1024-", "accept, connect, listen, resolve";
// From http://java.sun.com/docs/books/tutorial/rmi/running.html
// Copyright 1995-2005 Sun Microsystems, Inc. Reprinted with permission
// permission java.net.SocketPermission "*:1024-65535", "connect,accept";
// permission java.net.SocketPermission "*:80", "connect";
// permission java.net.SocketPermission "*:1024-65535", "connect,accept";
// permission java.io.FilePermission "c:\\home\\ann\\public_html\\classes\\-", "read";
// permission java.io.FilePermission "c:\\home\\jones\\public_html\\classes\\-", "read";
};