Puede iniciar un servidor de contenedor utilizando beans de ampliación gestionados Spring y soporte de espacio de nombres.
En primer lugar, defina un archivo XML ObjectGrid muy sencillo que contenga una "Grid" de ObjectGrid "Grid" y una correlación "Test". ObjectGrid tiene un plug-in ObjectGridEventListener llamado "partitionListener", y la correlación "Test" tiene un desalojador conectado llamado "testLRUEvictor". Tenga en cuenta que el plug-in ObjectGridEventListener y el plug-in Evictor se han configurado ambos utilizando Spring ya que sus nombres contienen "{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>
Ahora, cree un archivo XML de despliegue de ObjectGrid sencillo del modo siguiente. Divida ObjectGrid en 5 particiones, no es necesaria ninguna réplica.
<?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>
Ahora se utilizarán ambas características, los beans de ampliación gestionados Spring de ObjectGrid y el soporte de espacio de nombres, para configurar los beans ObjectGrid. El archivo XML de Spring se denomina Grid_spring.xml. Tenga en cuenta que se incluyen dos esquemas en el archivo XML: spring-beans-2.0.xsd es para la utilización de los beans gestionados Spring, y objectgrid.xsd para la utilización de los beans predefinidos en el espacio de nombres de 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>
Existen seis beans definidos en este archivo XML Spring:
El fragmento siguiente inicia el servidor ObjectGrid, que aloja tanto el servicio de contenedor, como el servicio de catálogos. Como se puede ver, el único método que se necesita llamar para iniciar el servidor es obtener un "container" de la fábrica de beans. Así se simplifica la complejidad de la programación moviendo la mayoría de la lógica a la configuración 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();
}
}