You are viewing a plain text version of this content. The canonical link for it is here.
Posted to pluto-scm@portals.apache.org by ms...@apache.org on 2016/04/08 15:40:39 UTC
[27/34] portals-pluto git commit: Completed async listener. Added
listener to demo async portlet.
Completed async listener. Added listener to demo async portlet.
Project: http://git-wip-us.apache.org/repos/asf/portals-pluto/repo
Commit: http://git-wip-us.apache.org/repos/asf/portals-pluto/commit/aae89a34
Tree: http://git-wip-us.apache.org/repos/asf/portals-pluto/tree/aae89a34
Diff: http://git-wip-us.apache.org/repos/asf/portals-pluto/diff/aae89a34
Branch: refs/heads/V3Prototype
Commit: aae89a3472117160620d253df2776850d9599834
Parents: 5c26ec4
Author: Scott Nicklous <ms...@apache.org>
Authored: Sun Apr 3 23:09:03 2016 +0200
Committer: Scott Nicklous <ms...@apache.org>
Committed: Sun Apr 3 23:09:03 2016 +0200
----------------------------------------------------------------------
.../portals/samples/AsyncPortletListener.java | 118 +++++++++++++++++++
.../portals/samples/AsyncPortletResource.java | 4 +
.../container/PortletAsyncListener.java | 31 ++++-
3 files changed, 152 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/aae89a34/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletListener.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletListener.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletListener.java
new file mode 100644
index 0000000..ce2086c
--- /dev/null
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletListener.java
@@ -0,0 +1,118 @@
+/* Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.portals.samples;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.logging.Logger;
+
+import javax.servlet.AsyncContext;
+import javax.servlet.AsyncEvent;
+import javax.servlet.AsyncListener;
+import javax.servlet.ServletResponse;
+
+/**
+ * @author Scott Nicklous
+ *
+ */
+public class AsyncPortletListener implements AsyncListener {
+ private static final Logger LOGGER = Logger.getLogger(AsyncPortletListener.class.getName());
+
+ private long start = System.currentTimeMillis();
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.AsyncListener#onComplete(javax.servlet.AsyncEvent)
+ */
+ @Override
+ public void onComplete(AsyncEvent evt) throws IOException {
+ long delta = System.currentTimeMillis() - start;
+
+ StringBuilder txt = new StringBuilder(128);
+ txt.append("Listener: Completed. Execution time: ").append(delta).append(" milliseconds.");
+
+ LOGGER.fine(txt.toString());
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.AsyncListener#onError(javax.servlet.AsyncEvent)
+ */
+ @Override
+ public void onError(AsyncEvent evt) throws IOException {
+
+ // this doesn't seem to get called when an error occurs in the executor
+ // thread.
+
+ long delta = System.currentTimeMillis() - start;
+
+ StringBuilder txt = new StringBuilder(128);
+ txt.append("Listener: Error after ").append(delta).append(" milliseconds.");
+ txt.append(", Exception: ").append(evt.getThrowable().getMessage());
+
+ LOGGER.fine(txt.toString());
+ evt.getAsyncContext().complete();
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.AsyncListener#onStartAsync(javax.servlet.AsyncEvent)
+ */
+ @Override
+ public void onStartAsync(AsyncEvent evt) throws IOException {
+ long delta = System.currentTimeMillis() - start;
+ StringBuilder txt = new StringBuilder(128);
+ txt.append("Async started again after ").append(delta).append(" milliseconds.");
+ LOGGER.fine(txt.toString());
+
+ // need to add this listener again so it gets called when finally
+ // complete.
+
+ AsyncContext ctx = evt.getAsyncContext();
+ ctx.addListener(this);
+ }
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see javax.servlet.AsyncListener#onTimeout(javax.servlet.AsyncEvent)
+ */
+ @Override
+ public void onTimeout(AsyncEvent evt) throws IOException {
+ long delta = System.currentTimeMillis() - start;
+
+ StringBuilder txt = new StringBuilder(128);
+ txt.append("Listener: Timeout after ").append(delta).append(" milliseconds.");
+
+ try {
+ PrintWriter writer = evt.getAsyncContext().getResponse().getWriter();
+ writer.println("<p>");
+ writer.println(txt.toString());
+ writer.println("</p>");
+ evt.getAsyncContext().complete();
+ } catch (Exception e) {
+ txt.append(" Couldn't get response and complete. Exception: " + e.toString());
+ }
+ LOGGER.fine(txt.toString());
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/aae89a34/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
----------------------------------------------------------------------
diff --git a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
index 59cc599..6cad2fb 100644
--- a/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
+++ b/PortletV3AnnotatedDemo/src/main/java/org/apache/portals/samples/AsyncPortletResource.java
@@ -129,6 +129,8 @@ public class AsyncPortletResource {
break;
}
+ } catch (IllegalStateException e) {
+ LOGGER.warning("Request may have timed out before it could complete. Exception: " + e.toString());
} catch (Exception e) {
LOGGER.warning("Exception during async processing: " + e.toString());
}
@@ -170,6 +172,8 @@ public class AsyncPortletResource {
AsyncContext ctx = req.startAsync();
ctx.setTimeout(4000);
+ AsyncPortletListener apl = new AsyncPortletListener();
+ ctx.addListener(apl);
if (auto || (adb.getDelay() <= 0)) {
http://git-wip-us.apache.org/repos/asf/portals-pluto/blob/aae89a34/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/services/container/PortletAsyncListener.java
----------------------------------------------------------------------
diff --git a/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/services/container/PortletAsyncListener.java b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/services/container/PortletAsyncListener.java
index d645aae..97b77c6 100644
--- a/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/services/container/PortletAsyncListener.java
+++ b/pluto-portal-driver-impl/src/main/java/org/apache/pluto/driver/services/container/PortletAsyncListener.java
@@ -25,7 +25,9 @@ import javax.portlet.ResourceRequest;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
+import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
import org.apache.pluto.container.PortletInvokerService;
import org.apache.pluto.container.PortletResourceResponseContext;
@@ -134,7 +136,34 @@ public class PortletAsyncListener implements AsyncListener {
long delta = System.currentTimeMillis() - start;
StringBuilder txt = new StringBuilder(128);
txt.append("Timeout after ").append(delta).append(" milliseconds.");
- LOGGER.fine(txt.toString());
+
+ // if the application has properly registered a listener and has not processed the
+ // timeout by calling onComplete or dispatch, complete the request.
+
+ boolean warn = false;
+ AsyncContext ctx = evt.getAsyncContext();
+ try {
+ ctx.getRequest();
+
+ try {
+ ctx.complete();
+ txt.append(" Portlet container completed request processing on behalf of the application.");
+ warn = true;
+ } catch (IllegalStateException e) {
+ txt.append(" An earlier listener has dispatched again.");
+ } catch (Exception e) {
+ txt.append(" Exception occured while completing request: " + e.toString());
+ }
+
+ } catch(Exception e) {
+ txt.append(" Async processing was completed by the application.");
+ }
+
+ if (warn) {
+ LOGGER.warning(txt.toString());
+ } else {
+ LOGGER.fine(txt.toString());
+ }
}
}