< Previous | Next >

Getting started tutorial lesson 2.1: Creating a client application

To insert, delete, update, and retrieve data from your data grid, you must write a client application. The getting started sample includes a client application that you can use to learn about creating your own client application.

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.

  1. Connect to the catalog service by obtaining a ClientClusterContext instance.

    To connect to the catalog server, use the connect method of ObjectGridManager API. The connect method that is used requires only the catalog server endpoint in the format of hostname:port. You can indicate multiple catalog server endpoints by separating the list of hostname:port values with commas. The following code snippet demonstrates how to connect to a catalog server and obtain a ClientClusterContext instance:

    ClientClusterContext ccc = ObjectGridManagerFactory.getObjectGridManager().connect("localhost:2809", 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.
  2. 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");
  3. 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();
  4. 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("Map1");
  5. 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(k, v);
    • The following code snippet demonstrates how to use the ObjectMap API with explicit transaction demarcation.

      sess.begin();
      map1.insert(key1, value1);
      sess.commit();
  6. 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.

Lesson checkpoint

In this lesson, you learned how to create a simple client application for performing data grid operations.
< Previous | Next >