Pages

19/04/2014

[TIBCO ActiveSpaces] Implement shared_all persistence

TIBCO ActiveSpaces supports persistence to a DB of your choice when spaces are configured in shared_all persistence mode.

By implementing the Persister interface in Java, C or .NET, you are able to create a persistence client that connects to your DB and joins the metaspace, receives callbacks on AS events and handles persistence to and from the DB.

You can download a code sample in Java (DISCLAIMER: NOT PRODUCTION READY, just a sample!) showing you how to create the persistence client to an Oracle DB.

To set it up correctly you'll have to:

  • change the DB connection parameters in ASPersister class
  • ONLY THE FIRST TIME - create DB tables with the structure as the spaces you want to persist (table name = space name, column name = field name, same data types)
For our example, run this:


CREATE TABLE shared_all_persisted(
    mykey INTEGER,
    myvalue VARCHAR2(2000 BYTE),
    mytime TIMESTAMP, 
    PRIMARY KEY (mykey)
)

  • start as-agent from AS_INSTALL_DIR/bin without additional parameters
  • start the persistence client (can be run from Eclipse's debug mode too)
When the client starts, you will see it join the metaspace as a LEECH, and it will create a sample shared_all_persisted space for you if it doesn't exist already.

Now put some tuples in the space and see them persisted on the DB. 

NOTE: remember that persistence starts working when the MINIMUM_SEEDER_COUNT is reached. In our example that parameter is set as the minimum allowed of 1, meaning that running the as-agent already satisfies the condition.

Now stop the persistence client and as-agent, then start as-agent again. From as-admin you can connect to the metaspace and check that it's empty.

Start the persistence client and watch it load the data back in AS from the DB.

You can put data in AS from Java as:


Tuple tuple = Tuple.create();
tuple.put("mykey", 1);
tuple.put("mytime", DateTime.create());
tuple.put("myvalue", "Something");
space.put(tuple);


IMPORTANT NOTE: to successfully run the sample code, you'll also need to get your own copy of the as-common.jar library and put it under the project's lib folder. The jar itself is located under your AS_INSTALL_DIR/lib folder.

2 comments:

With great power comes great responsibility