Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
676282c6
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
676282c6
编写于
5月 01, 2014
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Support ListenableFuture on @RequestMapping methods
Issue: SPR-11695
上级
0d2aa515
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
113 addition
and
1 deletion
+113
-1
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java
...ework/test/web/servlet/samples/standalone/AsyncTests.java
+38
-1
spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java
...g/springframework/web/bind/annotation/RequestMapping.java
+3
-0
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ListenableFutureReturnValueHandler.java
...method/annotation/ListenableFutureReturnValueHandler.java
+69
-0
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
...t/mvc/method/annotation/RequestMappingHandlerAdapter.java
+1
-0
src/asciidoc/index.adoc
src/asciidoc/index.adoc
+2
-0
未找到文件。
spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/AsyncTests.java
浏览文件 @
676282c6
...
...
@@ -34,6 +34,8 @@ import org.springframework.test.web.Person;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.MvcResult
;
import
org.springframework.ui.Model
;
import
org.springframework.util.concurrent.ListenableFuture
;
import
org.springframework.util.concurrent.ListenableFutureTask
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
org.springframework.web.context.request.async.DeferredResult
;
...
...
@@ -96,11 +98,29 @@ public class AsyncTests {
.
andExpect
(
content
().
string
(
"{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"
));
}
@Test
public
void
testListenableFuture
()
throws
Exception
{
MvcResult
mvcResult
=
this
.
mockMvc
.
perform
(
get
(
"/1"
).
param
(
"listenableFuture"
,
"true"
))
.
andExpect
(
request
().
asyncStarted
())
.
andReturn
();
this
.
asyncController
.
onMessage
(
"Joe"
);
this
.
mockMvc
.
perform
(
asyncDispatch
(
mvcResult
))
.
andExpect
(
status
().
isOk
())
.
andExpect
(
content
().
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andExpect
(
content
().
string
(
"{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"
));
}
@Controller
private
static
class
AsyncController
{
private
Collection
<
DeferredResult
<
Person
>>
deferredResults
=
new
CopyOnWriteArrayList
<
DeferredResult
<
Person
>>();
private
Collection
<
DeferredResult
<
Person
>>
deferredResults
=
new
CopyOnWriteArrayList
<
DeferredResult
<
Person
>>();
private
Collection
<
ListenableFutureTask
<
Person
>>
futureTasks
=
new
CopyOnWriteArrayList
<
ListenableFutureTask
<
Person
>>();
@RequestMapping
(
value
=
"/{id}"
,
params
=
"callable"
,
produces
=
"application/json"
)
...
...
@@ -130,11 +150,28 @@ public class AsyncTests {
return
deferredResult
;
}
@RequestMapping
(
value
=
"/{id}"
,
params
=
"listenableFuture"
,
produces
=
"application/json"
)
@ResponseBody
public
ListenableFuture
<
Person
>
getListenableFuture
()
{
ListenableFutureTask
<
Person
>
futureTask
=
new
ListenableFutureTask
<
Person
>(
new
Callable
<
Person
>()
{
@Override
public
Person
call
()
throws
Exception
{
return
new
Person
(
"Joe"
);
}
});
this
.
futureTasks
.
add
(
futureTask
);
return
futureTask
;
}
public
void
onMessage
(
String
name
)
{
for
(
DeferredResult
<
Person
>
deferredResult
:
this
.
deferredResults
)
{
deferredResult
.
setResult
(
new
Person
(
name
));
this
.
deferredResults
.
remove
(
deferredResult
);
}
for
(
ListenableFutureTask
<
Person
>
futureTask
:
this
.
futureTasks
)
{
futureTask
.
run
();
this
.
futureTasks
.
remove
(
futureTask
);
}
}
}
...
...
spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMapping.java
浏览文件 @
676282c6
...
...
@@ -204,6 +204,9 @@ import java.util.concurrent.Callable;
* <li>A {@link org.springframework.web.context.request.async.DeferredResult}
* which the application uses to produce a return value in a separate
* thread of its own choosing, as an alternative to returning a Callable.
* <li>A {@link org.springframework.util.concurrent.ListenableFuture}
* which the application uses to produce a return value in a separate
* thread of its own choosing, as an alternative to returning a Callable.
* <li>{@code void} if the method handles the response itself (by
* writing the response content directly, declaring an argument of type
* {@link javax.servlet.ServletResponse} / {@link javax.servlet.http.HttpServletResponse}
...
...
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/ListenableFutureReturnValueHandler.java
0 → 100644
浏览文件 @
676282c6
/*
* Copyright 2002-2014 the original author or authors.
*
* Licensed 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.springframework.web.servlet.mvc.method.annotation
;
import
org.springframework.core.MethodParameter
;
import
org.springframework.util.concurrent.ListenableFuture
;
import
org.springframework.util.concurrent.ListenableFutureCallback
;
import
org.springframework.web.context.request.NativeWebRequest
;
import
org.springframework.web.context.request.async.DeferredResult
;
import
org.springframework.web.context.request.async.WebAsyncUtils
;
import
org.springframework.web.method.support.HandlerMethodReturnValueHandler
;
import
org.springframework.web.method.support.ModelAndViewContainer
;
/**
* Handles return values of type
* {@link org.springframework.util.concurrent.ListenableFuture}.
*
* @author Rossen Stoyanchev
* @since 4.1
*/
public
class
ListenableFutureReturnValueHandler
implements
HandlerMethodReturnValueHandler
{
@Override
public
boolean
supportsReturnType
(
MethodParameter
returnType
)
{
Class
<?>
paramType
=
returnType
.
getParameterType
();
return
ListenableFuture
.
class
.
isAssignableFrom
(
paramType
);
}
@Override
public
void
handleReturnValue
(
Object
returnValue
,
MethodParameter
returnType
,
ModelAndViewContainer
mavContainer
,
NativeWebRequest
webRequest
)
throws
Exception
{
if
(
returnValue
==
null
)
{
mavContainer
.
setRequestHandled
(
true
);
return
;
}
DeferredResult
<
Object
>
deferredResult
=
new
DeferredResult
<
Object
>();
WebAsyncUtils
.
getAsyncManager
(
webRequest
).
startDeferredResultProcessing
(
deferredResult
,
mavContainer
);
ListenableFuture
<?>
future
=
(
ListenableFuture
<?>)
returnValue
;
future
.
addCallback
(
new
ListenableFutureCallback
<
Object
>()
{
@Override
public
void
onSuccess
(
Object
result
)
{
deferredResult
.
setResult
(
result
);
}
@Override
public
void
onFailure
(
Throwable
ex
)
{
deferredResult
.
setErrorResult
(
ex
);
}
});
}
}
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/RequestMappingHandlerAdapter.java
浏览文件 @
676282c6
...
...
@@ -588,6 +588,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter
handlers
.
add
(
new
CallableMethodReturnValueHandler
());
handlers
.
add
(
new
DeferredResultMethodReturnValueHandler
());
handlers
.
add
(
new
AsyncTaskMethodReturnValueHandler
(
this
.
beanFactory
));
handlers
.
add
(
new
ListenableFutureReturnValueHandler
());
// Annotation-based return value types
handlers
.
add
(
new
ModelAttributeMethodProcessor
(
false
));
...
...
src/asciidoc/index.adoc
浏览文件 @
676282c6
...
...
@@ -29143,6 +29143,8 @@ The following are the supported return types:
asynchronously in a thread managed by Spring MVC.
* A `DeferredResult<?>` can be returned when the application wants to produce the return
value from a thread of its own choosing.
* A `ListenableFuture<?>` can be returned when the application wants to produce the return
value from a thread of its own choosing.
* Any other return type is considered to be a single model attribute to be exposed to
the view, using the attribute name specified through `@ModelAttribute` at the method
level (or the default attribute name based on the return type class name). The model
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录