Servlet async smoke test on Payara (#9270)
This commit is contained in:
parent
9a2dd863ee
commit
865fcef43c
|
@ -6,9 +6,11 @@
|
||||||
package io.opentelemetry.smoketest.matrix;
|
package io.opentelemetry.smoketest.matrix;
|
||||||
|
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
import javax.servlet.AsyncContext;
|
import javax.servlet.AsyncContext;
|
||||||
import javax.servlet.http.HttpServlet;
|
import javax.servlet.http.HttpServlet;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
@ -18,9 +20,9 @@ import javax.servlet.http.HttpServletResponse;
|
||||||
public class AsyncGreetingServlet extends HttpServlet {
|
public class AsyncGreetingServlet extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private static final String LATCH_KEY = "LATCH_KEY";
|
||||||
private static final BlockingQueue<AsyncContext> jobQueue = new LinkedBlockingQueue<>();
|
private static final BlockingQueue<AsyncContext> jobQueue = new LinkedBlockingQueue<>();
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(2);
|
private static final ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
private static final GreetingServlet greetingServlet = new GreetingServlet();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
|
@ -50,23 +52,44 @@ public class AsyncGreetingServlet extends HttpServlet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
CountDownLatch latch = null;
|
||||||
System.err.println("start async request");
|
System.err.println("start async request");
|
||||||
AsyncContext ac = req.startAsync(req, resp);
|
AsyncContext ac = req.startAsync(req, resp);
|
||||||
|
boolean isPayara =
|
||||||
|
"org.apache.catalina.connector.AsyncContextImpl".equals(ac.getClass().getName());
|
||||||
|
if (isPayara) {
|
||||||
|
latch = new CountDownLatch(1);
|
||||||
|
req.setAttribute(LATCH_KEY, latch);
|
||||||
|
}
|
||||||
System.err.println("add async request to queue");
|
System.err.println("add async request to queue");
|
||||||
jobQueue.add(ac);
|
jobQueue.add(ac);
|
||||||
System.err.println("async request added to queue");
|
System.err.println("async request added to queue");
|
||||||
|
// Payara has a race condition between exiting from servlet and calling AsyncContext.dispatch
|
||||||
|
// from background thread which can result in dispatch not happening. To work around this we
|
||||||
|
// wait on payara for the dispatch call and only after that exit from servlet code.
|
||||||
|
if (isPayara) {
|
||||||
|
try {
|
||||||
|
latch.await(30, TimeUnit.SECONDS);
|
||||||
|
System.err.println("latch released");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleRequest(AsyncContext ac) {
|
private static void handleRequest(AsyncContext ac) {
|
||||||
System.err.println("handle async request");
|
System.err.println("dispatch async request");
|
||||||
try {
|
try {
|
||||||
greetingServlet.doGet(
|
ac.dispatch("/greeting");
|
||||||
(HttpServletRequest) ac.getRequest(), (HttpServletResponse) ac.getResponse());
|
System.err.println("async request dispatched");
|
||||||
ac.complete();
|
|
||||||
System.err.println("async request handled");
|
|
||||||
} catch (Throwable throwable) {
|
} catch (Throwable throwable) {
|
||||||
System.err.println("handling async request failed");
|
System.err.println("dispatching async request failed");
|
||||||
throwable.printStackTrace();
|
throwable.printStackTrace();
|
||||||
|
throw throwable;
|
||||||
|
}
|
||||||
|
CountDownLatch latch = (CountDownLatch) ac.getRequest().getAttribute(LATCH_KEY);
|
||||||
|
if (latch != null) {
|
||||||
|
latch.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,19 @@ import jakarta.servlet.http.HttpServlet;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import java.util.concurrent.BlockingQueue;
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
import java.util.concurrent.ExecutorService;
|
import java.util.concurrent.ExecutorService;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@SuppressWarnings("SystemOut")
|
@SuppressWarnings("SystemOut")
|
||||||
public class AsyncGreetingServlet extends HttpServlet {
|
public class AsyncGreetingServlet extends HttpServlet {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private static final String LATCH_KEY = "LATCH_KEY";
|
||||||
private static final BlockingQueue<AsyncContext> jobQueue = new LinkedBlockingQueue<>();
|
private static final BlockingQueue<AsyncContext> jobQueue = new LinkedBlockingQueue<>();
|
||||||
private static final ExecutorService executor = Executors.newFixedThreadPool(2);
|
private static final ExecutorService executor = Executors.newFixedThreadPool(2);
|
||||||
private static final GreetingServlet greetingServlet = new GreetingServlet();
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init() {
|
public void init() {
|
||||||
|
@ -49,23 +52,44 @@ public class AsyncGreetingServlet extends HttpServlet {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
||||||
|
CountDownLatch latch = null;
|
||||||
System.err.println("start async request");
|
System.err.println("start async request");
|
||||||
AsyncContext ac = req.startAsync(req, resp);
|
AsyncContext ac = req.startAsync(req, resp);
|
||||||
|
boolean isPayara =
|
||||||
|
"org.apache.catalina.connector.AsyncContextImpl".equals(ac.getClass().getName());
|
||||||
|
if (isPayara) {
|
||||||
|
latch = new CountDownLatch(1);
|
||||||
|
req.setAttribute(LATCH_KEY, latch);
|
||||||
|
}
|
||||||
System.err.println("add async request to queue");
|
System.err.println("add async request to queue");
|
||||||
jobQueue.add(ac);
|
jobQueue.add(ac);
|
||||||
System.err.println("async request added to queue");
|
System.err.println("async request added to queue");
|
||||||
|
// Payara has a race condition between exiting from servlet and calling AsyncContext.dispatch
|
||||||
|
// from background thread which can result in dispatch not happening. To work around this we
|
||||||
|
// wait on payara for the dispatch call and only after that exit from servlet code.
|
||||||
|
if (isPayara) {
|
||||||
|
try {
|
||||||
|
latch.await(30, TimeUnit.SECONDS);
|
||||||
|
System.err.println("latch released");
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
Thread.currentThread().interrupt();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleRequest(AsyncContext ac) {
|
private static void handleRequest(AsyncContext ac) {
|
||||||
System.err.println("handle async request");
|
System.err.println("dispatch async request");
|
||||||
try {
|
try {
|
||||||
greetingServlet.doGet(
|
ac.dispatch("/greeting");
|
||||||
(HttpServletRequest) ac.getRequest(), (HttpServletResponse) ac.getResponse());
|
System.err.println("async request dispatched");
|
||||||
ac.complete();
|
|
||||||
System.err.println("async request handled");
|
|
||||||
} catch (Throwable throwable) {
|
} catch (Throwable throwable) {
|
||||||
System.err.println("handling async request failed");
|
System.err.println("dispatching async request failed");
|
||||||
throwable.printStackTrace();
|
throwable.printStackTrace();
|
||||||
|
throw throwable;
|
||||||
|
}
|
||||||
|
CountDownLatch latch = (CountDownLatch) ac.getRequest().getAttribute(LATCH_KEY);
|
||||||
|
if (latch != null) {
|
||||||
|
latch.countDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue