Java – restrict access to spring MVC controller – N sessions at a time
We have licensed commercial products (unimportant products in this case), which is limited by the number of concurrent users Users access this product through spring controller
We have n licenses for this product. If n 1 users access it, they will receive an error message about the need to purchase more licenses I want to make sure that users don't see this message and want requests for products to be "queued" instead of allowing n 1 users to actually access it Of course, they prefer me to buy a license, so their tools won't let us do this locally
Instead of being able to control tools, I want to limit the number of concurrent sessions of the controller to never exceed n. others can wait
We are using spring MVC
Any ideas?
Solution
What you need is an objectpool View Apache commons pool http://commons.apache.org/pool
When the application starts, you should create an object pool containing license or commercial library objects (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 N pool.setMaxActive(N) // We want to wait if the pool is exhausted pool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_BLOCK)
When you need business objects 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, you need to provide more details about commercial libraries