HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

JINI is a new Java-based API from Sun Microsystems. Devices that are JINI enabled should be inherently able to talk to each other and even exchange code. JINI allows for store-bought devices to just "plug and work." What I'm going to discuss here is what JINI looks like to a Java programmer.

JINI comes out of the same group as RMI (Remote Method Invocation). It apparently used to be an acronym, but somehow lost it along the way. So a sort of prerequisite to programming in JINI is knowing some Java and being familiar with RMI. That said, let's begin. In JINI-land every device offers a service that's placed in a lookup for other devices to retrieve those services. Here's an example service:

public interface PrinterService extends Remote {
public void print(Object obj) throws RemoteException;
}

public class PrinterServiceImpl extends UnicastRemoteObject implements
PrinterService {

public void print(Object obj) throws RemoteException {
/** Code here to print the object **/
}

}

The print method in this example is what other devices will call on the printer in order to print. So where's the JINI in all this? Glad you asked! We need to add a method to make the service register itself in the lookup (for the sake of this article we'll assume a lookup is running somewhere out there on the network). (See example below.)

public void register() {
try {
joinManager = new JoinManager(this,
entries,
new ServiceIDHandler(),
leaseManager);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

JoinManager is a helpful utility class for registering a service with the lookup and managing the leases for you. When I first did some programming in JINI for a "whisper suite" demo for JavaOne '98, I didn't have this helpful class. Mine grew to be quite large. So now I've introduced the concept of a lease, which is basically a time-out renewal process so the lookup can discard old services that could be sitting in the lookup but are off the network (i.e., printer unplugged). With the JoinManager and a LeaseRenewalManager one doesn't have to worry about leasing. Here's the API spec for the constructor I'm using:

public JoinManager(java.lang.Object obj,
Entry[] attrSets,
ServiceIDListener callback,
LeaseRenewalManager leaseMgr)
throws java.io.IOException

As you can see, I haven't explained what an Entry is not and what a ServiceID is. A ServiceID is a unique identifier for each service. A service can already have a unique identifier or it can obtain one from the lookup. For the sake of our example we're assuming the printer doesn't have a ServiceID (although in reality an Epson JINI printer would probably have shipped with a ServiceID in the ROM). (See example below.)

public class ServiceIDHandler implements ServiceIDListener {
public class ServiceIDHandler implements ServiceIDListener {
public void serviceIDNotify(ServiceID serviceID) {
/** Save the service ID somewhere **/
}
}
}

Now we need to find out what "Entry" means. An Entry is an interface signifying that the object implementing it is an Entry (it has no methods!). When an object says it's an Entry, it's saying that it's serializable and used in JINI as a set of attributes of a service. That's why, when we're registering our printer service, the JoinManager constructor wants an array of Entries. One entry that's pretty easy to use is Name. So let's use it!

Entry[] entries = new Entry[1];
entries[0] = new Name("Epson Printer Service");

We'd then pass this into the JoinManager. Assuming a lookup is on the network, we'd have registered our PrinterService remote interface in the lookup for communication with other services.

Let's discuss a few more high-level and interesting things about JINI. JINI is really a way of "federating" virtual machines on a network to work together, that is, JINI makes the network the computer. When most devices have JVMs, they all use the same instruction set. The network merely acts as the bus between two virtual microprocessors on a virtual motherboard. Also, JINI allows devices to distribute code among themselves. One practical example of this is the driver for the Epson printer. It can be downloaded by a JINI-enabled PC; all you have to do is plug in the printer and click print in the word processor and it will print - no installing drivers in Windows 95 or Windows NT Server, rebooting and all that stuff. Ultimately, JINI-enabled devices will allow for a new level of computing that is greatly simplified in comparison to the present state.

In a future article we'll build a JINI browser for browsing the contents of a lookup.

About the Author
Jason J. Rutherglen is CEO of the Silicon Valley startup RSDi, which provides distributed fault-tolerant e-commerce products and solutions on the various Java application servers. RSDi's flagship product is the Online E-Commerce Server. Jason can be reached at [email protected]

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.