Vous pouvez démarrer un serveur de conteneur à l'aide de beans d'extension gérés Spring et la prise en charge d'espace de nom.
Tout d'abord, définissez un fichier ObjectGrid XML très simple qui contient un ObjectGrid "Grid" et une mappe "Test". Le fichier ObjectGrid est doté d'un plug-in ObjectGridEventListener appelé "partitionListener" et la mappe "Test" est dotée d'un expulseur appelé "testLRUEvictor". Notez que les plug-in ObjectGridEventListener et de l'expulseur sont configurés à l'aide de Spring, car leurs noms contiennent "{spring}".
<?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="Grid">
<bean id="ObjectGridEventListener" className="{spring}partitionListener" />
<backingMap name="Test" pluginCollectionRef="test" />
</objectGrid>
</objectGrids>
<backingMapPluginCollections>
<backingMapPluginCollection id="test">
<bean id="Evictor" className="{spring}testLRUEvictor"/>
</backingMapPluginCollection>
</backingMapPluginCollections>
</objectGridConfig>
Maintenant, créez un fichier de déploiement XML ObjectGrid simple, comme suit. Le fichier ObjectGrid est divisé en 5 partitions et aucune réplique n'est requise.
<?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="Grid">
<mapSet name="mapSet" numInitialContainers="1" numberOfPartitions="5" minSyncReplicas="0"
maxSyncReplicas="1" maxAsyncReplicas="0">
<map ref="Test"/>
</mapSet>
</objectgridDeployment>
</deploymentPolicy>
Maintenant, nous allons utiliser les beans d'extension gérés pas ObjectGrid Spring et les fonctions de prise en charge d'espaces de noms pour configurer les beans ObjectGrid. Le fichier XML Spring s'appelle Grid_spring.xml. Notez que deux schémas sont inclus dans le fichier XML : spring-beans-2.0.xsd permet d'utiliser les beans gérés Spring, et objectgrid.xsd permet d'utiliser les beans prédéfinis dans l'espace de noms objectgrid.
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:objectgrid="http://www.ibm.com/schema/objectgrid"
xsi:schemaLocation="
http://www.ibm.com/schema/objectgrid
http://www.ibm.com/schema/objectgrid/objectgrid.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<objectgrid:register id="ogregister" gridname="Grid"/>
<objectgrid:server id="server" isCatalog="true" name="server">
<objectgrid:catalog host="localhost" port="2809"/>
</objectgrid:server>
<objectgrid:container id="container"
objectgridxml="com/ibm/ws/objectgrid/test/springshard/objectgrid.xml"
deploymentxml="com/ibm/ws/objectgrid/test/springshard/deployment.xml"
server="server"/>
<objectgrid:LRUEvictor id="testLRUEvictor" numberOfLRUQueues="31"/>
<bean id="partitionListener"
class="com.ibm.websphere.objectgrid.springshard.ShardListener" scope="shard"/>
</beans>
Ce fichier XML Spring contient six beans définis :
Le fragment ci-dessous démarre le serveur ObjectGrid, lequel héberge à la fois les services de conteneur et de catalogue. Comme nous pouvons le voir, la seule méthode à appeler pour démarrer le serveur est d'obtenir un "conteneur" de bean de la fabrique de beans. Cela simplifie la complexité de la programmation en déplaçant la plupart de la logique dans la configuration de Spring.
public class ShardServer extends TestCase
{
Container container;
org.springframework.beans.factory.BeanFactory bf;
public void startServer(String cep)
{
try
{
bf = new org.springframework.context.support.ClassPathXmlApplicationContext(
"/com/ibm/ws/objectgrid/test/springshard/Grid_spring.xml", ShardServer.class);
container = (Container)bf.getBean("container");
}
catch (Exception e)
{
throw new ObjectGridRuntimeException("Cannot start OG container", e);
}
}
public void stopServer()
{
if(container != null)
container.teardown();
}
}