RMI Interface

From RifidiWiki

Revision as of 19:09, 18 May 2009 by Kyle (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The Rifidi Edge Server exposes its functionality to clients via Remote Method Invocation (RMI). From the RMI homepage:

Java Remote Method Invocation (Java RMI) enables the programmer to create distributed Java technology-based to Java technology-based applications, in which the methods of remote Java objects can be invoked from other Java virtual machines*, possibly on different hosts. RMI uses object serialization to marshal and unmarshal parameters and does not truncate types, supporting true object-oriented polymorphism.

Available RMI Stubs

RMI stubs are objects that the server creates to handle remote method calls. The Rifidi Edge Server creates three stubs when it starts up, registers them under a given name in the RMI Registry, and registers the objects as services in the OSGi registry.

EdgeServerStub

This stub handles global Edge server functionality, such as saving configurations. It is registered in the RMI registry under the name EdgeServerStub. For more information see <link to Javadoc>

ReaderStub

This stub handles all calls related to Readers, such as creating and deleting readers, changing reader properties, and managing session lifecycle. It is registered in the RMI registry under the name ReaderStub. For more information see <link to Javadoc>

CommandStub

This stub handles all calls related to commands, such as creating and deleting CommandConfigurations and changing CommandConfiguration properties. It is registered in the RMI registry under the name CommandStub. For more information see <link to Javadoc>

Creating a Client

Using native RMI

This example shows how to use RMI to connect to the Edge Server and make a call on a stub. It uses RMI directly from java and does no stub cacheing.

import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import org.rifidi.edge.core.api.rmi.EdgeServerStub;

/**
 * An example Edge Server RMI Client
 * 
 * @author Kyle Neumeier - kyle@pramari.com
 * 
 */
public class RMITest {

	/**
	 * Main method. Looks up stub and makes a call
	 * 
	 * @param args
	 *            - none
	 */
	public static void main(String[] args) {
		try {
			// obtain a reference to the RMI registry
			Registry registry = LocateRegistry.getRegistry("127.0.0.1", 1098);
			// look up the stub on the regsitry
			Remote remoteObject = registry.lookup("EdgeServerStub");
			if (remoteObject instanceof EdgeServerStub) {
				// if the stub is the correct type, make the call
				EdgeServerStub stub = (EdgeServerStub) remoteObject;
				System.out.println(stub.getStartupTime());
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
	}
}

Using Rifidi RMI Utils

This example uses the org.rifidi.edge.core.rmi.client and org.rifidi.rmi.util, which provide Command Objects around each available RMI call in the Edge Server's API. This makes working with the API easier and enables stubs to be cached on the client side for performance reasons. It is the recommended way of working with the Edge Server.

import org.rifidi.edge.core.rmi.client.edgeserverstub.ESGetStartupTimestamp;
import org.rifidi.edge.core.rmi.client.edgeserverstub.ESServerDescription;
import org.rifidi.rmi.utils.exceptions.ServerUnavailable;

/**
 * Demonstrate how to make an RMI call to the Edge Server using the Command
 * wrapper around RMI calls
 * 
 * @author Kyle Neumeier - kyle@pramari.com
 * 
 */
public class RMITestCommandObjects {

	/**
	 * @param args
	 *            - none
	 */
	public static void main(String[] args) {
		// create a new server description object
		ESServerDescription serverDescription = new ESServerDescription(
				"127.0.0.1", 1098);
		// create a new command object
		ESGetStartupTimestamp getStartupTimeCall = new ESGetStartupTimestamp(
				serverDescription);
		try {
			// make the RMI call
			System.out.println(getStartupTimeCall.makeCall());
		} catch (ServerUnavailable e) {
			e.printStackTrace();
		}
	}
}

For more information about the Command Objects, RMI, and Stub Cacheing, see

Personal tools