五、参考资料
import java.rmi.*;
public interface TestSvrInterface extends Remote{
public String sayHello() throws RemoteException;
}
|
import net.jini.core.discovery.LookupLocator;
import net.jini.core.entry.*;
import net.jini.core.lease.Lease;
import net.jini.core.lookup.*;
import net.jini.lookup.entry.Name;
import com.sun.jini.lease.LeaseRenewalManager;
import com.sun.jini.lookup.JoinManager;
import java.rmi.*;
import java.rmi.server.*;
public class TestSvr extends UnicastRemoteObject implements
TestSvrInterface{
public TestSvr() throws RemoteException{
super();
}
public String sayHello() throws RemoteException{
System.out.println("I'm called");
return ("TestSvr said... Greeting!");
}
public static void main(String args[]) throws Throwable{
// 设定自己的 attributes.
Entry[] eAttrib = new Entry[1];
eAttrib[0] = new Name("TestSvr");
TestSvr ts = new TestSvr();
// 建立一个LookupLocator物件,以寻找特定的 lookup service.
LookupLocator ll = new LookupLocator("jini://specific-host:4160");
// 找到 lookup service之後,建立一个与其沟通的介面。
ServiceRegistrar sreg = ll.getRegistrar();
// 透过此介面,向 lookup service 注册其服务资讯。
sreg.register (new ServiceItem(null,ts,eAttrib), Lease.FOREVER);
System.out.println("Waiting request......");
}
}
|
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.*;
import net.jini.core.entry.*;
import net.jini.lookup.entry.Name;
class TestClnt{
public static void main(String args[]) throws Throwable{
// 建立一个LookupLocator物件,以寻找特定的 lookup service.
LookupLocator ll = new LookupLocator("jini://specific-host:4160");
// 找到 lookup service之後,建立一个与其沟通的介面。
ServiceRegistrar sreg = ll.getRegistrar();
// 设定要寻找的服务之 attributes,并做成样版。
Entry[] eAttrib = new Entry[1];
eAttrib[0] = new Name("TestSvr");
ServiceTemplate tmplt = new ServiceTemplate (null, null, eAttrib);
// 将样版送至 lookup service,并要求其找出符合样版的服务。
TestSvrInterface tsi = (TestSvrInterface)( sreg.lookup(tmplt) );
// 找到服务之後,呼叫此服务之 method.
System.out.println("--- "+tsi.sayHello()+" ---");
}
}
|