Java 类org.springframework.http.client.Netty4ClientHttpRequestFactory 实例源码

项目:spring-credhub    文件:ClientHttpRequestFactoryFactory.java   
static ClientHttpRequestFactory usingNetty(ClientOptions options)
        throws IOException, GeneralSecurityException {

    SslContext sslContext = new JdkSslContext(SSLContext.getDefault(), true, ClientAuth.REQUIRE);

    final Netty4ClientHttpRequestFactory requestFactory = new Netty4ClientHttpRequestFactory();
    requestFactory.setSslContext(sslContext);

    if (options.getConnectionTimeout() != null) {
        requestFactory.setConnectTimeout(options.getConnectionTimeout());
    }
    if (options.getReadTimeout() != null) {
        requestFactory.setReadTimeout(options.getReadTimeout());
    }

    return requestFactory;
}
项目:spring-credhub    文件:ClientHttpRequestFactoryFactoryTests.java   
@Test
public void nettyClientCreated() throws Exception {
    ClientHttpRequestFactory factory = usingNetty(new ClientOptions());

    assertThat(factory, instanceOf(Netty4ClientHttpRequestFactory.class));

    ((DisposableBean) factory).destroy();
}
项目:jcurl    文件:NNIOEngine.java   
@Override
    public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
        int ioWorkerCount = Runtime.getRuntime().availableProcessors() * 2;
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup(ioWorkerCount);

        try {
            final Netty4ClientHttpRequestFactory netty4ClientHttpRequestFactory = new Netty4ClientHttpRequestFactory(eventLoopGroup);
            netty4ClientHttpRequestFactory.setConnectTimeout(2000);
            netty4ClientHttpRequestFactory.setReadTimeout(2000);
/*
            SslContext sslContext = SslContextBuilder
                .forClient()
                .sslProvider(SslProvider.JDK)
                .build()
            ;
*/
            if (requestOptions.getUrl().toLowerCase().startsWith("https://")) {
                SslContext sslContext = new DefaultClientSslContext();
                netty4ClientHttpRequestFactory.setSslContext(sslContext);
            }
            netty4ClientHttpRequestFactory.afterPropertiesSet();

            ResponseEntity<String> stringResponseEntity = null;
            for (int i = 0; i < requestOptions.getCount(); i++) {
                final HttpHeaders headers = new HttpHeaders();
                for(Map.Entry<String,String> e : requestOptions.getHeaderMap().entrySet()) {
                    headers.put(e.getKey(), Collections.singletonList(e.getValue()));
                }

                final HttpEntity<Void> requestEntity = new HttpEntity<>(headers);

                AsyncRestTemplate template = new AsyncRestTemplate(netty4ClientHttpRequestFactory);
                final ListenableFuture<ResponseEntity<String>> exchange = template.exchange(requestOptions.getUrl(), HttpMethod.GET, requestEntity, String.class);
                stringResponseEntity = exchange.get();
                System.out.println(stringResponseEntity.getBody());
            }
            return stringResponseEntity;
        } finally {
            eventLoopGroup.shutdownGracefully(100, 500, TimeUnit.MILLISECONDS);
        }
    }
项目:spring-vault    文件:ClientHttpRequestFactoryFactory.java   
static ClientHttpRequestFactory usingNetty(ClientOptions options,
        SslConfiguration sslConfiguration) throws GeneralSecurityException,
        IOException {

    final Netty4ClientHttpRequestFactory requestFactory = new Netty4ClientHttpRequestFactory();

    if (hasSslConfiguration(sslConfiguration)) {

        SslContextBuilder sslContextBuilder = SslContextBuilder //
                .forClient();

        if (sslConfiguration.getTrustStoreConfiguration().isPresent()) {
            sslContextBuilder
                    .trustManager(createTrustManagerFactory(sslConfiguration
                            .getTrustStoreConfiguration()));
        }

        if (sslConfiguration.getKeyStoreConfiguration().isPresent()) {
            sslContextBuilder.keyManager(createKeyManagerFactory(sslConfiguration
                    .getKeyStoreConfiguration()));
        }

        requestFactory.setSslContext(sslContextBuilder.sslProvider(
                SslProvider.JDK).build());
    }

    requestFactory.setConnectTimeout(Math.toIntExact(options
            .getConnectionTimeout().toMillis()));
    requestFactory.setReadTimeout(Math.toIntExact(options.getReadTimeout()
            .toMillis()));

    return requestFactory;
}
项目:spring-vault    文件:ClientHttpRequestFactoryFactoryIntegrationTests.java   
@Test
public void nettyClientShouldWork() throws Exception {

    ClientHttpRequestFactory factory = Netty.usingNetty(new ClientOptions(),
            Settings.createSslConfiguration());
    ((InitializingBean) factory).afterPropertiesSet();
    RestTemplate template = new RestTemplate(factory);

    String response = request(template);

    assertThat(factory).isInstanceOf(Netty4ClientHttpRequestFactory.class);
    assertThat(response).isNotNull().contains("initialized");

    ((DisposableBean) factory).destroy();
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:RestTemplateBuilderTests.java   
@Test
public void connectTimeoutCanBeConfiguredOnNetty4RequestFactory() {
    ClientHttpRequestFactory requestFactory = this.builder
            .requestFactory(Netty4ClientHttpRequestFactory.class)
            .setConnectTimeout(1234).build().getRequestFactory();
    assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout"))
            .isEqualTo(1234);
}
项目:https-github.com-g0t4-jenkins2-course-spring-boot    文件:RestTemplateBuilderTests.java   
@Test
public void readTimeoutCanBeConfiguredOnNetty4RequestFactory() {
    ClientHttpRequestFactory requestFactory = this.builder
            .requestFactory(Netty4ClientHttpRequestFactory.class).setReadTimeout(1234)
            .build().getRequestFactory();
    assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout"))
            .isEqualTo(1234);
}
项目:pinpoint    文件:RestTemplateIT.java   
@Test
public void test3() throws Exception {
    RestTemplate restTemplate = new RestTemplate(new Netty4ClientHttpRequestFactory());
    String forObject = restTemplate.getForObject(webServer.getCallHttpUrl(), String.class);

    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();

    verifier.verifyTrace(event("REST_TEMPLATE", RestTemplate.class.getConstructor()));
    verifier.verifyTrace(event("REST_TEMPLATE", "org.springframework.http.client.AbstractAsyncClientHttpRequest.executeAsync()"));
    verifier.verifyTrace(event("REST_TEMPLATE", "RestTemplate execAsync Result Invocation"));
    verifier.verifyTrace(event("ASYNC", "Asynchronous Invocation"));
    verifier.verifyTrace(event("REST_TEMPLATE", "org.springframework.util.concurrent.SettableListenableFuture.set(java.lang.Object)", annotation("http.status.code", 200)));
}
项目:reactive.loanbroker.system    文件:Application.java   
@Bean
public AsyncRestTemplate restTemplate(){
    return new AsyncRestTemplate(new Netty4ClientHttpRequestFactory());
}