Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
69d4eaaa
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,发现更多精彩内容 >>
提交
69d4eaaa
编写于
8月 17, 2015
作者:
R
Rossen Stoyanchev
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Use PublisherFactory from reactor-core
上级
773d0444
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
39 addition
and
140 deletion
+39
-140
spring-web-reactive/build.gradle
spring-web-reactive/build.gradle
+1
-0
spring-web-reactive/src/main/java/org/springframework/reactive/web/DispatcherHttpHandler.java
...g/springframework/reactive/web/DispatcherHttpHandler.java
+3
-24
spring-web-reactive/src/test/java/org/springframework/reactive/web/DispatcherApp.java
.../java/org/springframework/reactive/web/DispatcherApp.java
+35
-116
未找到文件。
spring-web-reactive/build.gradle
浏览文件 @
69d4eaaa
...
...
@@ -22,6 +22,7 @@ dependencies {
compile
"org.springframework:spring-core:4.2.0.RELEASE"
compile
"org.springframework:spring-web:4.2.0.RELEASE"
compile
"org.reactivestreams:reactive-streams:1.0.0"
compile
"io.projectreactor:reactor-core:2.0.5.RELEASE"
compile
"org.slf4j:slf4j-api:1.7.6"
compile
"ch.qos.logback:logback-classic:1.1.2"
...
...
spring-web-reactive/src/main/java/org/springframework/reactive/web/DispatcherHttpHandler.java
浏览文件 @
69d4eaaa
...
...
@@ -21,6 +21,8 @@ import java.util.List;
import
org.reactivestreams.Publisher
;
import
org.reactivestreams.Subscriber
;
import
org.reactivestreams.Subscription
;
import
reactor.core.reactivestreams.PublisherFactory
;
import
reactor.core.reactivestreams.SubscriberWithContext
;
import
org.springframework.beans.factory.BeanFactoryUtils
;
import
org.springframework.context.ApplicationContext
;
...
...
@@ -58,7 +60,7 @@ public class DispatcherHttpHandler implements HttpHandler {
if
(
handler
==
null
)
{
// No exception handling mechanism yet
response
.
setStatusCode
(
HttpStatus
.
NOT_FOUND
);
return
Publisher
s
.
complete
(
);
return
Publisher
Factory
.
forEach
(
SubscriberWithContext:
:
onComplete
);
}
HandlerAdapter
handlerAdapter
=
getHandlerAdapter
(
handler
);
...
...
@@ -143,27 +145,4 @@ public class DispatcherHttpHandler implements HttpHandler {
throw
new
IllegalStateException
(
"No HandlerAdapter for "
+
handler
);
}
private
static
class
Publishers
{
public
static
Publisher
<
Void
>
complete
()
{
return
subscriber
->
{
subscriber
.
onSubscribe
(
new
NoopSubscription
());
subscriber
.
onComplete
();
};
}
}
private
static
class
NoopSubscription
implements
Subscription
{
@Override
public
void
request
(
long
n
)
{
}
@Override
public
void
cancel
()
{
}
}
}
spring-web-reactive/src/test/java/org/springframework/reactive/web/DispatcherApp.java
浏览文件 @
69d4eaaa
...
...
@@ -24,6 +24,7 @@ import io.reactivex.netty.protocol.http.server.HttpServer;
import
org.reactivestreams.Publisher
;
import
org.reactivestreams.Subscriber
;
import
org.reactivestreams.Subscription
;
import
reactor.core.reactivestreams.PublisherFactory
;
import
org.springframework.http.MediaType
;
import
org.springframework.reactive.web.rxnetty.RequestHandlerAdapter
;
...
...
@@ -80,21 +81,10 @@ public class DispatcherApp {
@Override
public
Publisher
<
String
>
handle
(
ServerHttpRequest
request
,
ServerHttpResponse
response
)
{
return
new
Publisher
<
String
>()
{
@Override
public
void
subscribe
(
Subscriber
<?
super
String
>
subscriber
)
{
subscriber
.
onSubscribe
(
new
AbstractSubscription
<
String
>(
subscriber
)
{
@Override
protected
void
requestInternal
(
long
n
)
{
invokeOnNext
(
"Hello world."
);
invokeOnComplete
();
}
});
}
};
return
PublisherFactory
.
forEach
((
subscriber
)
->
{
subscriber
.
onNext
(
"Hello world."
);
subscriber
.
onComplete
();
});
}
}
...
...
@@ -113,40 +103,30 @@ public class DispatcherApp {
PlainTextHandler
textHandler
=
(
PlainTextHandler
)
handler
;
final
Publisher
<
String
>
resultPublisher
=
textHandler
.
handle
(
request
,
response
);
return
new
Publisher
<
HandlerResult
>()
{
@Override
public
void
subscribe
(
Subscriber
<?
super
HandlerResult
>
handlerResultSubscriber
)
{
handlerResultSubscriber
.
onSubscribe
(
new
AbstractSubscription
<
HandlerResult
>(
handlerResultSubscriber
)
{
@Override
protected
void
requestInternal
(
long
n
)
{
resultPublisher
.
subscribe
(
new
Subscriber
<
Object
>()
{
@Override
public
void
onSubscribe
(
Subscription
subscription
)
{
subscription
.
request
(
Long
.
MAX_VALUE
);
}
@Override
public
void
onNext
(
Object
result
)
{
invokeOnNext
(
new
HandlerResult
(
result
));
}
@Override
public
void
onError
(
Throwable
error
)
{
invokeOnError
(
error
);
}
@Override
public
void
onComplete
()
{
invokeOnComplete
();
}
});
}
});
}
};
return
PublisherFactory
.
forEach
((
subscriber
)
->
{
resultPublisher
.
subscribe
(
new
Subscriber
<
Object
>()
{
@Override
public
void
onSubscribe
(
Subscription
subscription
)
{
subscription
.
request
(
Long
.
MAX_VALUE
);
}
@Override
public
void
onNext
(
Object
result
)
{
subscriber
.
onNext
(
new
HandlerResult
(
result
));
}
@Override
public
void
onError
(
Throwable
error
)
{
subscriber
.
onError
(
error
);
}
@Override
public
void
onComplete
()
{
subscriber
.
onComplete
();
}
});
});
}
}
...
...
@@ -159,74 +139,13 @@ public class DispatcherApp {
}
@Override
public
Publisher
<
Void
>
handleResult
(
ServerHttpRequest
request
,
ServerHttpResponse
response
,
HandlerResult
result
)
{
public
Publisher
<
Void
>
handleResult
(
ServerHttpRequest
request
,
ServerHttpResponse
response
,
HandlerResult
result
)
{
response
.
getHeaders
().
setContentType
(
MediaType
.
TEXT_PLAIN
);
return
response
.
writeWith
(
new
Publisher
<
byte
[]>()
{
@Override
public
void
subscribe
(
Subscriber
<?
super
byte
[]>
writeSubscriber
)
{
writeSubscriber
.
onSubscribe
(
new
AbstractSubscription
<
byte
[]>(
writeSubscriber
)
{
@Override
protected
void
requestInternal
(
long
n
)
{
Charset
charset
=
Charset
.
forName
(
"UTF-8"
);
invokeOnNext
(((
String
)
result
.
getReturnValue
()).
getBytes
(
charset
));
invokeOnComplete
();
}
});
}
});
}
}
private
static
abstract
class
AbstractSubscription
<
T
>
implements
Subscription
{
private
final
Subscriber
<?
super
T
>
subscriber
;
private
volatile
boolean
terminated
;
public
AbstractSubscription
(
Subscriber
<?
super
T
>
subscriber
)
{
this
.
subscriber
=
subscriber
;
}
protected
boolean
isTerminated
()
{
return
this
.
terminated
;
}
@Override
public
void
request
(
long
n
)
{
if
(
isTerminated
())
{
return
;
}
if
(
n
>
0
)
{
requestInternal
(
n
);
}
}
protected
abstract
void
requestInternal
(
long
n
);
@Override
public
void
cancel
()
{
this
.
terminated
=
true
;
}
protected
void
invokeOnNext
(
T
data
)
{
this
.
subscriber
.
onNext
(
data
);
}
protected
void
invokeOnError
(
Throwable
error
)
{
this
.
terminated
=
true
;
this
.
subscriber
.
onError
(
error
);
}
protected
void
invokeOnComplete
()
{
this
.
terminated
=
true
;
this
.
subscriber
.
onComplete
();
return
response
.
writeWith
(
PublisherFactory
.
forEach
((
writeSubscriber
)
->
{
Charset
charset
=
Charset
.
forName
(
"UTF-8"
);
writeSubscriber
.
onNext
(((
String
)
result
.
getReturnValue
()).
getBytes
(
charset
));
writeSubscriber
.
onComplete
();
}));
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录