jPOS

miniSpace - 5 minutes tutorial


A minimalist coordination mechanism

miniSpace is a general-purpose coordination mechanism inspired after the Linda Coordination Language and JavaSpaces. It is by no means an attempt to implement any of those, we just implemented the minimum set of concepts we needed in our own projects.

You can think about a Space instance as being like a Map where its entries are lists of objects and its operations are fully synchronized.

There are three basic operations:

In addition to those three basic operations it also has a few handy methods:

Hello miniSpace


Let's have a look at a simple example, we will put two Strings in the Space under the key name "Test" (we call them 'queues') by using the out operation. The first string equals to "Hello ", and the second one equals to "World".

Then we will take and print one entry at a time off the "Test" queue by using the in operation.

Finally we will read (rd) that queue using the rdp (read-probe) operation. It should be null (because there were just two entries under the name 'Test' and we have already taken them off the Space).


  import org.jpos.space.*;

  Space sp = SpaceFactory.getSpace(); // get a reference to the default Space
  sp.out ("Test", "Hello ");          // put "Hello " in the "Test" queue
  sp.out ("Test", "World");           // put "World" in the "Test" queue
  System.out.print (sp.in ("Test"));  // should return the first entry
  System.out.print (sp.in ("Test"));  // should return the second entry
  if (sp.rdp ("Test") == null)        // should be null
    System.out.println (".");

  --- output ---
  Hello World.

 

We encourage you to play with the previous code and we suggest you perform some of the following variations in order to understand how it works:

Some use cases

The persistent Space

When you call SpaceFactory.getSpace() you are actually getting an instance of TSpace. That Space gets registered under the name tspace:default. So calling SpaceFactory.getSpace() is exactly the same as calling SpaceFactory.getSpace("tspace:default").

You can create as many Spaces as you want, and you can make them anonymous if you go directly to the Space constructur instead of using the SpaceFactory helper class.

There's a very useful space implementation called JDBMSpace. It is a persistent space implementation that uses a JDBM database as its back end. The result is that everything you write there will remain valid across JVM runs.

You can use a JDBMSpace by calling:


  Space sp = SpaceFactory ("jdbm:mypersistentspace");

  
You can also specify an optional path for your JDBM database:

  Space sp = SpaceFactory ("jdbm:mypersistentspace:/tmp/persistentspace");

  


What's next ?

You can get additional information in the project page at Google Code.