Browse Source
This commit add flushing support thanks to the FlushingDataBuffer wrapper that allows to identify the elements that should trigger a flush.pull/1111/head
7 changed files with 263 additions and 21 deletions
@ -0,0 +1,120 @@
@@ -0,0 +1,120 @@
|
||||
/* |
||||
* Copyright 2002-2016 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.core.io.buffer; |
||||
|
||||
import java.io.InputStream; |
||||
import java.io.OutputStream; |
||||
import java.nio.ByteBuffer; |
||||
import java.util.function.IntPredicate; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* {@link DataBuffer} wrapper that indicates the file or the socket writing this buffer |
||||
* should be flushed. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
public class FlushingDataBuffer implements DataBuffer { |
||||
|
||||
private final DataBuffer buffer; |
||||
|
||||
public FlushingDataBuffer(DataBuffer buffer) { |
||||
Assert.notNull(buffer); |
||||
this.buffer = buffer; |
||||
} |
||||
|
||||
@Override |
||||
public DataBufferFactory factory() { |
||||
return this.buffer.factory(); |
||||
} |
||||
|
||||
@Override |
||||
public int indexOf(IntPredicate predicate, int fromIndex) { |
||||
return this.buffer.indexOf(predicate, fromIndex); |
||||
} |
||||
|
||||
@Override |
||||
public int lastIndexOf(IntPredicate predicate, int fromIndex) { |
||||
return this.buffer.lastIndexOf(predicate, fromIndex); |
||||
} |
||||
|
||||
@Override |
||||
public int readableByteCount() { |
||||
return this.buffer.readableByteCount(); |
||||
} |
||||
|
||||
@Override |
||||
public byte read() { |
||||
return this.buffer.read(); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer read(byte[] destination) { |
||||
return this.buffer.read(destination); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer read(byte[] destination, int offset, int length) { |
||||
return this.buffer.read(destination, offset, length); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer write(byte b) { |
||||
return this.buffer.write(b); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer write(byte[] source) { |
||||
return this.buffer.write(source); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer write(byte[] source, int offset, int length) { |
||||
return this.write(source, offset, length); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer write(DataBuffer... buffers) { |
||||
return this.buffer.write(buffers); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer write(ByteBuffer... buffers) { |
||||
return this.buffer.write(buffers); |
||||
} |
||||
|
||||
@Override |
||||
public DataBuffer slice(int index, int length) { |
||||
return this.buffer.slice(index, length); |
||||
} |
||||
|
||||
@Override |
||||
public ByteBuffer asByteBuffer() { |
||||
return this.buffer.asByteBuffer(); |
||||
} |
||||
|
||||
@Override |
||||
public InputStream asInputStream() { |
||||
return this.buffer.asInputStream(); |
||||
} |
||||
|
||||
@Override |
||||
public OutputStream asOutputStream() { |
||||
return this.buffer.asOutputStream(); |
||||
} |
||||
} |
||||
@ -0,0 +1,82 @@
@@ -0,0 +1,82 @@
|
||||
/* |
||||
* Copyright 2002-2016 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.http.server.reactive; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import static org.springframework.web.client.reactive.HttpRequestBuilders.get; |
||||
import static org.springframework.web.client.reactive.WebResponseExtractors.bodyStream; |
||||
import reactor.core.publisher.Flux; |
||||
import reactor.core.publisher.Mono; |
||||
import reactor.core.test.TestSubscriber; |
||||
|
||||
import org.springframework.core.io.buffer.DataBuffer; |
||||
import org.springframework.core.io.buffer.FlushingDataBuffer; |
||||
import org.springframework.http.client.reactive.ReactorHttpClientRequestFactory; |
||||
import org.springframework.web.client.reactive.WebClient; |
||||
|
||||
/** |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
public class FlushingIntegrationTests extends AbstractHttpHandlerIntegrationTests { |
||||
|
||||
private WebClient webClient; |
||||
|
||||
@Before |
||||
public void setup() throws Exception { |
||||
super.setup(); |
||||
this.webClient = new WebClient(new ReactorHttpClientRequestFactory()); |
||||
} |
||||
|
||||
@Test |
||||
public void testFlushing() throws Exception { |
||||
Mono<String> result = this.webClient |
||||
.perform(get("http://localhost:" + port)) |
||||
.extract(bodyStream(String.class)) |
||||
.take(2) |
||||
.reduce((s1, s2) -> s1 + s2); |
||||
|
||||
TestSubscriber |
||||
.subscribe(result) |
||||
.await() |
||||
.assertValues("data0data1"); |
||||
} |
||||
|
||||
|
||||
@Override |
||||
protected HttpHandler createHttpHandler() { |
||||
return new FlushingHandler(); |
||||
} |
||||
|
||||
private static class FlushingHandler implements HttpHandler { |
||||
|
||||
@Override |
||||
public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { |
||||
Flux<DataBuffer> responseBody = Flux |
||||
.interval(50) |
||||
.take(2) |
||||
.concatWith(Flux.never()) |
||||
.map(l -> { |
||||
byte[] data = ("data" + l).getBytes(); |
||||
DataBuffer buffer = response.bufferFactory().allocateBuffer(data.length); |
||||
buffer.write(data); |
||||
return new FlushingDataBuffer(buffer); |
||||
}); |
||||
return response.writeWith(responseBody); |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue