Share on Google+Share on Google+

Spring AOP concurency Throttle Interceptor

Advertisement
In this tutorial you will see how Interceptor throttles concurrent access.

AOP concurrency Throttle Interceptor

In this example you will see, Interceptor that throttles concurrent access, blocking invocations if a specified concurrency limit is reached.

StudentBean.java

package roseindia.net;

public class StudentBean {
	String studentName;
	String studentCourse;

	public String getStudentName() {
		return studentName;
	}

	public void setStudentName(String studentName) {
		this.studentName = studentName;
	}

	public String getStudentCourse() {
		return studentCourse;
	}

	public void setStudentCourse(String studentCourse) {
		this.studentCourse = studentCourse;
	}

	public void showValue() {
		System.out.println("Show Results:" + this.studentName + " "
				+ this.studentCourse);
	}
}

MainClaz.java

package roseindia.net;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;

public class MainClaz {
	public static void main(String[] args) {
		BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"spring/context.xml"));
		StudentBean studentBean = (StudentBean) beanFactory.getBean("testBean");
		studentBean.showValue();
	}
}

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
    "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
  <bean id="testBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="target">
      <bean class="roseindia.net.StudentBean">
        <property name="studentName" value="James Bond," />
        <property name="studentCourse" value="B.Tech" />
      </bean>
    </property>
    <property name="interceptorNames">
      <list>
        <idref bean="nameMatchMethodPointcutAdvisor" />
      </list>
    </property>
    <property name="proxyTargetClass" value="true" />
  </bean>
  <bean id="nameMatchMethodPointcutAdvisor"
    class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor"
    singleton="false">
    <property name="advice" ref="concurrencyThrottleInterceptor" />
    <property name="mappedName" value="showValue" />
  </bean>
  <bean id="concurrencyThrottleInterceptor"
    class="org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor"
    singleton="false">
    <property name="concurrencyLimit" value="1" />
  </bean>

</beans>

When you run this application it will display message as shown below:


Show Results:James Bond, B.Tech

Download this example code

Advertisements

Posted on: September 15, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.