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; }
@Test public void nettyClientCreated() throws Exception { ClientHttpRequestFactory factory = usingNetty(new ClientOptions()); assertThat(factory, instanceOf(Netty4ClientHttpRequestFactory.class)); ((DisposableBean) factory).destroy(); }
@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); } }
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; }
@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(); }
@Test public void connectTimeoutCanBeConfiguredOnNetty4RequestFactory() { ClientHttpRequestFactory requestFactory = this.builder .requestFactory(Netty4ClientHttpRequestFactory.class) .setConnectTimeout(1234).build().getRequestFactory(); assertThat(ReflectionTestUtils.getField(requestFactory, "connectTimeout")) .isEqualTo(1234); }
@Test public void readTimeoutCanBeConfiguredOnNetty4RequestFactory() { ClientHttpRequestFactory requestFactory = this.builder .requestFactory(Netty4ClientHttpRequestFactory.class).setReadTimeout(1234) .build().getRequestFactory(); assertThat(ReflectionTestUtils.getField(requestFactory, "readTimeout")) .isEqualTo(1234); }
@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))); }
@Bean public AsyncRestTemplate restTemplate(){ return new AsyncRestTemplate(new Netty4ClientHttpRequestFactory()); }