为啥在建立 httpSession 后 Wildfly 服务器不能处理并发 REST 请求
Posted
技术标签:
【中文标题】为啥在建立 httpSession 后 Wildfly 服务器不能处理并发 REST 请求【英文标题】:Why can't the Wildfly server handle concurrent REST requests after a httpSession is established为什么在建立 httpSession 后 Wildfly 服务器不能处理并发 REST 请求 【发布时间】:2020-04-24 23:20:36 【问题描述】:我遇到了一个我试图理解的难题。我已经实现了一个在 Wildfly 服务器中运行的非常简单的示例 REST 服务。我有一个独立的多线程 Java 测试程序,它使用 Apache http 客户端库来访问该服务。客户端程序有两个独立的线程同时访问 REST 服务。当测试程序在服务器上未建立 httpSession 的情况下运行时,服务器会同时处理请求(如服务器日志中所示)。如果线程在建立 httpSession 后运行,则服务器以顺序方式处理请求(如第二个服务器日志中所示)。请注意,两个线程共享一个会话。
这是设计使然吗?我在这里想念什么?提前致谢。
以下是我的环境的基本原理:
Wildfly 服务器信息:Wildfly-17.0.0.Final 在 Windows 10 上运行,使用 在standalone-ha.xml 中指定的独立、高可用性配置 客户端信息:Apache http 客户端 4.5.5(托管版本)。以下是代表这两种情况的服务器日志:
Server log when no http session is established.
Requests run concurrently when there is no session.
13:39:13,384 INFO (default task-5) Getting widget 3
13:39:13,384 INFO (default task-6) Getting widget 4
13:39:13,384 INFO (default task-6) DELAYING widget 4
13:39:13,384 INFO (default task-5) Returning widget 3
13:39:13,404 INFO (default task-6) DELAY COMPLETE widget 4
13:39:13,404 INFO (default task-6) Returning widget 4
13:39:13,408 INFO (default task-6) Getting widget 3
13:39:13,408 INFO (default task-6) Returning widget 3
13:39:13,409 INFO (default task-5) Getting widget 4
13:39:13,409 INFO (default task-5) DELAYING widget 4
13:39:13,413 INFO (default task-6) Getting widget 3
13:39:13,413 INFO (default task-6) Returning widget 3
13:39:13,419 INFO (default task-6) Getting widget 3
13:39:13,419 INFO (default task-6) Returning widget 3
13:39:13,425 INFO (default task-6) Getting widget 3
13:39:13,425 INFO (default task-6) Returning widget 3
13:39:13,430 INFO (default task-6) Getting widget 3
13:39:13,430 INFO (default task-6) Returning widget 3
13:39:13,436 INFO (default task-5) DELAY COMPLETE widget 4
13:39:13,436 INFO (default task-5) Returning widget 4
13:39:13,436 INFO (default task-6) Getting widget 3
13:39:13,436 INFO (default task-6) Returning widget 3
13:39:13,441 INFO (default task-6) Getting widget 4
13:39:13,441 INFO (default task-5) Getting widget 3
13:39:13,442 INFO (default task-6) DELAYING widget 4
13:39:13,442 INFO (default task-5) Returning widget 3
13:39:13,447 INFO (default task-5) Getting widget 3
13:39:13,447 INFO (default task-5) Returning widget 3
...
Server log after having established a http session.
Requests run sequentially after having established a session.
13:41:08,761 INFO (default task-5) Session established PA7xR3HCYMR3A2S6iX-4Ziw1dGVNejPktseYMMdm
13:41:08,808 INFO (default task-5) Getting widget 4
13:41:08,808 INFO (default task-5) DELAYING widget 4
13:41:08,829 INFO (default task-5) DELAY COMPLETE widget 4
13:41:08,829 INFO (default task-5) Returning widget 4
13:41:08,833 INFO (default task-6) Getting widget 3
13:41:08,833 INFO (default task-6) Returning widget 3
13:41:08,837 INFO (default task-5) Getting widget 4
13:41:08,837 INFO (default task-5) DELAYING widget 4
13:41:08,857 INFO (default task-5) DELAY COMPLETE widget 4
13:41:08,857 INFO (default task-5) Returning widget 4
13:41:08,860 INFO (default task-6) Getting widget 3
13:41:08,860 INFO (default task-6) Returning widget 3
13:41:08,863 INFO (default task-5) Getting widget 4
13:41:08,864 INFO (default task-5) DELAYING widget 4
13:41:08,884 INFO (default task-5) DELAY COMPLETE widget 4
13:41:08,884 INFO (default task-5) Returning widget 4
13:41:08,887 INFO (default task-6) Getting widget 3
13:41:08,887 INFO (default task-6) Returning widget 3
13:41:08,890 INFO (default task-5) Getting widget 4
13:41:08,890 INFO (default task-5) DELAYING widget 4
13:41:08,911 INFO (default task-5) DELAY COMPLETE widget 4
13:41:08,911 INFO (default task-5) Returning widget 4
13:41:08,914 INFO (default task-6) Getting widget 3
13:41:08,915 INFO (default task-6) Returning widget 3
...
这是 REST 服务实现
@Path("concurrencyTest")
@Stateless
public class ConcurrencyTestWs extends BaseWs
private static final Logger log = LoggerFactory
.getLogger(ConcurrencyTestWs.class);
@SuppressWarnings("static-method")
@GET
@Path("/widget")
public Response getWidget(@Context HttpServletRequest request,
@QueryParam(value = "widgetId") Long widgetId)
try
log.info(String.format("Getting widget %d", widgetId));
if ((widgetId % 2) == 0)
log.info(String.format("DELAYING widget %d", widgetId));
try
Thread.sleep(20);
catch (Exception e)
System.err.println(e.getMessage());
log.info(String.format("DELAY COMPLETE widget %d", widgetId));
log.info(String.format("Returning widget %d", widgetId));
return Response.ok(String.format("Widget %d retrieved", widgetId))
.build();
catch (Exception e)
return RestUtilities.getInternalError(request, "Widget", widgetId, e);
@SuppressWarnings("static-method")
@POST
@Path("/establishSession")
public Response establishSession(@Context HttpServletRequest request)
try
HttpSession session = request.getSession(true);
log.info(String.format("Session established %s", session.getId()));
Response response = Response.ok().build();
return response;
catch (Exception e)
return RestUtilities.getInternalError(request, e);
@SuppressWarnings("static-method")
@POST
@Path("/terminateSession")
public Response terminateSession(@Context HttpServletRequest request)
try
HttpSession session = request.getSession(false);
log.info(String.format("Terminating session %s",
(session == null) ? "null" : session.getId()));
Response response = Response.ok().build();
return response;
catch (Exception e)
return RestUtilities.getInternalError(request, e);
这里是客户端源代码
public class WidgetFetchTest
private final static String restServiceUrl = "http://dtp002.novodynamics.com:8081/TEST/REST/v1.0/concurrencyTest";
private final CloseableHttpClient httpClient;
private final CookieStore cookieStore;
public WidgetFetchTest()
this.cookieStore = new BasicCookieStore();
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(60000).setConnectionRequestTimeout(60000)
.setSocketTimeout(60000).setCookieSpec(CookieSpecs.STANDARD).build();
@SuppressWarnings("resource")
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxTotal(100);
connectionManager.setDefaultMaxPerRoute(40);
this.httpClient = HttpClients.custom()
.setDefaultRequestConfig(requestConfig)
.setDefaultCookieStore(this.cookieStore)
.setConnectionManager(connectionManager).build();
public void run()
establishSession(this.httpClient);
DocFetchRunnable r1 = new DocFetchRunnable("4", this.httpClient);
DocFetchRunnable r2 = new DocFetchRunnable("3", this.httpClient);
ExecutorService exec = Executors.newFixedThreadPool(2);
exec.execute(r1);
exec.execute(r2);
try
Thread.sleep(60000);
catch (InterruptedException e)
e.printStackTrace();
protected static void establishSession(CloseableHttpClient client)
try
URIBuilder ub = new URIBuilder(
String.format("%s%s", restServiceUrl, "/establishSession"));
String url = ub.toString();
HttpContext ctx = HttpClientContext.create();
try (CloseableHttpResponse response = client.execute(new HttpPost(url),
ctx))
// intentionally empty.
catch (Exception e)
e.printStackTrace();
catch (URISyntaxException e)
e.printStackTrace();
protected static void getWidget(CloseableHttpClient client, String widgetId)
try
URIBuilder ub = new URIBuilder(
String.format("%s%s", restServiceUrl, "/widget"));
ub.addParameter("widgetId", widgetId);
String url = ub.toString();
HttpContext ctx = HttpClientContext.create();
try (CloseableHttpResponse response = client.execute(new HttpGet(url),
ctx))
// intentionally empty.
catch (Exception e)
e.printStackTrace();
catch (URISyntaxException e)
e.printStackTrace();
private class DocFetchRunnable implements Runnable
private String widgetId;
CloseableHttpClient client;
public DocFetchRunnable(String widgetId, CloseableHttpClient client)
this.widgetId = widgetId;
this.client = client;
@Override
public void run()
for (int i = 0; i < 50; i++)
System.out.println(
String.format("Fetching widget %s - %d", this.widgetId, i));
System.out.flush();
getWidget(this.client, this.widgetId);
System.out.println(
String.format("Widget fetched %s - %d", this.widgetId, i));
System.out.flush();
public static void main(String[] args)
WidgetFetchTest dft = new WidgetFetchTest();
dft.run();
System.exit(0);
【问题讨论】:
【参考方案1】:在 JBossDeveloper 论坛上发布此问题后,提供了以下答案。
请参考文档中的会话并发部分: Distributable Web Applications, Session Concurrency
通过在关联的缓存配置上放松从 REPEATABLE_READ 到 READ_COMMITTED 的隔离来解决此问题。
【讨论】:
以上是关于为啥在建立 httpSession 后 Wildfly 服务器不能处理并发 REST 请求的主要内容,如果未能解决你的问题,请参考以下文章
HttpSession and Hibernate session
servlet的HttpSession与HibernateSession的区别