Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/showcase/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@
<artifactId>struts2-bean-validation-plugin</artifactId>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-async-plugin</artifactId>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.struts2.showcase.async;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
* Filters async actions directly to Struts servlet
*/
public class AsyncFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {

}

@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
String requestURI = ((HttpServletRequest) servletRequest).getRequestURI();
if (!requestURI.contains("/async/receiveNewMessages")) {
filterChain.doFilter(servletRequest, servletResponse); // Just continue chain.
} else {
servletRequest.getRequestDispatcher("/async/receiveNewMessages").forward(servletRequest, servletResponse);
}
}

@Override
public void destroy() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.struts2.showcase.async;

import com.opensymphony.xwork2.ActionSupport;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

/**
* Example to illustrate the <code>async</code> plugin.
*/
public class ChatRoomAction extends ActionSupport {
private String message;
private Integer lastIndex;
private List<String> newMessages;

private static final List<String> messages = new ArrayList<>();

public void setMessage(String message) {
this.message = message;
}

public void setLastIndex(Integer lastIndex) {
this.lastIndex = lastIndex;
}

public List<String> getNewMessages() {
return newMessages;
}

public Callable<String> receiveNewMessages() throws Exception {
return new Callable<String>() {
@Override
public String call() throws Exception {
while (lastIndex >= messages.size()) {
Thread.sleep(3000);
}
newMessages = messages.subList(lastIndex, messages.size());
return SUCCESS;
}
};
}

public String sendMessage() {
synchronized (messages) {
messages.add(message);
}
return SUCCESS;
}
}
49 changes: 49 additions & 0 deletions apps/showcase/src/main/resources/struts-async.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
/*
* 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.
*/
-->
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
<package name="async" extends="json-default" namespace="/async">

<action name="receiveNewMessages" class="org.apache.struts2.showcase.async.ChatRoomAction" method="receiveNewMessages">
<result name="success" type="json">
<param name="root">newMessages</param>
</result>
<result name="timeout" type="json">
<param name="root">newMessages</param>
</result>
</action>

<action name="sendMessage" class="org.apache.struts2.showcase.async.ChatRoomAction" method="sendMessage">
<result name="success" type="json">
<param name="root">newMessages</param>
</result>
<result name="timeout" type="json">
<param name="root">newMessages</param>
</result>
</action>

</package>

</struts>
2 changes: 2 additions & 0 deletions apps/showcase/src/main/resources/struts.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@

<include file="struts-xslt.xml" />

<include file="struts-async.xml" />

<package name="default" extends="struts-default">
<interceptors>
<interceptor-stack name="crudStack">
Expand Down
1 change: 1 addition & 0 deletions apps/showcase/src/main/webapp/WEB-INF/decorators/main.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@
<li><s:a value="/token/index.html">Token</s:a></li>
<li><s:url var="url" namespace="/modelDriven" action="modelDriven"/><s:a
href="%{url}">Model Driven</s:a></li>
<li><s:a value="/async/index.html">Async</s:a></li>
</ul>
</li>
<li class="dropdown">
Expand Down
25 changes: 24 additions & 1 deletion apps/showcase/src/main/webapp/WEB-INF/web.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<display-name>Struts Showcase Application</display-name>


<filter>
<filter-name>async</filter-name>
<filter-class>org.apache.struts2.showcase.async.AsyncFilter</filter-class>
<async-supported>true</async-supported>
</filter>

<filter>
<filter-name>struts-prepare</filter-name>
<filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareFilter</filter-class>
Expand All @@ -40,6 +46,11 @@
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>async</filter-name>
<url-pattern>/async/*</url-pattern>
</filter-mapping>

<filter-mapping>
<filter-name>struts-prepare</filter-name>
<url-pattern>/*</url-pattern>
Expand Down Expand Up @@ -113,6 +124,13 @@
<load-on-startup>1</load-on-startup>
</servlet>

<servlet>
<servlet-name>strutsServlet</servlet-name>
<servlet-class>org.apache.struts2.dispatcher.servlet.StrutsServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
Expand All @@ -128,6 +146,11 @@
<url-pattern>*.vm</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>strutsServlet</servlet-name>
<url-pattern>/async/receiveNewMessages</url-pattern>
</servlet-mapping>

<!-- END SNIPPET: dwr -->

<!-- SNIPPET START: example.velocity.filter.chain
Expand Down
119 changes: 119 additions & 0 deletions apps/showcase/src/main/webapp/async/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<!--
/*
* 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.
*/
-->
<html>
<head>
<title>Struts2 Showcase - Async Example</title>
<script>
function User () {
this.lastIndex = 0;
this.sendMessage = function(message) {
$.ajax({
url: "sendMessage",
data: {
message: message
}
});
};
this.receiveNewMessages = function() {
$.ajax({
url: "receiveNewMessages",
context: this,
data: {
lastIndex: this.lastIndex
},
success: function (result) {
if (result != null) {//result is null on timeout
var msgs = $('#msgs');
var messages = msgs.val();
msgs.val(messages + result + '\n');
this.lastIndex += result.length;
}
this.receiveNewMessages();
}
});
};
this.receiveNewMessages();
}

var user;

function sendMessage() {
var msg = $('#msg');
var message = msg.val();
if (message.length > 0) {
user.sendMessage(message);
msg.val('');
}
return false;
}

$(function() {
user = new User();
});

</script>
</head>

<body>
<div class="page-header">
<h1>Async Example</h1>
</div>



<div class="container-fluid">
<div class="row">
<div class="col-md-12" style="text-align: center;">

<p>
These examples illustrate Struts build in support for async request processing.
</p>
<p>
When you have a process that takes a long time, it can make your app not scalable under heavy load conditions.
Scalability limitations include running out of memory or exhausting the pool of container threads.
To create scalable web applications, you must ensure that no threads associated with a request
are sitting idle, so the container can use them to process new requests.
Asynchronous processing refers to assigning these blocking operations to a new thread and returning
the thread associated with the request immediately to the container.
<br/> Reference: <a href="https://docs.oracle.com/javaee/7/tutorial/servlets012.htm">Asynchronous Processing</a>
<br/> An interesting and vital use case for the async request processing is server push.
A good solution is to use the Servlet 3.0+ asynchronous feature.
</p>

<br/>
<h2>Example: A minimal chat room using server push</h2>
<h3>Open current page in different tabs, browsers and computers then send messages.</h3>
<h4>This is a minimal chat room which uses server push to retrieve new messages.
It doesn't poll the server frequently to check if a new message is available to display.
Instead it waits for the server to push back new messages. This approach has two obvious advantages:
low-lag communication without requests being sent, and no waste of server resources and network bandwidth.</h4>
<h5>Reference: <a href="https://www.javaworld.com/article/2077995/java-concurrency/java-concurrency-asynchronous-processing-support-in-servlet-3-0.html">
Asynchronous processing support in Servlet 3.0</a></h5>
<textarea id="msgs" cols="40" rows="5" title="messages" readonly></textarea><br/>
<form>
<input name="msg" id="msg" type="text" title="message" required />
<input type="submit" value="Send" onclick="return sendMessage();" />
</form>
</div>
</div>
</div>
</body>
</html>
Loading