Limit Access To a Spring MVC Controller -- N sessions at a time Limit Access To a Spring MVC Controller -- N sessions at a time multithreading multithreading

Limit Access To a Spring MVC Controller -- N sessions at a time


What you need is a ObjectPool. Check out Apache Commons Pool http://commons.apache.org/pool

At the application startup you should create a Object Pool with licences or objects of commercial library (Not sure what kind of public interface they have).

public class CommercialObjectFactory extends BasePoolableObjectFactory {     // for makeObject we'll simply return a new commercial object    @Override    public Object makeObject() {         return new CommercialObject();     } }GenericObjectPool pool = new GenericObjectPool(new CommercialObjectFactory());// The size of pool in our case it is Npool.setMaxActive(N) // We want to wait if the pool is exhaustedpool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK) 

And when you need the commercial object in your code.

CommercialObject obj = null;try {     obj = (CommercialObject)pool.borrowObject();    // use the commerical object the way you to use it.    // ....} finally {     // be nice return the borrwed object    try {        if(obj != null) {            pool.returnObject(obj);        }    } catch(Exception e) {        // ignored    }} 

If this is not what you want then you will need to provide more detail about your commercial library.


Spring has a org.springframework.aop.interceptor.ConcurrencyThrottleInterceptor that can be used via AOP (or the underlying code can be used standalone). That might be a lighter weight approach than using a object pool.


I'm thinking of a SessionListener to increment the count when a session is created and decrement it when the session is invalidated or times out and an aspect to guard calls to the URL. But a design that clearly marries the two ideas together is eluding me.