Products
Clients
Extensions
Before starting the following example make sure you have GemFire installed.
To use a Java client with GemFire, you must add the dependencies that are appropriate for your application. The GemFire dependencies are available from the Pivotal Commercial Maven Repo. Access to the Pivotal Commercial Maven Repository requires a one-time registration step to create an account.
To add GemFire to a Maven or Gradle project
In a browser, navigate to the Pivotal Commercial Maven Repository.
Click the Create Account link.
Complete the information in the registration page and accept the EULA.
Click Register.
After registering, you will receive a confirmation email. Follow the instruction in this email to activate your account.
Add the GemFire repository to your project:
pom.xml
<repositories> <repository> <id>gemfire-release-repo</id> <name>Pivotal GemFire Release Repository</name> <url>https://commercial-repo.pivotal.io/data3/gemfire-release-repo/gemfire</url> </repository> </repositories>
build.gradle
repositories { maven { credentials { username "$gemfireRepoUsername" password "$gemfireRepoPassword" } url = uri("https://commercial-repo.pivotal.io/data3/gemfire-release-repo/gemfire") } }
Add your Pivotal Commercial Maven Repository credentials.
.m2/settings.xml
MY-USERNAME@example
MY-DECRYPTED-PASSWORD
<settings> <servers> <server> <id>gemfire-release-repo</id> <username>[email protected]</username> <password>MY-DECRYPTED-PASSWORD</password> </server> </servers> </settings>
.gradle/gradle.properties
gradle.properties
gemfireRepoUsername=MY-USERNAME@example.com gemfireRepoPassword=MY-DECRYPTED-PASSWORD
Add the dependencies to the project.
<dependencies> <dependency> <groupId>com.vmware.gemfire</groupId> <artifactId>gemfire-core</artifactId> <version>VERSION</version> </dependency> </dependencies>
dependencies { implementation "com.vmware.gemfire:gemfire-core:$VERSION" }
For the following client examples, start a simple cluster and create an example region.
$ gfsh
start locator
start server
create region --name=helloWorld --type=PARTITION
The following is an example of connecting to the GemFire cluster started above.
This example
ClientCache
helloWorld
import org.apache.geode.cache.Region; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCacheFactory; import org.apache.geode.cache.client.ClientRegionShortcut; public class HelloWorldApplication { public static void main(String[] args) { ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334).create(); Region<String, String> helloWorldRegion = cache.<String, String>createClientRegionFactory(ClientRegionShortcut.PROXY).create("helloWorld"); helloWorldRegion.put("1", "HelloWorldValue"); String value1 = helloWorldRegion.get("1"); System.out.println(value1); cache.close(); } }
Build and run the application. You should see ‘HelloWorldValue’ printed in the command line.