You can start a container server using Spring managed extension beans and namespace support.
First of all, define a very simple ObjectGrid XML file which contains one ObjectGrid "Grid" and one map "Test". The ObjectGrid has an ObjectGridEventListener plug-in called "partitionListener", and the map "Test" has an Evictor plugged in called "testLRUEvictor". Notice both the ObjectGridEventListener plug-in and Evictor plug-in are configured using Spring as their names contain "{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>
Now, create a simple ObjectGrid deployment XML file as follows. It partitions the ObjectGrid into 5 partitions, and no replica is required.
<?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>
Now we will use both ObjectGrid Spring managed extension beans and namespace support features to configure the ObjectGrid beans. The spring xml file is named Grid_spring.xml. Notice two schemas are included in the XML file: spring-beans-2.0.xsd is for using the Spring managed beans, and objectgrid.xsd is for using the beans predefined in the objectgrid namespace.
<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>
There were six beans defined in this spring XML file:
The snippet below starts the ObjectGrid server, which hosts both the container service and the catalog service. As we can see, the only method we need to call to start the server is to get a bean "container" from the bean factory. This simplifies the programming complexity by moving most of the logic into Spring configuration.
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();
}
}