CS 696 Emerging Technologies: Java Distributed Computing Spring Semester, 1999 Simple Jini Example |
||
---|---|---|
© 1999, All Rights Reserved, SDSU & Roger Whitney San Diego State University -- This page last updated 18-Mar-99 |
Hello World Example
This example:
package whitney.jini.examples.hello; import java.rmi.Remote; import java.rmi.RemoteException; public interface HelloInterface extends Remote { public String sayHello() throws RemoteException; }
HelloServer package whitney.jini.examples.hello; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceID; import net.jini.lookup.entry.Name; import com.sun.jini.lookup.ServiceIDListener; import com.sun.jini.lookup.JoinManager; import com.sun.jini.lease.LeaseRenewalManager; import java.rmi.Remote; import java.rmi.RemoteException; import java.rmi.RMISecurityManager; import java.rmi.server.UnicastRemoteObject; public class HelloServer extends UnicastRemoteObject implements HelloInterface, ServiceIDListener { private ServiceID myID; public HelloServer() throws RemoteException { } public String sayHello () throws RemoteException { return ("Hello World from Jini Hello server!"); } public void serviceIDNotify (ServiceID uniqueID) { myID = uniqueID; System.out.println("server: ID set: " + myID ); }
HelloServer Main
public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); HelloServer myServer = new HelloServer (); Entry[] identityingAttributes = new Entry[1]; identityingAttributes[0] = new Name("HelloServer"); JoinManager myManager = new JoinManager ( myServer, identityingAttributes, myServer, new LeaseRenewalManager () ); System.out.println ("Server has been Joined!"); } }
HelloClient package whitney.jini.examples.hello; import net.jini.core.entry.Entry; import net.jini.core.lookup.ServiceTemplate; import net.jini.core.lookup.ServiceRegistrar; import net.jini.core.discovery.LookupLocator; import net.jini.lookup.entry.Name; import java.rmi.RMISecurityManager; public class HelloClient { public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar (); Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("HelloServer"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); HelloInterface myServerInterface = (HelloInterface) registrar.lookup (template); System.out.println ( myServerInterface.sayHello () ); } }
Example Explained
The Client
public static void main (String[] args) throws Exception { System.setSecurityManager (new RMISecurityManager ()); LookupLocator lookup = new LookupLocator ("jini://eli.sdsu.edu"); ServiceRegistrar registrar = lookup.getRegistrar();
Service Lookup
net.jini.core.discovery.LookupLocator
A utility class that performs unicast discovery for a Lookup service
Constructors LookupLocator(String url)
LookupLocator(String host, int port)
The URL type is jini
Default port for a lookup service is 4160
Translate CAFE-BABE from hex to decimal to get 4160
String getHost()
int getPort()
ServiceRegistrar getRegistrar()
ServiceRegistrar getRegistrar(int timeout)
Interface net.jini.core.lookup.ServiceRegistrar
Methods Information about available servicesClass[] getEntryClasses(ServiceTemplate tmpl) Object[] getFieldValues(ServiceTemplate, int setIndex, String field) Class[] getServiceTypes(ServiceTemplate, String prefix)
Object lookup(ServiceTemplate tmpl) ServiceMatches lookup(ServiceTemplate tmpl, int maxMatches)
String[] getGroups()
LookupLocator getLocator()
ServiceID getServiceID()
EventRegistration notify(ServiceTemplate tmpl, int transitions, RemoteEventListener listener, MarshalledObject handback, long leaseDuration)
ServiceRegistration register(ServiceItem item, long leaseDuration)
HelloClient Continued // Create ServiceTemplate used to match or find the server // we want Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("HelloServer"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); HelloInterface myServerInterface = (HelloInterface) registrar.lookup (template);Interface net.jini.core.entry.Entry Entry subclasses:
Entry is a marker interface, it has no methods
Matching Entries
Let T (for template) be an object of a subclass Entry
Let E be an object of a subclass of Entry
A field of an Entry object is a wildcard it its value is null
The template T matches E if:
net.jini.core.lookup.ServiceTemplate
Constructor ServiceTemplate(ServiceID serviceID, Class[] serviceTypes, Entry[] attrSetTemplates)
Public Fields Entry[] attributeSetTemplates
ServiceID serviceID
Class[] serviceTypes
ServiceTemplates are used to match for services
A service template (tmpl) matches service item (item) a if:
Standard Entry Subclasses
net.jini.lookup.entry Classes
Some basic entry classes with their fields. All fields are Strings
Address |
ServiceInfo |
Location |
Name |
country
|
manufacturer
|
building
|
name |
locality
|
model
|
floor
|
|
organization
|
serialNumber
|
room
|
|
organizationalUnit
|
vendor
|
|
Comment |
postalCode
|
version
|
|
comment |
stateOrProvince
|
|
|
|
street |
|
|
|
Entry & JavaBeans
Jini uses JavaBeans to display entry objects to humans
You should have a JavaBean class for an Entry class that a human may interact with
JavaBean Class Design Pattern Let X be an entry class then:
Location |
LocationBean |
building
|
Entry
followLink()
|
floor
|
getBuilding()
|
room
|
getFloor()
|
|
getRoom()
|
|
makeLink(Entry
e)
|
|
setBuilding(String
x)
|
|
setFloor(String
x)
|
|
setRoom(String
x)
|
HelloClient Matching
The following code creates a simple template to use to find a service. The service id is set to null, so it is not used. Using the service ID would be faster, but we need to know it. We also set the serviceTypes to null. This means the template will match any serviceTypes. If we were looking for printers, we might set serviceTypes to be an array containing a printer type. Jini does not define these serviceTypes. They must be supplied by Jini developers. Only one entry template is given. Hence this service template will match any service that contains an entry of type "Name" or subtype of "Name" with the field "name" equal to the value "HelloServer".
Entry[] serverAttributes = new Entry[1]; serverAttributes[0] = new Name ("HelloServer"); ServiceTemplate template = new ServiceTemplate (null, null, serverAttributes); HelloInterface myServerInterface = (HelloInterface) registrar.lookup (template);
HelloServer
public class HelloServer extends UnicastRemoteObject implements HelloInterface, ServiceIDListener { private ServiceID myID; public void serviceIDNotify (ServiceID uniqueID) { myID = uniqueID; System.out.println("server: ID set: " + myID ); }
net.jini.core.lookup.ServiceID
Each service has a unique service id
HelloServer Main
HelloServer myServer = new HelloServer (); Entry[] identityingAttributes = new Entry[1]; identityingAttributes[0] = new Name("HelloServer"); JoinManager myManager = new JoinManager ( myServer, identityingAttributes, myServer, new LeaseRenewalManager () );
com.sun.jini.lookup.JoinManager
This class manages the join protocol for a service.
It discovers and keeps track of which lookup services to join, registers with them, keeps the registration leases renewed, and keeps the attributes up to date.
JoinManager(Object service, Entry[] attrSets,
ServiceIDListener callback, LeaseRenewalManager leaseMgr)
JoinManager Methods
addAttributes(Entry[] attrSets)
addAttributes(Entry[] attrSets, boolean checkSC)
addGroups(String[] groups)
addLocators(LookupLocator[] locators)
Entry[] getAttributes()
String[] getGroups()
ServiceRegistrar[] getJoinSet()