Make your own free website on Tripod.com

Developing A Simple Jini Service
Gopalan Suresh Raj

Note
To work with any of these samples, you will need the following:
.........................................The Java 2 compiler and API - JDK 1.2
.........................................
The Jini 1.x System Software

This simple Jini service returns the current time in milliseconds when it's getTime() method in invoked.

The Steps involved in developing a Jini Service are

1. Code the Jini Service Interface
2. Code the Jini Service
3. Compile the Code and generate stubs
4. Start up the Service

1. Code the Jini Service Interface

MyServerInterface.java
import java.rmi.*;

/***************************************************************************
 MyServerInterface -- The Server's Methods that are Available Remotely
 ***************************************************************************/

public interface MyServerInterface extends Remote {
 public long getTime() throws RemoteException;
}
 

2. Code the Jini Service

MyServer.java
import net.jini.core.entry.*;
import net.jini.core.lookup.*;
import net.jini.core.discovery.*;
import net.jini.lookup.entry.*;
import net.jini.discovery.*;
import com.sun.jini.lookup.*;
import com.sun.jini.lease.*;

import java.io.*;
import java.rmi.*;
import java.rmi.server.*;

/***************************************************************************

 MyServer -- The Jini Server Class

 ***************************************************************************/

public class MyServer extends UnicastRemoteObject
 implements MyServerInterface, ServiceIDListener, Serializable {

 public MyServer() throws RemoteException { super (); }
 

 public long getTime() throws RemoteException { // MyServiceInterface
  System.out.println ("====================================================");
  System.out.println ("MyServer: The method getTime() in MyServer was called");
  long serverTime = System.currentTimeMillis();
  System.out.println ("MyServer: Time at server is " + serverTime + " msecs");
  System.out.println ("====================================================");
  return(serverTime);
 }

 public void serviceIDNotify (ServiceID serviceId) { } // ServiceIDListener
 

 public static void main (String[] args) {
  int i;
  int iPort;
  String sHost;
  MyServer myServer;
  LookupLocator lookup;
  Entry[] aeAttributes;
  JoinManager joinmanager;
  ServiceID id;
  ServiceMatches matches;
  ServiceRegistrar registrar;

  try {
  
/*
   Setting the security manager to allow the RMI class loader
   to go to the codebase for classes that are not available
   locally.
   ========================================================== */

   System.setSecurityManager (new RMISecurityManager ());

   /*
   Create the attributes (an array of entry objects) that describe
   this server and use it to register this server with the lookup
   service. JoinManager finds and registers with the lookup
   service.
   =============================================================== */

   aeAttributes = new Entry[1];
   aeAttributes[0] = new Name("MyServer");

   myServer = new MyServer();
   joinmanager = new JoinManager(myServer,aeAttributes,myServer,new LeaseRenewalManager());
   System.out.println("MyServer: JoinManager = " + joinmanager);

   /*
   Find the Jini lookup service (reggie) and print its location.
   ============================================================= */

   lookup = new LookupLocator ("jini://localhost");
   sHost = lookup.getHost ();
   iPort = lookup.getPort ();

   System.out.println ("MyServer: LookupLocator = " + lookup);
   System.out.println ("MyServer: LookupLocator.host = " + sHost);
   System.out.println ("MyServer: LookupLocator.port = " + iPort);
 

   /*
   Get the lookup service's ServiceRegistrar (the class by which
   interaction with the lookup service is possible).
   ============================================================= */

   registrar = lookup.getRegistrar();
   id = registrar.getServiceID ();

   System.out.println ("MyServer: ServiceRegistrar = " + registrar);
   System.out.println ("MyServer: ServiceID = " + id);
 

   /*
   Itemize the service items and the service objects that are
   available in the lookup service. Note: Sometimes this is
   executed too quickly for the service that was registered above
   to show up.
   ============================================================== */

   try {Thread.sleep (2000);} catch (Exception e) {}

   matches = registrar.lookup(new ServiceTemplate (null, null, null),50);
  
System.out.println ("MyServer: ServiceMatches = " + matches);
   System.out.println ("MyServer: num matches = "+ matches.totalMatches);
   for (i = 0; i < matches.totalMatches; i++) {
    System.out.println ("MyServer: svc item " + i + ": " + matches.items[i]);
    System.out.println ("MyServer: svc object " + i + ": "+ matches.items[i].service);
   }
   System.out.println ("*-----------------------------------------*");
  } catch (Exception e) { System.out.println("MyServer: MyServer.main(): Exception " + e); }
 }
}


3. Compile the code and generate the RMI Stubs

H:\jini\TimeServer>
H:\jini\TimeServer>set CLASSPATH=.;G:\sun\jini\jini1_0\lib\jini-core.jar;G:\sun\jini\jini1_0\lib\jini-ext.jar;
G:\sun\jini\jini1_0\lib\mahalo.jar;G:\sun\jini\jini1_0\lib\mahalo-dl.jar;
G:\sun\jini\jini1_0\lib\reggie.jar;G:\sun\jini\jini1_0\lib\reggie-dl.jar;G:\sun\jini\jini1_0\lib\sun-util.jar;
G:\sun\jini\jini1_0\lib\tools.jar;%CLASSPATH%

H:\jini\TimeServer>javac *.java

H:\jini\TimeServer>rmic MyServer

H:\jini\TimeServer>

4. Startup the Jini Service

H:\jini\TimeServer>start java -jar G:\sun\jini\jini1_0\lib\tools.jar -port 8080 -dir G:\sun\jini\jini1_0\lib -verbose

H:\jini\TimeServer>start rmid

H:\jini\TimeServer>java -jar -Djava.security.policy=H:\jini\TimeServer\policy.all G:/sun/jini/jini1_0/lib/reggie.jar http://localhost:8080/reggie-dl.jar H:\jini\TimeServer\policy.all /reggie_log public
using absolute dbdir path: H:\reggie_log

H:\jini\TimeServer>java -Djava.security.policy=H:\jini\TimeServer\policy.all -Djava.rmi.server.codebase=http://localhost/ MyServer

MyServer: JoinManager = com.sun.jini.lookup.JoinManager@1bfdd4c7
MyServer: LookupLocator = jini://localhost/
MyServer: LookupLocator.host = localhost
MyServer: LookupLocator.port = 4160
MyServer: ServiceRegistrar = com.sun.jini.reggie.RegistrarProxy@2cd27317
MyServer: ServiceID = 88fc7d88-8ea6-4db9-9cac-ddeab6249ecc
MyServer: ServiceMatches = net.jini.core.lookup.ServiceMatches@5f01d4c7
MyServer: num matches = 4
MyServer: svc item 0: net.jini.core.lookup.ServiceItem@4b5d4c7
MyServer: svc object 0: MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3924](remote),objID:[4c91d33d:d5fe
eb8b2d:-8000, 0]]]]
MyServer: svc item 1: net.jini.core.lookup.ServiceItem@7f49d4c7
MyServer: svc object 1: com.sun.jini.reggie.RegistrarProxy@2cd27317
MyServer: svc item 2: net.jini.core.lookup.ServiceItem@70a1d4c7
MyServer: svc object 2: MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3859](remote),objID:[4c91d3c4:d5fe
e8b522:-8000, 0]]]]
MyServer: svc item 3: net.jini.core.lookup.ServiceItem@6d19d4c7
MyServer: svc object 3: MyServer_Stub[RemoteStub [ref: [endpoint:[130.151.69.105:3980](remote),objID:[0]]]]
*-----------------------------------------*
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919104970506 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105007119 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105010724 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105014360 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105017744 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105021370 msecs
====================================================
====================================================
MyServer: The method getTime() in MyServer was called
MyServer: Time at server is 919105024785 msecs
====================================================

 

click here to go to the
Developing a Simple Jini Client Page...
click here to go to
My Jini HomePage...

 


Go to the Component Engineering Cornucopia page

This site was developed and is maintained by Gopalan Suresh Raj

This page has been visited times since February 12,1999.

Last Updated : Feb 12, '99

If you have any questions, comments, or problems regarding this site, please write to me I would love to hear from you.


Copyright (c) 1997-99, Gopalan Suresh Raj - All rights reserved. Terms of use.

All products and companies mentioned at this site,are trademarks of their respective owners.