简体   繁体   中英

Clustered, Stateless web application - how to handle concurrent requests?

We have a spring mvc web application that will serve many users in their actions. Our data source is a mySQL DB.

The web app is located on a cluster of tomcat server.

Users do not have a session while interacting with the web application.

The protocol is a simple REST API between users and the web app.

We would like to ignore users requests if another request handling is still in progress. for example:

user1 requests for action1...
action1 handling begins in webapp.
user1 requests for action1 again (before the handling is completed)
server declines the 2nd request since action1 handling is still in progress..
action1 handling completed.
result for action1 is returned to user1's client.
(now further actions is accepted by the web app)

How is it possible to achieve this? if we used a single node of web app we could manage it simply in memory, but since we use a cluster and no shared memory\\cache it wont work.

Another alternative we thought about is using a locking in the DB, for example create a constraint for the userID in a dedicated table (called userLock) and before each handling we simply insert into this table, and finalizing by removing the entry from it, now if an illegal request is made, a constraint exception is thrown and not handled)

are there any other alternatives to this "semaphore" behavior?

Hazelcast provides distributed implementation of java.util.concurrent.locks.Lock which could be of use to you.

    HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
    Lock lock = hazelcastInstance.getLock( "myLock" );
    lock.lock();
    try { 
       // do something here
    } finally {
      lock.unlock();
    }

More options are listed in this answer.

您可以使用某些排队系统,JMS或其他http://en.m.wikipedia.org/wiki/Java_Message_Service

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM