BAEL-4915 added in the code for the article on long polling (#10660)

* BAEL-4915 added in the code for the article on long polling using DeferredResult

* BAEL-4915 altered package for long polling article due to previous package being full

Co-authored-by: Liam Garvie <liamgarvie@Liams-MacBook-Pro.local>
This commit is contained in:
LiamGve 2021-04-22 02:44:33 +01:00 committed by GitHub
parent 76c1b7c0fd
commit 6a3ba86878
4 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.longpolling.client;
import io.netty.handler.timeout.ReadTimeoutException;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import java.time.Duration;
@Component
public class LongPollingBakeryClient {
public String callBakeWithRestTemplate(RestTemplateBuilder restTemplateBuilder) {
RestTemplate restTemplate = restTemplateBuilder
.setConnectTimeout(Duration.ofSeconds(10))
.setReadTimeout(Duration.ofSeconds(10))
.build();
try {
return restTemplate.getForObject("/api/bake/cookie?bakeTime=1000", String.class);
} catch (ResourceAccessException e) {
throw e;
}
}
public String callBakeWithWebClient() {
WebClient webClient = WebClient.create();
try {
return webClient.get()
.uri("/api/bake/cookie?bakeTime=1000")
.retrieve()
.bodyToFlux(String.class)
.timeout(Duration.ofSeconds(10))
.blockFirst();
} catch (ReadTimeoutException e) {
throw e;
}
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.longpolling.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static java.lang.String.format;
/**
* Long polling controller example.
*/
@RestController
@RequestMapping("/api")
public class BakeryController {
private final static Logger LOG = LoggerFactory.getLogger(BakeryController.class);
private final static Long LONG_POLLING_TIMEOUT = 5000L;
private ExecutorService bakers;
public BakeryController() {
bakers = Executors.newFixedThreadPool(5);
}
@GetMapping("/bake/{bakedGood}")
public DeferredResult<String> publisher(@PathVariable String bakedGood, @RequestParam Integer bakeTime) {
DeferredResult<String> output = new DeferredResult<>(LONG_POLLING_TIMEOUT);
bakers.execute(() -> {
try {
Thread.sleep(bakeTime);
output.setResult(format("Bake for %s complete and order dispatched. Enjoy!", bakedGood));
} catch (Exception e) {
output.setErrorResult("Something went wrong with your order!");
}
});
output.onTimeout(() -> output.setErrorResult("the bakery is not responding in allowed time"));
return output;
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.longpolling.integration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockAsyncContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.IOException;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
@AutoConfigureMockMvc
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BakeryControllerIntegrationTest {
@Autowired
private MockMvc mockMvc;
@Test
public void givenDeferredResultTimesOut_ThenErrorResponseIsRecieved() throws Exception {
MvcResult asyncListener = mockMvc
.perform(MockMvcRequestBuilders.get("/api/bake/cookie?bakeTime=6000"))
.andExpect(request().asyncStarted())
.andReturn();
enableTimeout(asyncListener);
String response = mockMvc
.perform(asyncDispatch(asyncListener))
.andReturn()
.getResponse()
.getContentAsString();
assertThat(response)
.isEqualTo("the bakery is not responding in allowed time");
}
@Test
public void givenDeferredResultSuccessful_ThenSuccessResponseIsRecieved() throws Exception {
MvcResult asyncListener = mockMvc
.perform(MockMvcRequestBuilders.get("/api/bake/cookie?bakeTime=1000"))
.andExpect(request().asyncStarted())
.andReturn();
String response = mockMvc
.perform(asyncDispatch(asyncListener))
.andReturn()
.getResponse()
.getContentAsString();
assertThat(response)
.isEqualTo("Bake for cookie complete and order dispatched. Enjoy!");
}
private static void enableTimeout(MvcResult asyncListener) throws IOException {
((MockAsyncContext) asyncListener
.getRequest()
.getAsyncContext())
.getListeners()
.get(0)
.onTimeout(null);
}
}

View File

@ -24,6 +24,10 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>