Example usage for org.springframework.aop.interceptor ConcurrencyThrottleInterceptor setConcurrencyLimit

List of usage examples for org.springframework.aop.interceptor ConcurrencyThrottleInterceptor setConcurrencyLimit

Introduction

In this page you can find the example usage for org.springframework.aop.interceptor ConcurrencyThrottleInterceptor setConcurrencyLimit.

Prototype

public void setConcurrencyLimit(int concurrencyLimit) 

Source Link

Document

Set the maximum number of concurrent access attempts allowed.

Usage

From source file:org.springframework.aop.interceptor.ConcurrencyThrottleInterceptorTests.java

private void testMultipleThreads(int concurrencyLimit) {
    TestBean tb = new TestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(new Class[] { ITestBean.class });
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    cti.setConcurrencyLimit(concurrencyLimit);
    proxyFactory.addAdvice(cti);/*  ww w  .ja va  2s  .c o m*/
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();

    Thread[] threads = new Thread[NR_OF_THREADS];
    for (int i = 0; i < NR_OF_THREADS; i++) {
        threads[i] = new ConcurrencyThread(proxy, null);
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS / 10; i++) {
        try {
            Thread.sleep(5);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        threads[i] = new ConcurrencyThread(proxy,
                i % 2 == 0 ? (Throwable) new OutOfMemoryError() : (Throwable) new IllegalStateException());
        threads[i].start();
    }
    for (int i = 0; i < NR_OF_THREADS; i++) {
        try {
            threads[i].join();
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
    }
}