Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved


Chapter 5 Using the Example Code : ASPersistence

ASPersistence
The ASPersistence example demonstrates how to implement the external shared-all persistence interface.
Overview
With shared-all persistence, all nodes in a space persist their data into a single data store. To use shared-all persistence with ActiveSpaces, you must implement the Persister interface. The implemented persister is then used to store and retrieve data from the data store.
The ASPersistence example provides two example implementations of the Persister interface.
The first implementation, SimplePersister, uses an in-memory HashMap as the data store.
The second implementation, ASPersister, uses a database as the data store.
The onWrite method of the persister is invoked when data is put into the space.
The onRead method of the persister is invoked when data is read from the data store back into the space.To see data being read back out of the data store, the space needs to be defined with a capacity so that when the capacity is exceeded, the least recently used data in the space is ejected from the space. When data that has been previously ejected from the space is read back into the space, you will see the onRead method of the persister invoked.
The ASPersistence examples does not provide the means to read or write data into the space. Therefore, you should run the ASOperations example program along with the ASPersistence example.
Starting ASPersistence
The following examples show how to invoke ASPersistence for each of the API sets:
Java Invocation
java ASPersistence -metaspace examplems -member_name sharedall -capacity 2 -eviction_policy lru
C Invocation
ASPersistence -metaspace examplems -member_name sharedall -capacity 2 -eviction_policy lru
.NET Invocation
AS_HOME\examples\dotnet\ASPersistence.exe -metaspace examplems -member_name sharedall -capacity 2 -eviction_policy lru
ASPersistence does not use the default space but uses a basic space definition and sets the persistence type of the space to 'shared-all'. ASPersistence uses a default space name of shared_all_persisted to identify this slightly different space so that you will not get a space definition mismatch with the other examples which may already be running.
The following examples indicate how to invoke ASOperations to work in conjunction with the ASPersistence example so that you can see the various methods of the persister being invoked.
Java Invocation for ASOperations with ASPersistence
java ASOperations -metaspace examplems -role seeder -persistence shared_all -capacity 2 -eviction_policy lru
C Invocation for ASOperations with ASPersistence
ASOperations -metaspace examplems -role seeder -persistence shared_all -capacity 2 -eviction_policy lru
.NET Invocation
AS_HOME\examples\dotnet\ASOperations.exe -metaspace examplems -space shared_all_persisted -persistence share_all -capacity 2 -eviction_policy lru
Starting ASPersistence With Security
The following example shows the command line options that you can be use when starting ASPersistence to have it join the security domain exdomain:
-discovery tcp://127.0.0.1:50000 -member_name sharedall -capacity 2 -eviction_policy lru -security_token exdomain_token.txt
To invoke ASOperations to work in conjunction with the ASPersistence example when it has been started with security, use the following command line options:
-discovery tcp://127.0.0.1:50000 -role seeder -persistence shared_all -capacity 2 -eviction_policy lru -security_token exdomain_token.txt
These command line options start ASPersistence and ASOperations using the default metaspace name ms and allow them to connect to a security domain controller that has been started using the example security policy file example_policy.txt. Using ASPersistence
Because encrypted fields cannot be persisted with shared all persistence, you should not try to use the -encrypt_field command line option when starting ASPersistence or ASOperations for this scenario.
Use ASOperations to put the following data into the space:
1, any
   2, bat
   3, cat
Each time a put is done using ASOperations, you will see the onWrite method of the Persister interface invoked in the command window where the ASPersistence example was started.
Since we have defined the space to have a capacity of 2 but we have put three entries into the space, the first entry will be evicted from the space. To verify this, enter br at the command prompt displayed by the ASOperations example and press Enter when prompted for a filter. The last two entries put into the space should be displayed.
Now use ASOperations to get the entry with an index of 1 back into the space. Enter g for get in the command window of ASOperations. Enter 1 for the key and press Enter. You will see the onRead method of the Persister interface invoked in the ASPersistence command window.
Enter quit in the command window of the ASPersistence example to shut down the application. The onClose method of the Persister interface is invoked when ASPersistence is stopped.
Using ASPersistence with a Database
You can use the ASPersistence example with a database for the data store. The default database supported in the code is PostgreSQL. The class MySQLConnection is also provided to show you how easy it is to build on the current ASPersistence example to support a different database.
Complete these steps to use the ASPersistence example with a database:
1.
2.
3.
Modify your CLASSPATH to include the JDBC driver jar file for your database
4.
Modify the file ASPersistence.java as follows:
a.
//import persistence.ASPersister;
b.
import persistence.SimplePersister;
c.
Comment out the following lines in the ASPersistence constructor:
persister = space.setPersister(new SimplePersister(spaceDef));
d.
//persister = space.setPersister(new ASPersister(metaspace));
5.
Modify the file ASPersister.java as follows:
a.
Ensure that the values for DB_NAME, DB_HOST, USER_NAME, and USER_PASS are set appropriately for your database.
b.
Ensure that JDBC_DRIVER and JDBC_URL are set appropriately for your database.
c.
In the onOpen method, ensure that the appropriate database connection class is being instantiated.
6.
Rebuild Examples.jar.
7.
Invoke the ASPersistence and ASOperations examples as described in Starting ASPersistence, and ensure that your CLASSPATH has been set appropriately as described in Step 3.

Copyright © TIBCO Software Inc. All Rights Reserved
Copyright © TIBCO Software Inc. All Rights Reserved