WebSphere eXtreme Scale Administration Guide > Configure WebSphere eXtreme Scale > Integrate with Spring framework



Spring extension beans and namespace support


WebSphere eXtreme Scale provides a feature to declare plain old Java™ objects (POJOs) to use as extension points in the objectgrid.xml file and a way to name the beans and then specify the class name. Normally, instances of the specified class are created, and those objects are used as the plug-ins. Now, eXtreme Scale can delegate to Spring to obtain instances of these plug-in objects. If an application uses Spring then typically such POJOs have a requirement to be wired in to the rest of the application.

In some cases, use Spring to configure certain plug-in objects. Take the following configuration as an example:

<objectGrid name="Grid">
   
<bean id="TransactionCallback" className="com.ibm.websphere.objectgrid.jpa.JPATxCallback">
       
<property name="persistenceUnitName" type="java.lang.String"  value="employeePU" />
   
</bean>
    ...
</objectGrid>

The built-in TransactionCallback implementation, com.ibm.websphere.objectgrid.jpa.JPATxCallback class, is configured as the TransactionCallback class. This class is configured with one property persistenceUnitName as shown in the previous example. The JPATxCallback class also has the JPAPropertyFactory attribute, which is of type java.lang.Object. The ObjectGrid XML configuration cannot support this type of configuration.

The eXtreme Scale Spring integration solves this problem by delegating the bean creation to the Spring framework. The revised configuration follows:

<objectGrid name="Grid">
   
<bean id="TransactionCallback" className="{spring}jpaTxCallback"/>
    ...
</objectGrid>

The spring file for the "Grid" object contains the following information:

<bean id="jpaTxCallback" class="com.ibm.websphere.objectgrid.jpa.JPATxCallback" scope="shard">
   
<property name="persistenceUnitName" value="employeeEMPU"/>
   
<property name="JPAPropertyFactory" ref ="jpaPropFactory"/>
</bean>

<bean id="jpaPropFactory" class="com.ibm.ws.objectgrid.jpa.plugins.
JPAPropFactoryImpl" scope="shard">
</bean>

Here, the TransactionCallback is specified as {spring}jpaTxCallback, and the jpaTxCallback and jpaPropFactory bean are configured in the spring file as shown in the previous example. The Spring configuration makes it possible to configure a JPAPropertyFactory bean as a parameter of the JPATxCallback object.


Default Spring bean factory

When eXtreme Scale finds a plug-in or an extension bean (such as an ObjectTransformer, Loader, TransactionCallback, and so on) with a classname value that begins with the prefix {spring}, then eXtreme Scale uses the remainder of the name as a Spring Bean name and obtain the bean instance using the Spring Bean Factory.

By default, if no bean factory was registered for a given ObjectGrid, then it tries to find an ObjectGridName_spring.xml file. For example, if the grid is called "Grid" then the XML file is called /Grid_spring.xml. This file should be in the class path or in a META-INF directory which is in the class path. If this file is found, then eXtreme Scale constructs an ApplicationContext using that file and constructs beans from that bean factory.


Custom Spring bean factory

WebSphere eXtreme Scale also provides an ObjectGridSpringFactory API to register a Spring Bean Factory instance to use for a specific named ObjectGrid. This API registers an instance of BeanFactory with eXtreme Scale using the following static method:

void registerSpringBeanFactoryAdapter(String objectGridName, Object springBeanFactory)


Namespace support

Since version 2.0, Spring has a mechanism for schema-based extensions to the basic Spring XML format for defining and configuring beans. ObjectGrid uses this new feature to define and configure ObjectGrid beans. With Spring XML schema extension, some of the built-in implementations of eXtreme Scale plug-ins and some ObjectGrid beans are predefined in the "objectgrid" namespace. When writing the Spring configuration files, you do not have to specify the full class name of the built-ins. Instead, you can reference the predefined beans.

Also, with the attributes of the beans defined in the XML schema, you are less likely to provide a wrong attribute name. XML validation based on the XML schema can catch these kind of errors earlier in the development cycle.

These beans defined in the XML schema extensions are:

These beans are defined in the objectgrid.xsd XML schema. This XSD file is shipped as com/ibm/ws/objectgrid/spring/namespace/objectgrid.xsd file in the ogspring.jar file . For detailed descriptions of the XSD file and the beans defined in the XSD file, see Spring descriptor XML file.

Still use the JPATxCallback example from the previous section. In the previous section, the JPATxCallback bean is configured as the following:

<bean id="jpaTxCallback" class="com.ibm.websphere.objectgrid.jpa.JPATxCallback" scope="shard">
   
<property name="persistenceUnitName" value="employeeEMPU"/>
   
<property name="JPAPropertyFactory" ref ="jpaPropFactory"/>
</bean>

<bean id="jpaPropFactory" class="com.ibm.ws.objectgrid.jpa.plugins.JPAPropFactoryImpl" scope="shard">
</bean>

Use this namespace feature, the spring XML configuration can be written as the following:

<objectgrid:JPATxCallback id="jpaTxCallback" persistenceUnitName="employeeEMPU" 
    jpaPropertyFactory="jpaPropFactory" />

<bean id="jpaPropFactory" class="com.ibm.ws.objectgrid.jpa.plugins.JPAPropFactoryImpl" 
    scope="shard">
</bean>

Notice here that instead of specifying the "com.ibm.websphere.objectgrid.jpa.JPATxCallback" class as in the previous example, we directly use the pre-defined "objectgrid:JPATxCallback" bean. As you can see, this configuration is less verbose and more friendly to error checking.


Start container server with Spring extension beans

In this example, we will show how to start an ObjectGrid server using ObjectGrid Spring managed extension beans and namespace support.


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>


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>


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 6 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. This class is a class plugged in by users, 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.


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

Integrate with Spring framework


+

Search Tips   |   Advanced Search