--- /dev/null
+package de.spring.webservices.rest.business.service.impl;
+
+import java.util.concurrent.CompletableFuture;
+
+import javax.inject.Inject;
+
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+import org.springframework.stereotype.Service;
+
+import de.spring.webservices.domain.Car;
+import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
+import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogic;
+
+@Service("completableFutureBusinessLogic")
+public class CompletablefutureBusinessLogicImpl implements CompletableFutureBusinessLogic {
+ private final AwesomeBusinessLogic awesomeBusinessLogic;
+
+ @Inject
+ public CompletablefutureBusinessLogicImpl(AwesomeBusinessLogic awesomeBusinessLogic) {
+ this.awesomeBusinessLogic = awesomeBusinessLogic;
+ }
+
+ @Override
+ public CompletableFuture<Page<Car>> findAll(Pageable pageRequest) {
+ return CompletableFuture.supplyAsync(() -> awesomeBusinessLogic.findAll(pageRequest));
+ }
+
+ @Override
+ public CompletableFuture<Car> findById(long id) {
+ return CompletableFuture.supplyAsync(() -> awesomeBusinessLogic.findById(id));
+ }
+
+ @Override
+ public CompletableFuture<Car> create(Car car) {
+ return CompletableFuture.supplyAsync(() -> awesomeBusinessLogic.create(car));
+ }
+}
import java.util.Map;
import java.util.concurrent.CompletableFuture;
+import static de.spring.webservices.rest.controller.adapters.CompletableFutureAdapter.deferredAdapter;
+
import javax.inject.Inject;
import org.slf4j.Logger;
import org.springframework.web.context.request.async.DeferredResult;
import de.spring.webservices.domain.Car;
-import de.spring.webservices.rest.business.service.AwesomeBusinessLogic;
-import de.spring.webservices.rest.controller.adapters.CompletableFutureAdapter;
+import de.spring.webservices.rest.business.service.CompletableFutureBusinessLogic;
@RestController
@RequestMapping("/api/deferrable/cars/")
private static final int PAGE = 2;
private static final int PAGE_SIZE = 10;
- private final AwesomeBusinessLogic awesomeBusinessLogic;
+ private final CompletableFutureBusinessLogic completableFutureBusinessLogic;
@Inject
- public DeferrableCarController(AwesomeBusinessLogic awesomeBusinessLogic) {
- this.awesomeBusinessLogic = awesomeBusinessLogic;
+ public DeferrableCarController(CompletableFutureBusinessLogic completableFutureBusinessLogic) {
+ this.completableFutureBusinessLogic = completableFutureBusinessLogic;
}
@RequestMapping(produces = { MediaType.APPLICATION_JSON_UTF8_VALUE }, method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
public DeferredResult<Page<Car>> cars() {
-
- return CompletableFutureAdapter.callAdapter(() ->
- awesomeBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE)));
+
+ return deferredAdapter(completableFutureBusinessLogic.findAll(new PageRequest(PAGE, PAGE_SIZE)));
}
@RequestMapping(value = "{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
}
}
- return CompletableFutureAdapter.callAdapter(() -> awesomeBusinessLogic.findById(id));
+ return deferredAdapter(completableFutureBusinessLogic.findById(id));
+
}
@RequestMapping(consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public DeferredResult<ResponseEntity<Car>> create(@RequestBody Car car) {
-
- return CompletableFutureAdapter.callAdapter(() -> createResponseCar(car));
+
+ return deferredAdapter(createAsync(car));
}
- private ResponseEntity<Car> createResponseCar(Car car) {
- Car createdCar = awesomeBusinessLogic.create(car);
-
+ private CompletableFuture<ResponseEntity<Car>> createAsync(Car car) {
+
+ return completableFutureBusinessLogic
+ .create(car)
+ .thenComposeAsync(newCar ->
+ CompletableFuture.supplyAsync(() -> createResponseCar(newCar))
+
+ );
+ }
+
+ private ResponseEntity<Car> createResponseCar(Car car) {
HttpHeaders headers = new HttpHeaders();
- headers.add(HttpHeaders.LOCATION, "/api/cars/" + createdCar.getId());
- return new ResponseEntity<>(createdCar, headers, HttpStatus.CREATED);
+ headers.add(HttpHeaders.LOCATION, "/api/cars/" + car.getId());
+ return new ResponseEntity<>(car, headers, HttpStatus.CREATED);
}
}
public T doCall();
}
-
- public static final <T> DeferredResult<T> callAdapter(DeferredCall<T> deferredCall) {
+ public static final <T> DeferredResult<T> deferredAdapter(CompletableFuture<T> completableFuture) {
DeferredResult<T> deferredResult = new DeferredResult<>(ASYNC_TIMEOUT);
- CompletableFuture
- .supplyAsync(deferredCall::doCall)
+
+ completableFuture
.thenAcceptAsync(deferredResult::setResult)
- .exceptionally(exception -> {
-
- LOGGER.error("findById error: ", exception);
-
- deferredResult.setErrorResult(exception);
-
- return null;
- });
+ .exceptionally(exception -> {
+
+ LOGGER.error("error: ", exception);
+
+ deferredResult.setErrorResult(exception);
+
+ return null;
+ });
return deferredResult;
}