同步Web服务方法有意义吗?
我正在创建一个用 Java 编写并托管在 JBoss AS 上的 Web 服务。我还不是 Web 服务设计方面的专业人士,但我是否正确理解并且每次调用服务都会启动一个新的线程而不是新的进程?在我的服务中使用同步方法有意义吗?我需要一种方法,一次只能为一个用户调用,而不是同时为多个用户调用。
I'm creating a web-service written in Java and hosted on JBoss AS. I'm not a professional in web-service design yet but do I get it correctly and each call to the service initiates a new thread and not a new process? Does it make sense to have synchronized methods in my service? I need to have a method which is invoked only for one user at a time not simultaneously for multiple.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,请求由单独的处理程序线程处理。所有 JBoss 都有一个单一的进程。
如果您的应用程序最终托管在集群中的多个节点上,同步可能会出现问题。如果没有 Terracotta 等魔法的帮助,锁不会在多个 JVM 之间传播。对于一个简单的解决方案,您可以在数据库中使用悲观行锁来控制访问。人们当然会倾向于挑战需要阻塞方法的整个设计,并寻找一种可以并行运行的替代方案。
另外,如果您要走这条路,那么 java.util.concurrent 包中的锁比 Synchronized 关键字更受青睐。
Yes, requests are handled by individual handler threads. There is a single process for all of JBoss.
Synchronization can be problematic if your application ends up getting hosted across multiple nodes in a cluster. The locks won't propagate across multiple JVMs without the help of some magic like Terracotta. For a simple solution you can use a pessimistic row lock in your database to control access. One would of course be inclined to challenge the entire design that requires a blocking method and look for an alternative that can run in parallel.
Also, Locks from the java.util.concurrent package are preferred to the synchronized keyword if you are going that route.