Program guide > Programming for Spring integration



Start a container server with Spring

You can start a container server using Spring managed extension beans and namespace support.

With several XML files configured for Spring, you can start basic eXtreme Scale container servers.


Procedure

  1. ObjectGrid XML file:

    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>
    

  2. ObjectGrid deployment XML file:

    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>
    

  3. ObjectGrid Spring XML file:

    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:

    1. objectgrid:register: This register the default bean factory for the ObjectGrid "Grid".

    2. objectgrid:server: This defines an ObjectGrid server with name "server". This server will also provide catalog service since it has an objectgrid:catalog bean nested in it.

    3. objectgrid:catalog: This defines an ObjectGrid catalog service endpoint, which is set to "localhost:2809".

    4. objectgrid:container: This defines an ObjectGrid container with specified objectgrid XML file and deployment XML file as we discussed before. The server property specifies which server this container is hosted in.

    5. objectgrid:LRUEvictor: This defines an LRUEvictor with the number of LRU queues to use set to 31.

    6. bean partitionListener: This defines a ShardListener plug-in. You must provide an implementation for this plug-in, so it cannot use the pre-defined beans. Also this scope of the bean is set to "shard", which means there is only one instance of this ShardListener per ObjectGrid shard.

  4. Start the server:

    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();
        }
    }
    


Parent topic:

Program for Spring integration


Related concepts

Spring framework

Spring extension beans and namespace support


Related tasks

Manage transactions with Spring

Related reference

Spring managed extension beans