You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@river.apache.org by co...@apache.org on 2010/08/27 16:53:00 UTC

[CONF] Apache River > User Guide - Basic River Services

Space: Apache River (https://cwiki.apache.org/confluence/display/RIVER)
Page: User Guide - Basic River Services (https://cwiki.apache.org/confluence/display/RIVER/User+Guide+-+Basic+River+Services)

Added by Tom Hobbs:
---------------------------------------------------------------------
h1. Getting Started With River

This document is going to explain how you can use the inbuilt "simple" services that come with River.  More specifically;

# A Lookup Service (the "reggie" implementation)
# A Java Space (the "outrigger" implementation)
# A Transaction Service (the "mahalo" implementation)

The instructions assume that you're building from source as checked out from the SVN trunk.  Currently this is necessary because the code snippets below use methods and classes which, at time of writing, haven't made it into the latest binary release yet.  Having said that, the code you will need in the binary release isn't to far removed from what you'll see below, so you can progress with the binary release if you want to and are happy odifying the code.

If you are going to use the latest release then you will still need to download (from the SVN trunk) some of the Windows BAT files mentioned below.  *nix sh scripts of those same batch files will appear shortly.

h2. Environment Setup
The following instructions all assume that you have checked out River to some directory which is throughout referred to as {{$RIVER_HOME}}.  Further, you have changed directory into {{$RIVER_HOME/examples/hello}}.

*IMPORTANT:* Run all scripts from the {{hello}} directory
# Change directory to $RIVER_HOME
# Use Ant to build River; I.e. {{ant all.build}}

h2. Running Code Which Uses River Services
Since River uses dynamic code downloading, we need to grant various permissions to our JVM to allow that.  In the interest of simplicity, we are going to grant everything every permission.  In the real world, this would obviously not be recommended.

To grant these permissions we need to create a policy file:

{code:title=policy.all|borderStyle=solid}
grant { 
	permission java.security.AllPermission; 
}; 
{code}

Create the above file and save it somewhere that it can be easily referenced as a command line argument to some Java you're going to write - such as the working directory that you will run the code from.

Further, your programs which will use River services will need the following JVM arguments.

{code:title=JVM Arguments|borderStyle=solid}
-Djava.security.policy=path/to/policy.all
-Djava.rmi.server.RMIClassLoaderSpi=net.jini.loader.pref.PreferredClassProvider
{code}

Also, before doing anything with any River code you need to set an appropriate security manager.  This needs to be done only once per program.

{code:title=Setting the Security Manager|borderStyle=solid}
System.setSecurityManager(new RMISecurityManager());
{code}

h2. Starting the HTTP server

h3. What?  Wait, why do I need to do that?
When services get marshalled for sending over the wire, the first part of their stream is the codebase URL.  This URL tells the downloading JVM where to load the supporting JARs from.  Often (although not always) in the River/Jini world this code base harks back to some HTTP server.  There is a simple HTPP server packaged inside River which can be used for this.  That's what we're going to start.

See [3.2 How codebase is used in Java RMI|http://download.oracle.com/javase/1.5.0/docs/guide/rmi/codebase.html] for more details.

h3. Instructions
# Change to directory to {{$RIVER_HOME/examples/hello/}}
# Execute the script; {{scripts/httpd.bat}}

h3. Testing it
To test that the HTTP server is running correctly use wget or your web browser on the following URL

- http://localhost:8080/reggie-dl.jar

h2. Starting a Service Registrar (Lookup Service)

h3. What?  Wait, why do I need to do that?
Typically, in an environment which uses River/Jini services, a Lookup Service will be used.  This Lookup Service is then used by your code to find River/Jini services.  Lookup Services always know the current state of the subnet or federated space.

Broadly speaking (although not 100% accurately) you need a Lookup Service in order to be able to find other services to use.

h2. Instructions

# Change directory to $RIVER_HOME/examples/hello
# Execute the script {{scripts/jrmp-reggie.bat}}

h3. Testing it
There are two ways to find our Lookup Service.  Remember that the interface which fulfills the role of a Lookup Service is {{ServiceRegistrar}}.

h4. Unicast
If we know where our Lookup Service is running, we can use unicast to connect straight to it.

{code:title=Unicast|borderStyle=solid}
LookupLocator ll = new LookupLocator("jini://localhost:4160");
StreamServiceRegistrar sr = ll.getStreamRegistrar();
System.out.println("Service Registrar: "+sr.getServiceID());
{code}

Assuming you don't get a {{NullPointerException}} and you do get a service ID written out, then your Lookup Service is running fine.

The {{LookupLocator}} takes a String representing a "jini URL".  This jini URL is made up from the Strings "{{jini://}}" a hostname and (optionally) "{{:}}" and a port number.  4160 is the default port and is specified in the Lookup Service configuration file.  The scripts in the {{examples/hello}} directory will use the default port unless you have changed it.  See [DJ.5.5 Address and Port Mappings for TCP and Multicast UDP|http://incubator.apache.org/river/doc/specs/html/discovery-spec.html] for more details.

h4. Multicast
If we know only that "some lookup services are on the subnet somewhere" then we can use multicast to find them.

{code:title=Multicast|borderStyle=solid}
DiscoveryListenerManagement dlm = new LookupDiscovery(LookupDiscovery.ALL_GROUPS);

LeaseRenewalManager lrm = new LeaseRenewalManager();
ServiceDiscoveryManager sdm = new ServiceDiscoveryManager(dlm, lrm);

Thread.sleep(500); //need to wait a little bit for the Lookup Service to generate the events to the sdm

ServiceTemplate srTemplate = new ServiceTemplate(null, new Class[] { ServiceRegistrar.class }, null);

ServiceItem[] sis = sdm.lookup(srTemplate, 10, null);
for(ServiceItem si : sis) {
    System.out.println("Service Registrar: "+si.serviceID);
}

dlm.terminate();
{code}

This approach is slightly different.  It is relying the {{ServiceDiscoveryManager}} to listen to the UDP multicast packets announcing the presence of a Lookup Service.  The same rules apply as for unicast on deciding whether or not your Lookup Service is running.

This approach can be modified if you know which hosts on the subnet _might_ be running lookup services.  You can define and pass this list to the {{LookupDiscovery}} constructor.

h2. Starting a Java Space

h3. What?  Wait, why do I need to do that?
Finding a Lookup Service is only useful if there are some other services on the network which you want to use, so now we're going to start a Java Space, and find that.  Using the Java Space is outside the scope of this document, although the "how" is relativily straight forward.

h3. Instructions
# Change directory to $RIVER_HOME/examples/hello
# Execute the script {{scripts/jrmp-outrigger-group.bat}}

h3. Testing it
{code:title=Finding A Javaspace with a ServiceRegistrar|borderStyle=solid}
ServiceTemplate template = new ServiceTemplate(null, new Class[] { JavaSpace.class }, new Entry[0]);

ServiceMatches sms = sr.lookup(template, 10);
for(ServiceItem si : sms.items) {
    System.out.println("Found: "+si);
}
{code}

{code:title=Finding A Javaspace with a ServiceDiscoveryManager|borderStyle=solid}
ServiceItem si = sdm.lookup(template, null);
System.out.println("Java Space: "+(JavaSpace)si.service);
{code}

As before, a lack of {{NullPointerException}} and not having {{null}} printed out will indicate that you have found a running Java Space.

h2. Starting a Transaction Service

h3. What?  Wait, why do I need to do that?
Because you'd like to find something that will give you a nice distributed transaction.

h3. Instructions
# Change directory to $RIVER_HOME/examples/hello
# Execute the script {{scripts/jrmp-mahalo-group.bat}}

h3. Testing it
Testing it is done in the same way as for the Java Space.  However, the constructor of the {{ServiceTemplate}} changes.

{code:title=Transaction Manager ServiceTemplate|borderStyle=solid}
ServiceTemplate template = new ServiceTemplate(null, new Class[] { TransactionManager.class }, new Entry[0]);
{code}



Change your notification preferences: https://cwiki.apache.org/confluence/users/viewnotifications.action