本文整理汇总了Java中com.linecorp.armeria.common.HttpResponse.streaming方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.streaming方法的具体用法?Java HttpResponse.streaming怎么用?Java HttpResponse.streaming使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.linecorp.armeria.common.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.streaming方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: abortedAggregation
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* The aggregation future must be completed even if the response being aggregated has been aborted.
*/
@Test
public void abortedAggregation() {
final Thread mainThread = Thread.currentThread();
final HttpResponseWriter res = HttpResponse.streaming();
final CompletableFuture<AggregatedHttpMessage> future;
// Practically same execution, but we need to test the both case due to code duplication.
if (executorSpecified) {
future = res.aggregate(CommonPools.workerGroup().next());
} else {
future = res.aggregate();
}
final AtomicReference<Thread> callbackThread = new AtomicReference<>();
assertThatThrownBy(() -> {
final CompletableFuture<AggregatedHttpMessage> f =
future.whenComplete((unused, cause) -> callbackThread.set(Thread.currentThread()));
res.abort();
f.join();
}).hasCauseInstanceOf(AbortedStreamException.class);
assertThat(callbackThread.get()).isNotSameAs(mainThread);
}
示例2: testOrdinaryRequest
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Tests the request pattern that does not exceed maxConcurrency.
*/
@Test
public void testOrdinaryRequest() throws Exception {
final ClientRequestContext ctx = newContext();
final HttpRequest req = mock(HttpRequest.class);
final HttpResponseWriter actualRes = HttpResponse.streaming();
@SuppressWarnings("unchecked")
final Client<HttpRequest, HttpResponse> delegate = mock(Client.class);
when(delegate.execute(ctx, req)).thenReturn(actualRes);
final ConcurrencyLimitingHttpClient client =
newDecorator(1).apply(delegate);
assertThat(client.numActiveRequests()).isZero();
final HttpResponse res = client.execute(ctx, req);
assertThat(res.isOpen()).isTrue();
assertThat(client.numActiveRequests()).isEqualTo(1);
closeAndDrain(actualRes, res);
assertThat(res.isOpen()).isFalse();
await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
示例3: testUnlimitedRequest
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Test
public void testUnlimitedRequest() throws Exception {
final ClientRequestContext ctx = newContext();
final HttpRequest req = mock(HttpRequest.class);
final HttpResponseWriter actualRes = HttpResponse.streaming();
@SuppressWarnings("unchecked")
final Client<HttpRequest, HttpResponse> delegate = mock(Client.class);
when(delegate.execute(ctx, req)).thenReturn(actualRes);
final ConcurrencyLimitingHttpClient client =
newDecorator(0).apply(delegate);
// A request should be delegated immediately, creating no deferred response.
final HttpResponse res = client.execute(ctx, req);
verify(delegate).execute(ctx, req);
assertThat(res.isOpen()).isTrue();
assertThat(client.numActiveRequests()).isEqualTo(1);
// Complete the response, leaving no active requests.
closeAndDrain(actualRes, res);
await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
示例4: serve
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponse res = delegate().serve(ctx, req);
HttpResponseWriter decorated = HttpResponse.streaming();
res.subscribe(new Subscriber<HttpObject>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(HttpObject httpObject) {
decorated.write(httpObject);
}
@Override
public void onError(Throwable t) {
decorated.close(t);
}
@Override
public void onComplete() {
decorated.close();
}
}, true);
return decorated;
}
示例5: serve
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
final ArmeriaConnector connector = this.connector;
final HttpResponseWriter res = HttpResponse.streaming();
req.aggregate().handle(voidFunction((aReq, cause) -> {
if (cause != null) {
logger.warn("{} Failed to aggregate a request:", ctx, cause);
res.close(HttpHeaders.of(HttpStatus.INTERNAL_SERVER_ERROR));
return;
}
boolean success = false;
try {
final ArmeriaHttpTransport transport = new ArmeriaHttpTransport(req.method());
final HttpChannel httpChannel = new HttpChannel(
connector,
connector.getHttpConfiguration(),
new ArmeriaEndPoint(hostname, connector.getScheduler(),
ctx.localAddress(), ctx.remoteAddress()),
transport);
fillRequest(ctx, aReq, httpChannel.getRequest());
ctx.blockingTaskExecutor().execute(() -> invoke(ctx, res, transport, httpChannel));
success = true;
} finally {
if (!success) {
res.close();
}
}
})).exceptionally(CompletionActions::log);
return res;
}
示例6: handleException
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse handleException(RequestContext ctx, HttpRequest req, Throwable cause) {
final HttpResponseWriter response = HttpResponse.streaming();
response.write(HttpHeaders.of(HttpStatus.OK));
// Timeout may occur before responding.
ctx.eventLoop().schedule((Runnable) response::close, 10, TimeUnit.SECONDS);
return response;
}
示例7: testTimeout
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Tests if the request is not delegated but closed when the timeout is reached before delegation.
*/
@Test
public void testTimeout() throws Exception {
final ClientRequestContext ctx1 = newContext();
final ClientRequestContext ctx2 = newContext();
final HttpRequest req1 = mock(HttpRequest.class);
final HttpRequest req2 = mock(HttpRequest.class);
final HttpResponseWriter actualRes1 = HttpResponse.streaming();
final HttpResponseWriter actualRes2 = HttpResponse.streaming();
@SuppressWarnings("unchecked")
final Client<HttpRequest, HttpResponse> delegate = mock(Client.class);
when(delegate.execute(ctx1, req1)).thenReturn(actualRes1);
when(delegate.execute(ctx2, req2)).thenReturn(actualRes2);
final ConcurrencyLimitingHttpClient client =
newDecorator(1, 500, TimeUnit.MILLISECONDS).apply(delegate);
// Send two requests, where only the first one is delegated.
final HttpResponse res1 = client.execute(ctx1, req1);
final HttpResponse res2 = client.execute(ctx2, req2);
// Let req2 time out.
Thread.sleep(1000);
res2.subscribe(NoopSubscriber.get());
assertThatThrownBy(() -> res2.completionFuture().join())
.hasCauseInstanceOf(ResponseTimeoutException.class);
assertThat(res2.isOpen()).isFalse();
// req1 should not time out because it's been delegated already.
res1.subscribe(NoopSubscriber.get());
assertThat(res1.isOpen()).isTrue();
assertThat(res1.completionFuture()).isNotDone();
// Close req1 and make sure req2 does not affect numActiveRequests.
actualRes1.close();
await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
示例8: serve
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponse res = delegate().serve(ctx, req);
HttpResponseWriter decorated = HttpResponse.streaming();
res.subscribe(new Subscriber<HttpObject>() {
@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
}
@Override
public void onNext(HttpObject httpObject) {
decorated.write(httpObject);
}
@Override
public void onError(Throwable t) {
decorated.close(t);
}
@Override
public void onComplete() {
decorated.close();
}
});
return decorated;
}
示例9: postStringAsync2
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Post
@Path("/a/string-async2")
public HttpResponse postStringAsync2(AggregatedHttpMessage message, RequestContext ctx) {
validateContext(ctx);
HttpResponseWriter response = HttpResponse.streaming();
response.write(HttpHeaders.of(HttpStatus.OK));
response.write(message.content());
response.close();
return response;
}
示例10: doPost
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception {
MediaType contentType = req.headers().contentType();
SerializationFormat serializationFormat = findSerializationFormat(contentType);
if (serializationFormat == null) {
return HttpResponse.of(HttpStatus.UNSUPPORTED_MEDIA_TYPE,
MediaType.PLAIN_TEXT_UTF_8,
"Missing or invalid Content-Type header.");
}
ctx.logBuilder().serializationFormat(serializationFormat);
String methodName = GrpcRequestUtil.determineMethod(ctx);
if (methodName == null) {
return HttpResponse.of(HttpStatus.BAD_REQUEST,
MediaType.PLAIN_TEXT_UTF_8,
"Invalid path.");
}
ServerMethodDefinition<?, ?> method = registry.lookupMethod(methodName);
if (method == null) {
return HttpResponse.of(
ArmeriaServerCall.statusToTrailers(
Status.UNIMPLEMENTED.withDescription("Method not found: " + methodName),
false));
}
ctx.logBuilder().requestContent(GrpcLogUtil.rpcRequest(method.getMethodDescriptor()), null);
String timeoutHeader = req.headers().get(GrpcHeaderNames.GRPC_TIMEOUT);
if (timeoutHeader != null) {
try {
long timeout = TimeoutHeaderUtil.fromHeaderValue(timeoutHeader);
ctx.setRequestTimeout(Duration.ofNanos(timeout));
} catch (IllegalArgumentException e) {
return HttpResponse.of(ArmeriaServerCall.statusToTrailers(Status.fromThrowable(e), false));
}
}
HttpResponseWriter res = HttpResponse.streaming();
ArmeriaServerCall<?, ?> call = startCall(
methodName, method, ctx, req.headers(), res, serializationFormat);
if (call != null) {
ctx.setRequestTimeoutHandler(() -> {
call.close(Status.DEADLINE_EXCEEDED, EMPTY_METADATA);
});
req.subscribe(call.messageReader(), ctx.eventLoop(), true);
}
return res;
}
示例11: doOptions
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles an {@link HttpMethod#OPTIONS OPTIONS} request.
* This method sends a {@link HttpStatus#METHOD_NOT_ALLOWED 405 Method Not Allowed} response by default.
*/
protected HttpResponse doOptions(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponseWriter res = HttpResponse.streaming();
doOptions(ctx, req, res);
return res;
}
示例12: doGet
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles a {@link HttpMethod#GET GET} request.
* This method sends a {@link HttpStatus#METHOD_NOT_ALLOWED 405 Method Not Allowed} response by default.
*/
protected HttpResponse doGet(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponseWriter res = HttpResponse.streaming();
doGet(ctx, req, res);
return res;
}
示例13: doPost
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles a {@link HttpMethod#POST POST} request.
* This method sends a {@link HttpStatus#METHOD_NOT_ALLOWED 405 Method Not Allowed} response by default.
*/
protected HttpResponse doPost(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponseWriter res = HttpResponse.streaming();
doPost(ctx, req, res);
return res;
}
示例14: doPut
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles a {@link HttpMethod#PUT PUT} request.
* This method sends a {@link HttpStatus#METHOD_NOT_ALLOWED 405 Method Not Allowed} response by default.
*/
protected HttpResponse doPut(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponseWriter res = HttpResponse.streaming();
doPut(ctx, req, res);
return res;
}
示例15: doPatch
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Handles a {@link HttpMethod#PATCH PATCH} request.
* This method sends a {@link HttpStatus#METHOD_NOT_ALLOWED 405 Method Not Allowed} response by default.
*/
protected HttpResponse doPatch(ServiceRequestContext ctx, HttpRequest req) throws Exception {
HttpResponseWriter res = HttpResponse.streaming();
doPatch(ctx, req, res);
return res;
}