Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Harness Offsprings to divide, parallelize and conquer

Here's how to quickly turn your apps into a scalable, high-performance data-processing solution

  • Print
  • Feedback

Page 3 of 5

First, import defaultSplitterContext.xml

:

<import resource="classpath:defaultSplitterContext.xml"/>

Second, declare the delayedServiceImplementation bean, which contains the implementation we are trying to parallelize:
<bean id="delayedServiceImpl"   class="net.sourceforge.offsprings.examples.DelayedServiceImpl"/>

Third, declare the AOP Proxy bean delayedService, which acts as a foundation for the point cut interceptor. In summary, the delayedService proxy intercepts all calls to the DelayedService interface and redirects them through the splitter (bean splitterAdviceDelayedService) while using the actual implementation in delayedServiceImplementation:
"target" is the delayedServiceImplementation bean.
     "interceptorNames" is the splitterAdvisorDelayedService bean.

<bean id="delayedService" class="org.springframework.aop.framework.ProxyFactoryBean">
      <property name="proxyInterfaces">
        <value>net.sourceforge.offsprings.examples.DelayedService</value>
              </property>
      <property name="interceptorNames">
        <list>
          <value>splitterAdvisorDelayedService</value>
        </list>
      </property>
      <property name="target">
        <ref bean="delayedServiceImpl"/>
      </property>
    </bean>

Fourth, declare the splitterAdvisorDelayedService bean which effects the point cut to the splitter:
    Intercept all methods with names matching the "pattern" ".*"
    Apply the "advice" given by the splitterAdviceDelayedService bean

<bean id="splitterAdvisorDelayedService"    class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
      <property name="pattern">
        <value>.*</value>
      </property>
               <property name="advice">
                 <ref bean="splitterAdviceDelayedService"/>
      </property>
    </bean>

Fifth, declare the bean splitterAdviceDelayedService, which extends the default advice defaultSplitterAdvice. This bean provides the core functionality of the whole application; it's a splitter.
We can reconfigure splitter parameters as necessary:
  • Property parallelism is the number of separate threads which will be in parallel
  • Property batchsize is the number of items that will be processed in each batch

Some optional properties are only relevant for XML InputStream results:
  • Property docPath is an XML xpath expression to the document element (e.g. delayed_service/docs/doc) within the XML. If omitted the elements in each batches root elements are merged.
  • Property metaPath is an xpath expression to the element containing meta information for each batch. If omitted, it is assumed there is no meta information.
  • Property metaXSLTTemplate provides the name of an XSL template that will be used to merge the content in the location designated by metaPath.
        
  
<bean id="splitterAdviceDelayedService" parent="defaultSplitterAdvice">
      <property name="parallelism" value="10"/>
               <property name="batchsize" value="10"/>
              <property name = "properties">
                 <map>
                   <entry key="docPath" value="delayed_service/docs/doc" />
                   <entry key="metaPath" value="delayed_service/meta_info" />
                   <entry key="metaXSLTTemplate" value="meta.xsl" />
                 </map>
      </property>
    </bean>
<> Take it apart, put it back together

Let's take a look at the implementation details. The SplitterAdvice class (let's call it splitter) is the foundation of the whole application. It orchestrates the splitting of the process in the parallel threads and assembling the result of each thread execution. Splitter is an AOP advice; therefore, it implements the Spring MethodInterceptor interface.

As a result, all calls are redirected to the method invoke(MethodInvocation invocation). The MethodInvocation method must know which argument represents the collection of document identifiers to split them into the chunks. The Parameter idsArgumentPosition can be set in application context to specify which argument contains the identifiers.

  • Print
  • Feedback

Resources