|
| 1 | +package com.baeldung.spring.session; |
| 2 | + |
| 3 | +import org.slf4j.Logger; |
| 4 | +import org.slf4j.LoggerFactory; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.web.bind.annotation.GetMapping; |
| 7 | +import org.springframework.web.bind.annotation.RestController; |
| 8 | + |
| 9 | +import java.util.concurrent.Executor; |
| 10 | + |
| 11 | +@RestController |
| 12 | +public class SleuthController { |
| 13 | + |
| 14 | + private Logger logger = LoggerFactory.getLogger(this.getClass()); |
| 15 | + private final SleuthService sleuthService; |
| 16 | + private final Executor executor; |
| 17 | + |
| 18 | + @Autowired |
| 19 | + public SleuthController(SleuthService sleuthService, Executor executor) { |
| 20 | + this.sleuthService = sleuthService; |
| 21 | + this.executor = executor; |
| 22 | + } |
| 23 | + |
| 24 | + @GetMapping("/") |
| 25 | + public String helloSleuth() { |
| 26 | + logger.info("Hello Sleuth"); |
| 27 | + return "success"; |
| 28 | + } |
| 29 | + |
| 30 | + @GetMapping("/same-span") |
| 31 | + public String helloSleuthSameSpan() throws InterruptedException { |
| 32 | + logger.info("Same Span"); |
| 33 | + sleuthService.doSomeWorkSameSpan(); |
| 34 | + return "success"; |
| 35 | + } |
| 36 | + |
| 37 | + @GetMapping("/new-span") |
| 38 | + public String helloSleuthNewSpan() throws InterruptedException { |
| 39 | + logger.info("New Span"); |
| 40 | + sleuthService.doSomeWorkNewSpan(); |
| 41 | + return "success"; |
| 42 | + } |
| 43 | + |
| 44 | + @GetMapping("/new-thread") |
| 45 | + public String helloSleuthNewThread() { |
| 46 | + logger.info("New Thread"); |
| 47 | + Runnable runnable = () -> { |
| 48 | + try { |
| 49 | + Thread.sleep(1000L); |
| 50 | + } catch (InterruptedException e) { |
| 51 | + e.printStackTrace(); |
| 52 | + } |
| 53 | + logger.info("I'm inside the new thread - with a new span"); |
| 54 | + }; |
| 55 | + executor.execute(runnable); |
| 56 | + |
| 57 | + logger.info("I'm done - with the original span"); |
| 58 | + return "success"; |
| 59 | + } |
| 60 | + |
| 61 | + @GetMapping("/async") |
| 62 | + public String helloSleuthAsync() throws InterruptedException { |
| 63 | + logger.info("Before Async Method Call"); |
| 64 | + sleuthService.asyncMethod(); |
| 65 | + logger.info("After Async Method Call"); |
| 66 | + return "success"; |
| 67 | + } |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +} |
0 commit comments