[英]How to make Jersey Rest POST request to be synchronized
How can i make Jersey REST POST request Synchronized so that if one request is in process no other request can be made. 如何使Jersey REST POST请求同步,以便如果一个请求正在进行中,则无法进行其他请求。
i tried making method synchronized
but it didn't work. 我尝试使方法synchronized
但它不起作用。
Instead of trying to synchronize
your service methods and start / stop your GraphDatabaseService
per request it might be interesting to start your GraphDatabaseService
in a ServletContextListener
and then access it through the web application's context. 不是尝试synchronize
服务方法并按照请求启动/停止GraphDatabaseService
, GraphDatabaseService
在ServletContextListener
启动GraphDatabaseService
然后通过Web应用程序的上下文访问它可能会很有趣。 This takes advantage of the fact that GraphDatabaseService
is thread safe. 这利用了GraphDatabaseService
是线程安全的事实。
Perhaps a listener like this: 也许像这样的听众:
public class ExampleListener implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) {
sce.getServletContext().setAttribute("graphDb", new GraphDatabaseFactory().newEmbeddedDatabase("/tmp/GraphDB"));
}
public void contextDestroyed(ServletContextEvent sce) {
((GraphDatabaseService)sce.getServletContext().getAttribute("graphDb")).shutdown();
}
}
which you can initialize in your web.xml like this: 您可以在web.xml中初始化,如下所示:
<listener>
<listener-class>org.example.ExampleListener</listener-class>
</listener>
and then utilize in a REST method like this: 然后在这样的REST方法中使用:
@POST
public void graphOperation(@Context ServletContext context) {
GraphDatabaseService graphDb = (GraphDatabaseService)context.getAttribute("graphDb");
// Graph operations here...
}
You could even add your ServletContext
to your service class constructor and get the attributes you need as member fields of your service class to make it more convenient. 您甚至可以将ServletContext
添加到服务类构造函数中,并获取所需的属性作为服务类的成员字段,以使其更方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.