本文整理汇总了Java中com.linecorp.armeria.common.HttpResponse.subscribe方法的典型用法代码示例。如果您正苦于以下问题:Java HttpResponse.subscribe方法的具体用法?Java HttpResponse.subscribe怎么用?Java HttpResponse.subscribe使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.linecorp.armeria.common.HttpResponse
的用法示例。
在下文中一共展示了HttpResponse.subscribe方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: shouldRetry
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public CompletableFuture<Optional<Backoff>> shouldRetry(HttpRequest request, HttpResponse response) {
final CompletableFuture<HttpHeaders> future = new CompletableFuture<>();
final HttpHeaderSubscriber subscriber = new HttpHeaderSubscriber(future);
response.completionFuture().whenComplete(subscriber);
response.subscribe(subscriber);
return future.handle((headers, unused) -> {
if (headers != null) {
final HttpStatus resStatus = headers.status();
if (resStatus != null) {
return statusBasedBackoffFunction.apply(resStatus);
}
}
return Optional.empty();
});
}
示例2: testFaultyDelegate
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Tests the case where a delegate raises an exception rather than returning a response.
*/
@Test
public void testFaultyDelegate() throws Exception {
final ClientRequestContext ctx = newContext();
final HttpRequest req = mock(HttpRequest.class);
@SuppressWarnings("unchecked")
final Client<HttpRequest, HttpResponse> delegate = mock(Client.class);
when(delegate.execute(ctx, req)).thenThrow(Exception.class);
final ConcurrencyLimitingHttpClient client =
ConcurrencyLimitingHttpClient.newDecorator(1).apply(delegate);
assertThat(client.numActiveRequests()).isZero();
final HttpResponse res = client.execute(ctx, req);
// Consume everything from the returned response so its close future is completed.
res.subscribe(NoopSubscriber.get());
assertThat(res.isOpen()).isFalse();
assertThatThrownBy(() -> res.completionFuture().get()).hasCauseInstanceOf(Exception.class);
await().untilAsserted(() -> assertThat(client.numActiveRequests()).isZero());
}
示例3: 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;
}
示例4: start
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
@Override
public void start(Listener<O> responseListener, Metadata unused) {
requireNonNull(responseListener, "responseListener");
final Compressor compressor;
if (callOptions.getCompressor() != null) {
compressor = compressorRegistry.lookupCompressor(callOptions.getCompressor());
if (compressor == null) {
responseListener.onClose(
Status.INTERNAL.withDescription(
"Unable to find compressor by name " + callOptions.getCompressor()),
EMPTY_METADATA);
return;
}
} else {
compressor = Identity.NONE;
}
messageFramer.setCompressor(compressor);
prepareHeaders(req.headers(), compressor);
listener = responseListener;
final HttpResponse res;
try {
res = httpClient.execute(ctx, req);
} catch (Exception e) {
close(Status.fromThrowable(e));
return;
}
res.subscribe(responseReader, ctx.eventLoop(), true);
}
示例5: getHttpHeaders
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
private static HttpHeaders getHttpHeaders(HttpResponse res) {
final CompletableFuture<HttpHeaders> future = new CompletableFuture<>();
final HttpHeaderSubscriber subscriber = new HttpHeaderSubscriber(future);
res.completionFuture().whenComplete(subscriber);
res.subscribe(subscriber);
// Neither blocks here nor throws an exception because it already has headers.
return future.join();
}
示例6: 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());
}
示例7: closeAndDrain
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
/**
* Closes the response returned by the delegate and consumes everything from it, so that its close future
* is completed.
*/
private static void closeAndDrain(HttpResponseWriter actualRes, HttpResponse deferredRes) {
actualRes.close();
deferredRes.subscribe(NoopSubscriber.get());
deferredRes.completionFuture().join();
waitForEventLoop();
}
示例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: runStreamingResponseTest
import com.linecorp.armeria.common.HttpResponse; //导入方法依赖的package包/类
private void runStreamingResponseTest(boolean slowClient) throws InterruptedException, ExecutionException {
// Disable timeouts and length limits so that test does not fail due to slow transfer.
clientWriteTimeoutMillis = 0;
clientResponseTimeoutMillis = 0;
clientMaxResponseLength = 0;
serverRequestTimeoutMillis = 0;
final HttpResponse res = client().get("/zeroes/" + STREAMING_CONTENT_LENGTH);
final AtomicReference<HttpStatus> status = new AtomicReference<>();
final StreamConsumer consumer = new StreamConsumer(GlobalEventExecutor.INSTANCE, slowClient) {
@Override
public void onNext(HttpObject obj) {
if (obj instanceof HttpHeaders) {
status.compareAndSet(null, ((HttpHeaders) obj).status());
}
super.onNext(obj);
}
@Override
public void onError(Throwable cause) {
// Will be notified via the 'awaitClose().get()' below.
}
@Override
public void onComplete() {}
};
res.subscribe(consumer);
res.completionFuture().get();
assertThat(status.get(), is(HttpStatus.OK));
assertThat(consumer.numReceivedBytes(), is(STREAMING_CONTENT_LENGTH));
}