To insert, delete, update, and retrieve data from your
data grid, you must write a client application. The getting started
sample includes a Java client application
that you can use to learn about creating your own client application.
About this task
The Client.java file in the wxs_install_root/ObjectGrid/gettingstarted/client/src/ directory
is the client program that demonstrates how to connect to a catalog
server, obtain the ObjectGrid instance, and use the ObjectMap API.
The ObjectMap API stores data as key-value pairs
and is ideal for caching objects that have no relationships involved.
The following steps discuss the contents of the Client.java file.
If you need to cache objects that have relationships,
use the EntityManager API.
Procedure
- Connect to the catalog service by obtaining a ClientClusterContext
instance.
To connect to the catalog server, use the connect method
of ObjectGridManager API. The
following code snippet demonstrates how to connect to a catalog server and obtain a
ClientClusterContext
instance:
ClientClusterContext ccc = ObjectGridManagerFactory.getObjectGridManager().connect(cep, null, null);
If the connections to the catalog servers succeed, the
connect method returns a ClientClusterContext instance. The ClientClusterContext
instance is required to obtain the ObjectGrid from the
ObjectGridManager API.
- Obtain an ObjectGrid instance.
To obtain ObjectGrid instance, use the getObjectGrid method of the
ObjectGridManager API. The getObjectGrid method requires both
the ClientClusterContext instance and the name of the data grid instance. The ClientClusterContext
instance is obtained during the connection to catalog server. The name of
ObjectGrid instance is Grid that is specified in the
objectgrid.xml file. The
following code snippet demonstrates how to obtain the data grid by calling the
getObjectGrid method of the ObjectGridManager API.
ObjectGrid grid = ObjectGridManagerFactory.getObjectGridManager().getObjectGrid(ccc, "Grid");
- Get a Session instance.
You can get a Session from the obtained ObjectGrid instance. A Session instance is required to
get the ObjectMap instance, and perform transaction demarcation. The following code snippet
demonstrates how to get a Session instance by calling the getSession method of
the ObjectGrid API.
Session sess = grid.getSession();
- Get an ObjectMap instance.
After getting a Session, you can get an ObjectMap instance from a Session instance by calling
getMap method of the Session API. You must pass the name of
map as parameter to getMap method to get the ObjectMap
instance. The following code snippet demonstrates how to obtain ObjectMap by calling the
getMap method of the Session API.
ObjectMap map1 = sess.getMap(mapName);
- Use the ObjectMap methods.
After an ObjectMap instance is obtained, you can use the ObjectMap API.
Remember that the ObjectMap interface is a transactional map and requires transaction demarcation by
using the begin and commit methods of the
Session API. If there is no explicit transaction demarcation in the application,
the ObjectMap operations run with auto-commit transactions.
-
The following code snippet demonstrates how to use the ObjectMap API with an
auto-commit transaction.
map1.insert(key1, value1);
- You can either run a transaction on one partition at a time, or on
multiple partitions. To run a transaction on a single partition, use a one-phase commit transaction:
sess.setTxCommitProtocol(TxCommitProtocol.ONEPHASE);
sess.begin();
map1.insert(k, v);
sess.commit();
To
run a transaction across multiple partitions, use a two-phase commit transaction:
sess.setTxCommitProtocol(TxCommitProtocol.TWOPHASE);
sess.begin();
map1.insert(k, v);
sess.commit();
- Optional: Close the Session.
After all of the Session and ObjectMap operations are complete, close the session with the
Session.close() method. Running this method returns the resources that were being
used by the session.
sess.close();
As a result,
subsequent getSession() method calls return faster, and fewer Session objects are
in the heap.