當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpResponse.streaming方法代碼示例

本文整理匯總了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);
}
 
開發者ID:line,項目名稱:armeria,代碼行數:28,代碼來源:DefaultHttpResponseTest.java

示例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());
}
 
開發者ID:line,項目名稱:armeria,代碼行數:27,代碼來源:ConcurrencyLimitingHttpClientTest.java

示例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());
}
 
開發者ID:line,項目名稱:armeria,代碼行數:24,代碼來源:ConcurrencyLimitingHttpClientTest.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:28,代碼來源:PooledResponseBufferBenchmark.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:37,代碼來源:JettyService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:9,代碼來源:AnnotatedHttpServiceExceptionHandlerTest.java

示例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());
}
 
開發者ID:line,項目名稱:armeria,代碼行數:41,代碼來源:ConcurrencyLimitingHttpClientTest.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:28,代碼來源:HttpClientIntegrationTest.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:11,代碼來源:AnnotatedHttpServiceTest.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:51,代碼來源:GrpcService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:10,代碼來源:AbstractHttpService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:10,代碼來源:AbstractHttpService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:10,代碼來源:AbstractHttpService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:10,代碼來源:AbstractHttpService.java

示例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;
}
 
開發者ID:line,項目名稱:armeria,代碼行數:10,代碼來源:AbstractHttpService.java


注:本文中的com.linecorp.armeria.common.HttpResponse.streaming方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。