Download presentation
Presentation is loading. Please wait.
Published byWilson Stoken Modified over 10 years ago
1
Jini, third verse Richard Chapman October 13, 1999
2
The interface // HelloWorldServiceInterface.java Package corejini.chapter5; public interface HelloWorldServiceInterface { public String getMessage(); }
3
Code for the service HelloWorldService.java package corejini.chapter5; import net.jini.discovery.DiscoveryListener; import net.jini.discovery.DiscoveryEvent; import net.jini.discovery.LookupDiscovery; import net.jini.core.lookup.ServiceItem; import net.jini.core.lookup.ServiceRegistrar;
4
Service code, contd import jini.core.lookup.ServiceRegistration; import java.util.Hashtable; import java.io.IOException; import java.io.Serializable; import java.rmi.RemoteException; import java.rmi.RMISecurityManager;
5
Service code, proxy object class HelloworldServiceProxy implements Serializable, HelloWorldServiceInterface { public HelloWorldServiceProxy() { } public String getMessage() { return “Hello, world!”; }
6
Service code, wrapper class public class HelloWorldSerivce implements Runnable { protected final int LEASE_TIME=10*60*1000; protected HashTable registrations = new Hashtable(); protected ServiceItem item; protected LookupDiscovery disco;
7
Listen for discovery events class Listener implements DiscoveryListener { public void discovered(DiscoveryEvent ev) { System.out.println( “discovered a loookup service!”); ServiceRegistrar[] newregs = ev.getRegistrars(); for (int i=0;i<newregs.length; i++) { if(!registrations.containsKey(newregs[I])) { registerWithLookup(newregs[i]); }
8
To discard a lookup service public void discarded(DiscoveryEvent ev) { ServiceRegistrar[] deadregs = ev.getRegistrars(); for (int i=0; i<deadregs.length; i++) { registrations.remove(deadregs[i]); } // end of class Listener from previous slide ago }
9
HelloWorldService constructor public HelloWorldService() throws IOException{ item = new ServiceItem(null,createProxy(),null); //set a security manager if (System.getSecurityManager()==null) { System.setSecurityManager( new RMISecurityManager()); } // search for the “public group”, named by”” disco = new LookupDiscovery(new String[] {“”});
10
Lookup Lookup service Service item Proxy attribute Service item
11
HelloWorldService, cont’d //install a listener disco.addDiscoveryListener(new Listener()); } protected HelloWorldServiceInterface createProxy() { return new HelloWorldServiceProxy(); }
12
HelloWorldService, cont’d protected synchronized void registerWithLookup(ServiceRegistrar registrar) { ServiceRegistration registration = null; try { registration = registrar.register(item, LEASE_TIME); } catch(RemoteException ex) { System.out.println(“Couldn’t register:” + ex.getMessage()); return; }
13
HelloWorldService, cont’d if (item.serviceID == null) { item.serviceID = registration.getServiceID(); System.out.println(“Set serviceID to “+ item.serviceID); } registrations.put(registrar,registration); // end of registerWithLookup from prev. slide }
14
Thread to keep the JVM alive // thread in HelloWorldService to make sure // the JVM doesn’t exit public void run() { while (true) { try { Thread.sleep(1000000); } catch (InterrruptedException ex) { }
15
Create service, start thread public static void main(String args[]) { try { HelloWorldService hws = new HelloWorldService(); new Thread(hws).start(); } catch (IOException ex) { System.out.println(“Couldn’t”+ “create service:”+ex.getMessage()); } // finally the end of HelloWorldService }
16
Client code //needs most of the imports that the server did, plus import net.jini.core.lookup.ServiceTemplate; import java.util.Vector;
17
Client code public class HelloWorldClient implements Runnable { protected ServiceTemplate template; protected LookupDiscovery disco; class Listener implements DiscoveryListener{ public void discovered(DiscoveryEvent ev){ ServiceRegistrar[] newregs = ev.getRegistrars(); for (int i=0;i<newregs.length;i++){ lookForService(newRegs[i]); } public void discarded(DiscoveryEvent ev){} }
18
Client code public HelloWorldClient() throws IOException { Class[] types = {HelloWorldServiceInterface.class}; template = new ServiceTemplate(null,types, null); if (System.getSecurityManager() == null) { System.setSecurityManager( new RMISecurityManager()); }
19
Client code // only search the public group disco = new LookupDiscovery(new String[] {“”}); //install a listener disco.addDiscoveryListener(new Listener()); // end of HelloWorldClient }
20
Look for proxies protected Object lookforService(ServiceRegistrar lusvc) { Object o = null; try { o = lusvc.lookup(template); } catch (RemoteException ex) { System.err.println(“Error doing”+ “lookup: ”+ex.getMessage()); return null; }
21
Look for proxies if (o==null) { System.err.println(“No matching”+ “service”); return null; } System.out.println(“Got a matching”+ ”service”); System.out.println(“It’s message is “+ ((HelloWorldServiceInterface) o).getMessage()); return o; }
22
Client, cont’d // client needs a thread “run” exactly // like server, to keep JVM alive public void run() { while (true) {.... //method to start the thread for clent public static void main(String args[]) { try { HelloWorldClient hwc = new HelloWorldClient(); new Thread(hwc).start; } catch (IOException ex) { System.out.println(“Couldn’t create cli:”+ ex.getMessage()); }}}
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.