簡體   English   中英

如何使Jersey Rest POST請求同步

[英]How to make Jersey Rest POST request to be synchronized

如何使Jersey REST POST請求同步,以便如果一個請求正在進行中,則無法進行其他請求。

我嘗試使方法synchronized但它不起作用。

不是嘗試synchronize服務方法並按照請求啟動/停止GraphDatabaseServiceGraphDatabaseServiceServletContextListener啟動GraphDatabaseService然后通過Web應用程序的上下文訪問它可能會很有趣。 這利用了GraphDatabaseService是線程安全的事實。

也許像這樣的聽眾:

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

}

您可以在web.xml中初始化,如下所示:

<listener>
  <listener-class>org.example.ExampleListener</listener-class>
</listener>

然后在這樣的REST方法中使用:

@POST
public void graphOperation(@Context ServletContext context) {
  GraphDatabaseService graphDb = (GraphDatabaseService)context.getAttribute("graphDb");
  // Graph operations here...
}

您甚至可以將ServletContext添加到服務類構造函數中,並獲取所需的屬性作為服務類的成員字段,以使其更方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM