From 11f6472e8948cbfd25149f54be376a86762ef60d Mon Sep 17 00:00:00 2001 From: John Thompson Date: Tue, 23 Apr 2019 22:13:33 -0400 Subject: [PATCH 1/8] adding get client --- pom.xml | 4 +-- .../web/client/BreweryClient.java | 34 +++++++++++++++++++ src/main/resources/application.properties | 2 +- .../web/client/BreweryClientTest.java | 25 ++++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java create mode 100644 src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java diff --git a/pom.xml b/pom.xml index ad64a1e5..b4cd4dfe 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.2.0.M2 + 2.1.4.RELEASE guru.springframework @@ -70,7 +70,7 @@ maven-surefire-plugin 3.0.0-M3 - + org.apache.maven.plugins maven-enforcer-plugin diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java new file mode 100644 index 00000000..6936f014 --- /dev/null +++ b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java @@ -0,0 +1,34 @@ +package guru.springframework.msscbreweryclient.web.client; + +import guru.springframework.msscbreweryclient.web.model.BeerDto; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +import java.util.UUID; + +/** + * Created by jt on 2019-04-23. + */ +@ConfigurationProperties(prefix = "sfg.brewery", ignoreUnknownFields = false) +@Component +public class BreweryClient { + + public final String BEER_PATH_V1 = "/api/v1/beer/"; + private String apihost; + + private final RestTemplate restTemplate; + + public BreweryClient(RestTemplateBuilder restTemplateBuilder) { + this.restTemplate = restTemplateBuilder.build(); + } + + public BeerDto getBeerById(UUID uuid){ + return restTemplate.getForObject(apihost + BEER_PATH_V1 + uuid.toString(), BeerDto.class); + } + + public void setApihost(String apihost) { + this.apihost = apihost; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b137891..633dfbaa 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1 @@ - +sfg.brewery.apihost=http://localhost:8080 diff --git a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java new file mode 100644 index 00000000..a8b32680 --- /dev/null +++ b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java @@ -0,0 +1,25 @@ +package guru.springframework.msscbreweryclient.web.client; + +import guru.springframework.msscbreweryclient.web.model.BeerDto; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +class BreweryClientTest { + + @Autowired + BreweryClient client; + + @Test + void getBeerById() { + BeerDto dto = client.getBeerById(UUID.randomUUID()); + + assertNotNull(dto); + + } +} \ No newline at end of file From cc4d3fbe043065448b201c68902948add5df21a1 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 25 Apr 2019 18:36:54 -0400 Subject: [PATCH 2/8] adding POST example --- .../web/client/BreweryClient.java | 5 +++++ .../web/client/BreweryClientTest.java | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java index 6936f014..1967f39c 100644 --- a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java +++ b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java @@ -6,6 +6,7 @@ import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; +import java.net.URI; import java.util.UUID; /** @@ -28,6 +29,10 @@ public BeerDto getBeerById(UUID uuid){ return restTemplate.getForObject(apihost + BEER_PATH_V1 + uuid.toString(), BeerDto.class); } + public URI saveNewBeer(BeerDto beerDto){ + return restTemplate.postForLocation(apihost + BEER_PATH_V1, beerDto); + } + public void setApihost(String apihost) { this.apihost = apihost; } diff --git a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java index a8b32680..5bde84d3 100644 --- a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java +++ b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java @@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; +import java.net.URI; import java.util.UUID; import static org.junit.jupiter.api.Assertions.*; @@ -22,4 +23,17 @@ void getBeerById() { assertNotNull(dto); } + + @Test + void testSaveNewBeer() { + //given + BeerDto beerDto = BeerDto.builder().beerName("New Beer").build(); + + URI uri = client.saveNewBeer(beerDto); + + assertNotNull(uri); + + System.out.println(uri.toString()); + + } } \ No newline at end of file From 8358ffbf4227c74f88764f46ec5b715a9fb50e4e Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 25 Apr 2019 18:51:06 -0400 Subject: [PATCH 3/8] adding PUT example --- .../msscbreweryclient/web/client/BreweryClient.java | 4 ++++ .../msscbreweryclient/web/client/BreweryClientTest.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java index 1967f39c..e105c0b6 100644 --- a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java +++ b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java @@ -33,6 +33,10 @@ public URI saveNewBeer(BeerDto beerDto){ return restTemplate.postForLocation(apihost + BEER_PATH_V1, beerDto); } + public void updateBeer(UUID uuid, BeerDto beerDto){ + restTemplate.put(apihost + BEER_PATH_V1 + "/" + uuid.toString(), beerDto); + } + public void setApihost(String apihost) { this.apihost = apihost; } diff --git a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java index 5bde84d3..4cd3ffef 100644 --- a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java +++ b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java @@ -36,4 +36,13 @@ void testSaveNewBeer() { System.out.println(uri.toString()); } + + @Test + void testUpdateBeer() { + //given + BeerDto beerDto = BeerDto.builder().beerName("New Beer").build(); + + client.updateBeer(UUID.randomUUID(), beerDto); + + } } \ No newline at end of file From 03b55e32a2c8e537ae182ca938e5746ddff2b368 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 25 Apr 2019 19:04:29 -0400 Subject: [PATCH 4/8] adding DELETE example --- .../msscbreweryclient/web/client/BreweryClient.java | 4 ++++ .../msscbreweryclient/web/client/BreweryClientTest.java | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java index e105c0b6..c567a0af 100644 --- a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java +++ b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java @@ -37,6 +37,10 @@ public void updateBeer(UUID uuid, BeerDto beerDto){ restTemplate.put(apihost + BEER_PATH_V1 + "/" + uuid.toString(), beerDto); } + public void deleteBeer(UUID uuid){ + restTemplate.delete(apihost + BEER_PATH_V1 + "/" + uuid ); + } + public void setApihost(String apihost) { this.apihost = apihost; } diff --git a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java index 4cd3ffef..ac6eef8f 100644 --- a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java +++ b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java @@ -45,4 +45,9 @@ void testUpdateBeer() { client.updateBeer(UUID.randomUUID(), beerDto); } + + @Test + void testDeleteBeer() { + client.deleteBeer(UUID.randomUUID()); + } } \ No newline at end of file From 2cd18eddeb3a8041165518ca177bf6bb517f355c Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 16 May 2019 16:52:10 -0400 Subject: [PATCH 5/8] adding assignment code --- .../web/client/BreweryClient.java | 22 ++++++++++- .../web/client/BreweryClientTest.java | 38 ++++++++++++++++++- 2 files changed, 57 insertions(+), 3 deletions(-) diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java index c567a0af..706de81b 100644 --- a/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java +++ b/src/main/java/guru/springframework/msscbreweryclient/web/client/BreweryClient.java @@ -1,6 +1,7 @@ package guru.springframework.msscbreweryclient.web.client; import guru.springframework.msscbreweryclient.web.model.BeerDto; +import guru.springframework.msscbreweryclient.web.model.CustomerDto; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.stereotype.Component; @@ -17,6 +18,7 @@ public class BreweryClient { public final String BEER_PATH_V1 = "/api/v1/beer/"; + public final String CUSTOMER_PATH_V1 = "/api/v1/customer/"; private String apihost; private final RestTemplate restTemplate; @@ -34,14 +36,30 @@ public URI saveNewBeer(BeerDto beerDto){ } public void updateBeer(UUID uuid, BeerDto beerDto){ - restTemplate.put(apihost + BEER_PATH_V1 + "/" + uuid.toString(), beerDto); + restTemplate.put(apihost + BEER_PATH_V1 + uuid, beerDto); } public void deleteBeer(UUID uuid){ - restTemplate.delete(apihost + BEER_PATH_V1 + "/" + uuid ); + restTemplate.delete(apihost + BEER_PATH_V1 + uuid ); } public void setApihost(String apihost) { this.apihost = apihost; } + + public CustomerDto getCustomerById(UUID customerId) { + return restTemplate.getForObject(apihost+ CUSTOMER_PATH_V1 + customerId.toString(), CustomerDto.class); + } + + public URI saveNewCustomer(CustomerDto customerDto) { + return restTemplate.postForLocation(apihost + CUSTOMER_PATH_V1, customerDto); + } + + public void updateCustomer(UUID customerId, CustomerDto customerDto) { + restTemplate.put(apihost + CUSTOMER_PATH_V1 + customerId, customerDto); + } + + public void deleteCustomer(UUID customerId) { + restTemplate.delete(apihost + CUSTOMER_PATH_V1 + customerId); + } } diff --git a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java index ac6eef8f..93773f1d 100644 --- a/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java +++ b/src/test/java/guru/springframework/msscbreweryclient/web/client/BreweryClientTest.java @@ -1,6 +1,7 @@ package guru.springframework.msscbreweryclient.web.client; import guru.springframework.msscbreweryclient.web.model.BeerDto; +import guru.springframework.msscbreweryclient.web.model.CustomerDto; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -8,7 +9,7 @@ import java.net.URI; import java.util.UUID; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertNotNull; @SpringBootTest class BreweryClientTest { @@ -50,4 +51,39 @@ void testUpdateBeer() { void testDeleteBeer() { client.deleteBeer(UUID.randomUUID()); } + + @Test + void getCustomerById() { + CustomerDto dto = client.getCustomerById(UUID.randomUUID()); + + assertNotNull(dto); + + } + + @Test + void testSaveNewCustomer() { + //given + CustomerDto customerDto = CustomerDto.builder().name("Joe").build(); + + URI uri = client.saveNewCustomer(customerDto); + + assertNotNull(uri); + + System.out.println(uri.toString()); + + } + + @Test + void testUpdateCustomer() { + //given + CustomerDto customerDto = CustomerDto.builder().name("Jim").build(); + + client.updateCustomer(UUID.randomUUID(), customerDto); + + } + + @Test + void testDeleteCustomer() { + client.deleteCustomer(UUID.randomUUID()); + } } \ No newline at end of file From 4554e3607c4f224c5332620828cd0b8e631cd1b2 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 8 Aug 2019 17:58:15 -0400 Subject: [PATCH 6/8] adding Apache HTTP Client --- pom.xml | 5 ++ .../BlockingRestTemplateCustomizer.java | 45 ++++++++++++++++++ .../web/config/NIORestTemplateCustomizer.java | 47 +++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java create mode 100644 src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java diff --git a/pom.xml b/pom.xml index b4cd4dfe..786c79a4 100644 --- a/pom.xml +++ b/pom.xml @@ -34,6 +34,11 @@ lombok true + + org.apache.httpcomponents + httpasyncclient + 4.1.4 + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java b/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java new file mode 100644 index 00000000..9f196319 --- /dev/null +++ b/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java @@ -0,0 +1,45 @@ +package guru.springframework.msscbreweryclient.web.config; + +import org.apache.http.client.config.RequestConfig; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.springframework.boot.web.client.RestTemplateCustomizer; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; +import org.springframework.stereotype.Component; +import org.springframework.web.client.RestTemplate; + +/** + * Created by jt on 2019-08-08. + */ +@Component +public class BlockingRestTemplateCustomizer implements RestTemplateCustomizer { + + public ClientHttpRequestFactory clientHttpRequestFactory(){ + PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); + connectionManager.setMaxTotal(100); + connectionManager.setDefaultMaxPerRoute(20); + + RequestConfig requestConfig = RequestConfig + .custom() + .setConnectionRequestTimeout(3000) + .setSocketTimeout(3000) + .build(); + + CloseableHttpClient httpClient = HttpClients + .custom() + .setConnectionManager(connectionManager) + .setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy()) + .setDefaultRequestConfig(requestConfig) + .build(); + + return new HttpComponentsClientHttpRequestFactory(httpClient); + } + + @Override + public void customize(RestTemplate restTemplate) { + restTemplate.setRequestFactory(this.clientHttpRequestFactory()); + } +} diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java b/src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java new file mode 100644 index 00000000..3d2cd425 --- /dev/null +++ b/src/main/java/guru/springframework/msscbreweryclient/web/config/NIORestTemplateCustomizer.java @@ -0,0 +1,47 @@ +package guru.springframework.msscbreweryclient.web.config; + +import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; +import org.apache.http.impl.nio.client.HttpAsyncClients; +import org.apache.http.impl.nio.conn.PoolingNHttpClientConnectionManager; +import org.apache.http.impl.nio.reactor.DefaultConnectingIOReactor; +import org.apache.http.impl.nio.reactor.IOReactorConfig; +import org.apache.http.nio.reactor.IOReactorException; +import org.springframework.boot.web.client.RestTemplateCustomizer; +import org.springframework.http.client.ClientHttpRequestFactory; +import org.springframework.http.client.HttpComponentsAsyncClientHttpRequestFactory; +import org.springframework.web.client.RestTemplate; + +/** + * Created by jt on 2019-08-07. + */ +//@Component +public class NIORestTemplateCustomizer implements RestTemplateCustomizer { + + public ClientHttpRequestFactory clientHttpRequestFactory() throws IOReactorException { + final DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(IOReactorConfig.custom(). + setConnectTimeout(3000). + setIoThreadCount(4). + setSoTimeout(3000). + build()); + + final PoolingNHttpClientConnectionManager connectionManager = new PoolingNHttpClientConnectionManager(ioreactor); + connectionManager.setDefaultMaxPerRoute(100); + connectionManager.setMaxTotal(1000); + + CloseableHttpAsyncClient httpAsyncClient = HttpAsyncClients.custom() + .setConnectionManager(connectionManager) + .build(); + + return new HttpComponentsAsyncClientHttpRequestFactory(httpAsyncClient); + + } + + @Override + public void customize(RestTemplate restTemplate) { + try { + restTemplate.setRequestFactory(clientHttpRequestFactory()); + } catch (IOReactorException e) { + e.printStackTrace(); + } + } +} From d0bb9680a8f7f236672d7872b588c673b7301523 Mon Sep 17 00:00:00 2001 From: John Thompson Date: Thu, 8 Aug 2019 17:59:41 -0400 Subject: [PATCH 7/8] adding logging --- src/main/resources/application.properties | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 633dfbaa..082c8a48 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,3 @@ sfg.brewery.apihost=http://localhost:8080 + +logging.level.org.apache.http=debug \ No newline at end of file From 168f58221269f799713a8b5e2a0e566a86b39e1d Mon Sep 17 00:00:00 2001 From: nmanukya Date: Fri, 18 Dec 2020 17:55:44 +0100 Subject: [PATCH 8/8] removing hard code by using application properties --- .../BlockingRestTemplateCustomizer.java | 23 +++++++++++++++---- src/main/resources/application.properties | 6 ++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java b/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java index 9f196319..a42d0c4e 100644 --- a/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java +++ b/src/main/java/guru/springframework/msscbreweryclient/web/config/BlockingRestTemplateCustomizer.java @@ -5,6 +5,7 @@ import org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.client.RestTemplateCustomizer; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; @@ -16,16 +17,30 @@ */ @Component public class BlockingRestTemplateCustomizer implements RestTemplateCustomizer { + private final Integer maxTotalConnections; + private final Integer defaultMaxTotalConnections; + private final Integer connectionRequestTimeout; + private final Integer socketTimeout; + + public BlockingRestTemplateCustomizer(@Value("${sfg.maxtotlaconnections}") Integer maxTotalConnections, + @Value("${sfg.defaultmaxtotalconnections}") Integer defaultMaxTotalConnections, + @Value("${sfg.connectionrequesttimeout}") Integer connectionRequestTimeout, + @Value("${sfg.sockettimeout}") Integer socketTimeout) { + this.maxTotalConnections = maxTotalConnections; + this.defaultMaxTotalConnections = defaultMaxTotalConnections; + this.connectionRequestTimeout = connectionRequestTimeout; + this.socketTimeout = socketTimeout; + } public ClientHttpRequestFactory clientHttpRequestFactory(){ PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); - connectionManager.setMaxTotal(100); - connectionManager.setDefaultMaxPerRoute(20); + connectionManager.setMaxTotal(maxTotalConnections); + connectionManager.setDefaultMaxPerRoute(defaultMaxTotalConnections); RequestConfig requestConfig = RequestConfig .custom() - .setConnectionRequestTimeout(3000) - .setSocketTimeout(3000) + .setConnectionRequestTimeout(connectionRequestTimeout) + .setSocketTimeout(socketTimeout) .build(); CloseableHttpClient httpClient = HttpClients diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 082c8a48..06f48575 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,3 +1,7 @@ sfg.brewery.apihost=http://localhost:8080 -logging.level.org.apache.http=debug \ No newline at end of file +logging.level.org.apache.http=debug +sfg.maxtotlaconnections=100 +sfg.defaultmaxtotalconnections=20 +sfg.connectionrequesttimeout=3000 +sfg.sockettimeout=3000 \ No newline at end of file