[Java programming language only]

Getting started tutorial lesson 2.1: Creating a [Version 8.6 and later]Java 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 [Version 8.6 and later]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

  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 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.
  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(mapName);
  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(key1, value1);
    • [Version 8.6 and later] 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();
  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.
Related conceptsCaching objects with no relationships involved (ObjectMap API)ObjectMaps are like Java Maps that allow data to be stored as key-value pairs. ObjectMaps provide a simple and intuitive approach for the application to store data. An ObjectMap is ideal for caching objects that have no relationships involved. Related tasksGetting started with developing applicationsTo begin developing WebSphere eXtreme Scale applications, you must set up your development environment, learn about APIs that you can use, then develop and test your application.Tutorial: Storing order information in entitiesThe tutorial for the entity manager shows you how to use WebSphere eXtreme Scale to store order information on a Web site. You can create a simple Java Platform, Standard Edition 5 application that uses an in-memory, local data grid. The entities use Java SE 5 annotations and generics.Related informationAPI documentation

Lesson checkpoint

In this lesson, you learned how to create a simple client application for performing data grid operations.