How to prevent concurrency in web service API?

We have three web service ( /a, /b, /c), where each service maps the method ( go()) in a separate Java class ( ClassA, ClassB, ClassC).

Only one service is started at a time (i.e., /bit cannot work while it is running /a). However, since this is a REST API, nothing prevents clients from simultaneously requesting services.

What is the best and easiest way on the server to ensure that services do not start simultaneously?


Update . This is an internal application, we will not have a large load and there will be only one application server.

Update . This is a subjective question, since you can make different arguments in the overall design of the application, which affects the final answer. Accepted an excessive answer, as I found that the most interesting and useful.

+5
source share
5 answers

Assuming it's not that simple to get the web server to have only one auditory stream serving requests ... I suppose I just used a static lock ( ReentrantLock , perhaps for clarity, although you could synchronize any common object):

public class Global {
  public static final Lock webLock = new ReentrantLock();
}

public class ClassA {
    public void go() {
        Global.webLock.lock()
        try {
            // do A stuff
        } finally {
            Global.webLock.unlock()
        }
    }
}

public class ClassB {
    public void go() {
        Global.webLock.lock()
        try {
            // do B stuff
        } finally {
            Global.webLock.unlock()
        }
    }
}

public class ClassC {
    public void go() {
        Global.webLock.lock()
        try {
            // do C stuff
        } finally {
            Global.webLock.unlock()
        }
    }
}
+6
source

. . , , , , . , , .

+6

-, , , , , concurrency WebService. .., , , - ? -, .

, , , - , -, . B , A , , , , 409 . , , .

+3

- , .

0

?

-

POST /A

. , ,

<ResultsOfProcessA>
  <Status>Complete</Status>
  <ProcessB href="/B"/>
</ResultsOfProcessA>

, ,

POST /B

C.

, B , . A - , B C, URL.

, , , A B. D, (A, B C). D, URI, . A, D B . , B A.

, , A B , D. , A B , D C.

100% - , D, , A , . , "Last Modified" D, , D. . , , , .

0

All Articles