|
| 1 | +package com.example.demo; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 4 | +import org.springframework.http.MediaType; |
| 5 | +import org.springframework.web.bind.annotation.*; |
| 6 | + |
| 7 | +import java.util.concurrent.*; |
| 8 | + |
| 9 | +@RestController |
| 10 | +@RequestMapping("/api/pets/create/async") |
| 11 | +public class AsyncContextPropagationController { |
| 12 | + private final Executor springExecutor; |
| 13 | + private final AsyncContextPropagationService asyncContextPropagationService; |
| 14 | + |
| 15 | + public AsyncContextPropagationController( |
| 16 | + @Qualifier("asyncContextPropagationExecutor") Executor springExecutor, |
| 17 | + AsyncContextPropagationService asyncContextPropagationService |
| 18 | + ) { |
| 19 | + this.springExecutor = springExecutor; |
| 20 | + this.asyncContextPropagationService = asyncContextPropagationService; |
| 21 | + } |
| 22 | + |
| 23 | + private record PetCreate(String name) {} |
| 24 | + |
| 25 | + @PostMapping( |
| 26 | + path = "/completable-future-single", |
| 27 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 28 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 29 | + ) |
| 30 | + public PetsController.Rows completableFutureSingle(@RequestBody PetCreate pet) throws Exception { |
| 31 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 32 | + try { |
| 33 | + return CompletableFuture |
| 34 | + .supplyAsync(() -> createPet(pet.name()), executor) |
| 35 | + .get(); |
| 36 | + } finally { |
| 37 | + executor.shutdown(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + @PostMapping( |
| 42 | + path = "/submit-callable", |
| 43 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 44 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 45 | + ) |
| 46 | + public PetsController.Rows submitCallable(@RequestBody PetCreate pet) throws Exception { |
| 47 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 48 | + try { |
| 49 | + return executor.submit(() -> createPet(pet.name())).get(); |
| 50 | + } finally { |
| 51 | + executor.shutdown(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @PostMapping( |
| 56 | + path = "/thread-pool-execute", |
| 57 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 58 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 59 | + ) |
| 60 | + public PetsController.Rows threadPoolExecute(@RequestBody PetCreate pet) throws Exception { |
| 61 | + ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(1); |
| 62 | + try { |
| 63 | + CompletableFuture<PetsController.Rows> future = new CompletableFuture<>(); |
| 64 | + executor.execute(() -> { |
| 65 | + try { |
| 66 | + future.complete(createPet(pet.name())); |
| 67 | + } catch (Throwable throwable) { |
| 68 | + future.completeExceptionally(throwable); |
| 69 | + } |
| 70 | + }); |
| 71 | + return future.get(); |
| 72 | + } finally { |
| 73 | + executor.shutdown(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @PostMapping( |
| 78 | + path = "/fork-join-submit", |
| 79 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 80 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 81 | + ) |
| 82 | + public PetsController.Rows forkJoinSubmit(@RequestBody PetCreate pet) throws Exception { |
| 83 | + return ForkJoinPool.commonPool() |
| 84 | + .submit(() -> createPet(pet.name())) |
| 85 | + .get(); |
| 86 | + } |
| 87 | + |
| 88 | + @PostMapping( |
| 89 | + path = "/scheduled-callable", |
| 90 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 91 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 92 | + ) |
| 93 | + public PetsController.Rows scheduledCallable(@RequestBody PetCreate pet) throws Exception { |
| 94 | + ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); |
| 95 | + try { |
| 96 | + return executor.schedule( |
| 97 | + () -> createPet(pet.name()), |
| 98 | + 1, |
| 99 | + TimeUnit.MILLISECONDS |
| 100 | + ).get(); |
| 101 | + } finally { |
| 102 | + executor.shutdown(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @PostMapping( |
| 107 | + path = "/spring-task-executor", |
| 108 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 109 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 110 | + ) |
| 111 | + public PetsController.Rows springTaskExecutor(@RequestBody PetCreate pet) throws Exception { |
| 112 | + CompletableFuture<PetsController.Rows> future = new CompletableFuture<>(); |
| 113 | + springExecutor.execute(() -> { |
| 114 | + try { |
| 115 | + future.complete(createPet(pet.name())); |
| 116 | + } catch (Throwable throwable) { |
| 117 | + future.completeExceptionally(throwable); |
| 118 | + } |
| 119 | + }); |
| 120 | + return future.get(); |
| 121 | + } |
| 122 | + |
| 123 | + @PostMapping( |
| 124 | + path = "/spring-async-annotation", |
| 125 | + consumes = MediaType.APPLICATION_JSON_VALUE, |
| 126 | + produces = MediaType.APPLICATION_JSON_VALUE |
| 127 | + ) |
| 128 | + public PetsController.Rows springAsyncAnnotation(@RequestBody PetCreate pet) throws Exception { |
| 129 | + return asyncContextPropagationService |
| 130 | + .createPetWithAsyncAnnotation(pet.name()) |
| 131 | + .get(); |
| 132 | + } |
| 133 | + |
| 134 | + private PetsController.Rows createPet(String name) { |
| 135 | + Integer rowsCreated = DatabaseHelper.createPetByName(name); |
| 136 | + return new PetsController.Rows(rowsCreated); |
| 137 | + } |
| 138 | +} |
0 commit comments