You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2019/11/07 13:18:15 UTC
[tomee] 02/02: Do not unconditionally call AsyncContext.dispatch()
This is an automated email from the ASF dual-hosted git repository.
jgallimore pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomee.git
commit 5e904cac7cc0c425d8b841d386f5951e3ea63a20
Author: Jonathan Gallimore <jo...@jrg.me.uk>
AuthorDate: Thu Nov 7 13:17:38 2019 +0000
Do not unconditionally call AsyncContext.dispatch()
---
.../org/superbiz/asyncservlet/CalcServlet.java | 35 ++++++++++++++++++++--
pom.xml | 2 +-
2 files changed, 33 insertions(+), 4 deletions(-)
diff --git a/examples/async-servlet/src/main/java/org/superbiz/asyncservlet/CalcServlet.java b/examples/async-servlet/src/main/java/org/superbiz/asyncservlet/CalcServlet.java
index de4d1b2..1dc7453 100644
--- a/examples/async-servlet/src/main/java/org/superbiz/asyncservlet/CalcServlet.java
+++ b/examples/async-servlet/src/main/java/org/superbiz/asyncservlet/CalcServlet.java
@@ -18,6 +18,8 @@ package org.superbiz.asyncservlet;
import javax.ejb.EJB;
import javax.servlet.AsyncContext;
+import javax.servlet.AsyncEvent;
+import javax.servlet.AsyncListener;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@@ -25,6 +27,7 @@ import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
+import java.util.concurrent.atomic.AtomicBoolean;
@WebServlet(urlPatterns = "/*", asyncSupported = true)
public class CalcServlet extends HttpServlet {
@@ -111,6 +114,30 @@ public class CalcServlet extends HttpServlet {
final int threadDelay = delay;
final AsyncContext asyncContext = req.startAsync();
+
+ final AtomicBoolean interrupted = new AtomicBoolean(false);
+
+ asyncContext.addListener(new AsyncListener() {
+ @Override
+ public void onComplete(AsyncEvent asyncEvent) throws IOException {
+
+ }
+
+ @Override
+ public void onTimeout(AsyncEvent asyncEvent) throws IOException {
+ interrupted.set(true);
+ }
+
+ @Override
+ public void onError(AsyncEvent asyncEvent) throws IOException {
+ interrupted.set(true);
+ }
+
+ @Override
+ public void onStartAsync(AsyncEvent asyncEvent) throws IOException {
+
+ }
+ });
asyncContext.setTimeout(timeout);
asyncContext.start(() -> {
@@ -122,10 +149,12 @@ public class CalcServlet extends HttpServlet {
try {
process(operation, x, y, result);
- } catch (final Exception e) {
-
} finally {
- asyncContext.dispatch();
+ if (! interrupted.get()) {
+
+ // do not call dispatch if this request has timed-out or errored
+ asyncContext.dispatch();
+ }
}
});
diff --git a/pom.xml b/pom.xml
index 6cd7a33..e2e4cc1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -214,7 +214,7 @@
<microprofile.version>2.0</microprofile.version>
<microprofile.config.version>1.3</microprofile.config.version>
<microprofile.config.impl.version>1.2.1</microprofile.config.impl.version>
- <microprofile.jwt.version>1.2-SNAPSHOT</microprofile.jwt.version>
+ <microprofile.jwt.version>1.1.1</microprofile.jwt.version>
<microprofile.jwt.impl.version>${project.version}</microprofile.jwt.impl.version>
<!-- 1.1 Implementation not started yet -->
<microprofile.fault-tolerance.version>1.1.4</microprofile.fault-tolerance.version>