Pour pouvoir utiliser le reste du tutoriel, vous devez créer et empaqueter un programme Java simple et deux fichiers XML. Ces fichiers définissent une configuration ObjectGrid simple avec une instance ObjectGrid accounting et une mappe customer. Le fichier SimpleDP.xml comporte une règle de déploiement d'un groupe de mappes configuré avec une partition sans répliques requises minimales.
SimpleApp.java
// Cet exemple de programme est fourni TEL QUEL et peut être utilisé, exécuté, copié et modifié
// gratuitement par le client
// (a) à des fins d'études,
// (b) afin de développer des applications conçues pour être exécutées avec un produit IBM WebSphere,
// soit pour un usage interne soit pour une redistribution par le client, en tant que partie de
// l'application, au sein des produits du client.
// Eléments sous licence - Propriété d'IBM
// 5724-J34 (C) COPYRIGHT International Business Machines Corp. 2007-2009
package com.ibm.websphere.objectgrid.security.sample.guide;
import com.ibm.websphere.objectgrid.ClientClusterContext;
import com.ibm.websphere.objectgrid.ObjectGrid;
import com.ibm.websphere.objectgrid.ObjectGridManager;
import com.ibm.websphere.objectgrid.ObjectGridManagerFactory;
import com.ibm.websphere.objectgrid.ObjectMap;
import com.ibm.websphere.objectgrid.Session;
public class SimpleApp {
public static void main(String[] args) throws Exception {
SimpleApp app = new SimpleApp();
app.run(args);
}
/**
* read and write the map
* @throws Exception
*/
protected void run(String[] args) throws Exception {
ObjectGrid og = getObjectGrid(args);
Session session = og.getSession();
ObjectMap customerMap = session.getMap("customer");
String customer = (String) customerMap.get("0001");
if (customer == null) {
customerMap.insert("0001", "fName lName");
} else {
customerMap.update("0001", "fName lName");
}
customer = (String) customerMap.get("0001");
// Fermer la session (facultatif dans les versions 7.1.1 et ultérieures) pour améliorer les performances
session.close();
System.out.println("The customer name for ID 0001 is " + customer);
}
/**
* Get the ObjectGrid
* @return an ObjectGrid instance
* @throws Exception
*/
protected ObjectGrid getObjectGrid(String[] args) throws Exception {
ObjectGridManager ogManager = ObjectGridManagerFactory.getObjectGridManager();
// Create an ObjectGrid
ClientClusterContext ccContext = ogManager.connect("localhost:2809", null, null);
ObjectGrid og = ogManager.getObjectGrid(ccContext, "accounting");
return og;
}
}
SimpleApp.xml
<?xml version="1.0" encoding="UTF-8"?>
<objectGridConfig xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/ws/objectgrid/config ../objectGrid.xsd"
xmlns="http://ibm.com/ws/objectgrid/config">
<objectGrids>
<objectGrid name="accounting">
<backingMap name="customer" readOnly="false" copyKey="true"/>
</objectGrid>
</objectGrids>
</objectGridConfig>
Le fichier XML suivant permet de configurer l'environnement de déploiement.
SimpleDP.xml
<?xml version="1.0" encoding="UTF-8"?>
<deploymentPolicy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://ibm.com/ws/objectgrid/deploymentPolicy ../deploymentPolicy.xsd"
xmlns="http://ibm.com/ws/objectgrid/deploymentPolicy">
<objectgridDeployment objectgridName="accounting">
<mapSet name="mapSet1" numberOfPartitions="1" minSyncReplicas="0" maxSyncReplicas="2" maxAsyncReplicas="1">
<map ref="customer"/>
</mapSet>
</objectgridDeployment>
</deploymentPolicy>
Ces fichiers créent une configuration ObjectGrid simple avec une grille ObjectGrid, une instance accounting et une mappe customer.