In this lab, you will gain hands-on experience working with a GemFire client cache configuration.
-
Configuring a client cache XML file
-
Defining regions both for local private use as well as connecting to cache server
-
Configuring cache properties using gemfire.properties
-
Initializing the client cache in an application
Estimated completion time: 45 minutes
For this lab, we’ll use the server-bootstrap
module to start up a backend.
-
Start the server-side processes using the
server-bootstrap
start.sh
script (start.bat
for Windows) inside the foldercluster
:cd server-bootstrap/cluster ./start.sh
-
Next, run the gradle task 'seedRegions' to seed the regions with some data
cd server-bootstrap gradle seedRegions
The client’s cache.xml <client-cache>
declaration automatically configures it as a standalone Gemfire application. At this point, the locator and server will have already been started and the listener is listening on port 10334
. In this section, you will edit the clientCache.xml
file located in src/main/resources
to perform the following tasks.
-
(
TODO-01
) Define a pool configuration with one entry configured to contact the locator atlocalhost
and listening on port10334
-
(
TODO-02
) Define a region calledCustomer
as a proxy type region using either therefid
(region shortcut) or explicitly using region attributes. Similarly, create a region calledBook
but this time create it as a caching proxy type region.
In addition to providing configuration information in the cache xml file, you can also define properties using a gemfire.properties
file (also located in src/main/resources
). In this section, you will use this file to define the logging level that will be used by the client application. Consult the GemFire User Guide’s gemfire properties reference for a full list of properties, and to find out the list of valid values for this property.
(TODO-03
) Open the gemfire.properties
file and set the log level initially to warning
.
Open the ClientCacheTests.java
file in the io.pivotal.bookshop.tests
package (under src/test/java
) and add code for the following methods.
⇒ Run the tests. All four unit tests should fail.
Inspect the code. Clearly the client is not yet configured.
-
(
TODO-04
) In thesetup()
method to, add code to create aClientCache
. Use the appropriate method on theClientCacheFactory
class to set thecache-xml-file
property toclientCache.xml
. -
(
TODO-05
) Also, add code to the samesetup()
method to get theCustomer
region and assign to the class private fieldcustomers
. Add a similar statement to get theBook
region and assign to the private fieldbooks
.NoteYou will not edit the test code itself, which performs the get operations. Details on how to make such calls to the region will be covered in the next section. -
(
TODO-06
) Re-run the tests. The testsregionsShouldExist
,customersShouldExist
, andbooksShouldExist
should now pass. -
As an optional step, go back and edit the
gemfire.properties
file and change the log level tofine
. Re-run the application and notice the additional detail that is output when running your client application.
In this final section, you will define a local region and run a test to load and fetch data from it.
-
(
TODO-07
) Open theclientCache.xml
file and add an additional region calledLocalRegion
. Use the region shortcuts to define this as a local region. -
(
TODO-08
) Return to theClientCacheTests
class. Update the setup method to obtain a reference to the local region and assign it to the field variable localRegion. -
(
TODO-09
) Finally re-run the tests and verify that all tests pass.
To begin, open the BookDao.java
class in the io.pivotal.bookshop.buslogic
package in the IDE. The purpose of this class is to hide the basic operations involved in creating, reading, updating and deleting entries (the CRUD operations).
Note that the region is defined to store Book
objects with an Long
key as illustrated by the private field defined at the top of the DAO. The constructor is responsible for initializing the region using the ClientCache
that is passed in. You will implement the basic functionality of this class in the following steps.
-
(
TODO-10
) Before implementing the functionality inBookDao
, open the corresponding JUnit test class (DataOperationsTests.java
in theio.pivotal.bookshop.tests
package insrc/test/java
). Take a moment to inspect the various test methods to see how the DAO is used. -
(
TODO-11
) Implement theinsertBook()
method. Use the key and book object to insert into the region.TipRecall that there are different methods that can be used to insert an entry. Use the method that enforces that the entry can’t already exist. -
(
TODO-12
) Implement thegetBook()
method. Use the supplied key to retrieve the associated entry from the region and return it. -
(
TODO-13
) Implement theupdateBook()
method. Use the supplied key andBook
object to update the existing entry in the region. -
(
TODO-14
) Implement theremoveBook()
method. Use the supplied key to delete the entry from the region. Recall that there are two methods to do this. Either one is acceptable. -
(
TODO-15
) When you have completed all the steps above, run the test suite (DataOperationsTests
). Make sure the GemFire server process (locator and server) are already running if you haven’t done so already.
Congratulations!! You have completed this lab.