简体   繁体   English

如何确保一次只调用一个servlet(不是实例)?

[英]how to I make sure that only one servlet(not instance) is call at a time?

I have an application which makes lot of multiple servlet calls. 我有一个可以进行多个servlet调用的应用程序。 How do I make sure that till a particular servlet X processing is completed no other servlet instance of Y, Z is called. 如何确保在完成特定的Servlet X处理之前,不会调用Y,Z的其他servlet实例。

A SingleThreadModel or synchronization will make sure that only one thread per servlet is called. SingleThreadModel或同步将确保每个servlet仅调用一个线程。 However that may not work on more than one servlet. 但是,这可能不适用于多个servlet。

Use Callback mechanism for each server calls. 对每个服务器调用使用回调机制。 Onsuccess of first server call you can call second. 第一个服务器呼叫成功后,您可以再呼叫一次。

You could implement manual synchronization by defining your own locks. 您可以通过定义自己的锁来实现手动同步。

For Example declare a static Object to use as semaphore: 例如,声明一个静态对象用作信号量:

public static final Object MY_LOCK = new Object();

and where you want to synchronize you could use it like that: 而您想要同步的地方可以像这样使用它:

public void someMethod() {
    synchronized(MY_LOCK) {
        // do stuff that may not be called at same time
    }
}

public void someOtherMethod() {
    synchronized(MY_LOCK) {
        // do other stuff that may not be called at same time
    }
}

That way you can use the same Lock in different Methods (or servlets in your case). 这样,您可以在不同的方法(或您的案例中的Servlet)中使用相同的Lock。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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