如何美元的Web服务API p $ pvent并发? [英] How to prevent concurrency in web service API?

查看:201
本文介绍了如何美元的Web服务API p $ pvent并发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有三个Web服务( / A / B / C ),其中每个服务映射到一个方法(去()在一个单独的Java类( ClassA的 ClassB的 ClassC )。

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

只有一个服务应该在同一时间运行(例如: / B 不能运行,而 / A 是运行)。然而,由于这是一个REST API没有什么prevent客户端请求的服务同时运行。

Only one service should run at the same time (ie: /b cannot run while /a is running). However as this is a REST API there is nothing to prevent clients from requesting the services run concurrently.

什么是服务器上最优秀,最简单的方法的执行的的服务的的同时运行?

What is the best and most simple method on the server to enforce that the services don't run concurrently?


更新:这是一个内部应用程序,我们将不会有大的负荷,将只有一个单一的应用服务器

Update: This is an internal app, we will not have a large load and will just have a single app server.

更新:这是因为你可以在一般的应用程序设计,影响了最终的答案做出不同参数的主观题。接受overthink的回答,因为我发现最有趣和有益的。

Update: This is a subjective question as you can make different arguments on the general application design which affects the final answer. Accepted overthink's answer as I found that most interesting and helpful.

推荐答案

假设它不正常,只是迫使Web服务器只有一个监听线程处理请求...我想我只是用一个静态的锁(<一个href=\"http://java.sun.com/javase/6/docs/api/java/util/concurrent/locks/ReentrantLock.html\">ReentrantLock也许,为清楚起见,尽管你可以在任何共享对象同步,真的):

Assuming it's not ok to just force the web server to have only one listening thread serving requests... I suppose I'd just use a static lock (ReentrantLock probably, for clarity, though you could sync on any shared object, really):

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()
        }
    }
}

这篇关于如何美元的Web服务API p $ pvent并发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆