Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2.

Similar presentations


Presentation on theme: "1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2."— Presentation transcript:

1 1 Jini Tutorial, Part 3 Jini Programming

2 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2 Overview of Jini Goals Architecture Basic concepts, protocols Part 3 Jini Programming Part 4 Advanced topics Mobile Jini access Adaptive user interfaces Security

3 3 Contents Software overview – architecture, packages Programming the basic steps discovery, join (service registration) and lookup (finding services) Deployment – where to put what

4 4 The key steps of operation Every service and client will have to discover one or more lookup services Every service will have to register with (join) the discovered lookup service(s) Every client will have to search for (look up) services in the lookup service(s) First we look at the programming of these fundamental steps of operation

5 5 Jini package structure The naming of Jini packages follow the inverted domain name naming convention jini.net.xxx The core classes are in packages jini.net.core.*, stored in jini-core.jar Classes building on core classes are in jini.net.*, stored in file jini-ext.jar Non-standard utility, helper classes are in com.sun.jini.*, stored in file sun- util.jar

6 6 The programmers view of Jini The essential steps of creating a system of Jini services and clients 1.Create well-known interfaces (if required) 2.Create services that implement the interfaces 3.Provide discovery in services and clients 4.Services: Program service registration 5.Clients: Program service lookup

7 7 How we should proceed Simple service example to show these steps 1.Defining service interface 2.Creating the service 3.Programming the Unicast Discovery 4.Service registration 5.Service lookup 6.Multicast discovery 7.The interesting parts

8 8 Creating the interface First we define the well-known interface for our sample service public interface GreetingInterface { public void hello(); }

9 9 Creating the Service Proxy Then we create the proxy class the client will see public class GreetingServiceProxy implements Serializable, GreetingInterface { public GreetingServiceProxy (){ }; public void hello(){ System.out.println(Welcome to Jini World!); }

10 10 What we want to achieve Service A Lookup Service Service Proxy registrar registration register( )

11 11 Unicast discovery LookupLocator lookup = null; ServiceRegistrar registrar = null; lookup = new LookupLocator(jini://hostname); registrar = lookup.getRegistrar();

12 12 Service registration Once we have a proxy to the lookup service, we can register the service ServiceRegistration registration = null; //create serviceItem (no ID, no attributes) try{// register for 100 seconds registration = registrar.register(serviceItem, L); } catch (java.rmi.RemoteException e){ } ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null);

13 13 Putting the service together import net.jini.core.discovery.*; import net.jini.core.lookup.*; public class GreetingService { static public void main(String argv[]) { LookupLocator lookup = null;//object for discovery ServiceRegistrar registrar = null; //lookup serv. proxy try { lookup = new LookupLocator("jini://hostname"); } catch(java.net.MalformedURLException e) {...} // perform unicast discovery try { registrar = lookup.getRegistrar(100000); }catch... }

14 14 Putting the service together... ServiceRegistration registration = null; //create serviceItem (no ID, no attributes) ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null); try{// register for 100 seconds registration = registrar.register(serviceItem, L); }catch (java.rmi.RemoteException e){ } } // GreetingService

15 15 The client side Lookup Service Client 1 Found proxy registrar lookup( ) Interface Template Unicast discovery

16 16 Programming the service lookup System.setSecurityManager( new RMISecurityManager() ); // create template for service search... GreetingServiceInterface returnedService = null; try{ returnedService = (GreetingServiceInterface) registrar.lookup(template); }catch (java.rmi.RemoteException e){... } returnedService.hello(); }

17 17 Creating the template Class[] serviceTypes = new Class[1]; try { serviceTypes[0] = Class.forName( "GreetingServiceInterface"); }catch (ClassNotFoundException e){... } ServiceTemplate template = new ServiceTemplate(null, serviceTypes, null);

18 18 Using Attibutes Why use attributes The type (interface) of a service will only say what we can do with a service. We also need information on the properties of the service (eg type and speed of a printer, supercomputer specialising in climate modelling, storage capacity, location of service) You can use standard Jini attributes Address, Comment, Location, Name, ServiceInfo, ServiceType, Status And/or create your own particular ones

19 19 Attributes Entry[] attribSet = new Entry[3]; attribSet[0] = new Location(G", 002", South Building"); attribSet[1] = new Location(1st", 107", "North Building"); attribSet[2] = new Name(Greeting Service for Euro-Par participants"); ServiceTemplate newTemplate = new ServiceTemplate(null, serviceTypes, attribSet); Since attributes are Java objects and downloaded with the service, they can be used for transferring any type of static or dynamic information from the service to the client

20 20 Is that all? Whats missing? We know how to discover a lookup service, register and lookup services BUT, we can only discover a known lookup service, which is not discovery at all, only connecting to it Multicast discovery to deal with unknown lookup services

21 21 Programming the multicast discovery – Step 1 LookupDiscovery discover = null; try{ discover = new LookupDiscovery(LookupDiscovery.ALL_GROUPS); }catch... This implements the process shown in the next figure. Lookup Service N Discovering entity Lookup Service … Lookup Service 2 Lookup Service 1 multicast request

22 22 Programming the multicast discovery – Step 2 Asynchronous responses are handled by a listener object (implementing the DiscoveryListener interface) discover.addDiscoveryListener(listener); try{ Thread.currentThread().sleep(10000L); } catch... }

23 23 Handling responses in the listener Two methods for joining and leaving public void discovered(DiscoveryEvent e){ //Lookup service discovered } public void discarded(DiscoveryEvent e){ //Clean up after lookup service discarded"); }

24 24 Step 3 -- Handling registration Multicast version of the registration process Note the use of event and the number of lookup services public void discovered(DiscoveryEvent event){ ServiceRegistrar[] registrars = event.getRegistrars(); ServiceRegistration[] registration = null; ServiceItem serviceItem = new ServiceItem(null, new GreetingServiceProxy(), null); for (int i = 0; i < registrars.length; i++){ try { registration[i] = registrars[i].register(serviceItem, L); } catch... }

25 25 Creating a real service proxy Creating a new proxy that talks to the backend service public class GreetingServiceProxy implements Serializable, GreetingInterface { protected GreetingBackEnd be; public GreetingServiceProxy ( be ){ this.be = be; }; public void hello(){ String str = be.getString(); System.out.println(str); }

26 26 Modifying the service The interface for the remote proxy public interface GreetingBackEnd extends Remote{ public String getString(); } We also need to create the Backend Remote object that will receive RMI calls from the proxy public class BackEnd extends UnicastRemoteObject implements GreetingBackEnd{ public String getString(){ return Welcome to Euro-Par; }

27 27 Modifying registration The service will publish a proxy initialised with a reference to the back end service (the remote object) BackEnd be = new BackEnd(); GreetingServiceProxy proxy = new GreetingServiceProxy(be);... ServiceItem serviceItem = new ServiceItem(null, proxy, null);

28 28 Where are we now? We have seen the programming of the discovery, join, lookup protocols Shortcomings: We have no leasing for our service Only up and running services will be located – latecomers (both lookup and other services) are missed. We need notification (events) when this happen

29 29 Service computer Client device Lookup service computer Deployment RMI Daemon HTTP Server for exporting LS classes Lookup Service Client Service HTTP Server for exporting service classes RMI Daemon HTTP Server for exporting client classes

30 30 Summary Overviewed the programming steps of the Discovery, Join and Lookup protocols Have used a simple and real proxy to a Jini service Looked at the use of attributes Overviewed deployment issues We look at the more advanced issues in the next part


Download ppt "1 Jini Tutorial, Part 3 Jini Programming. 2 Tutorial outline Part 1 Introduction Distributed systems Java basics Remote Method Invocation (RMI) Part 2."

Similar presentations


Ads by Google