|
| 1 | +package org.gitanimals.supports.deadletter |
| 2 | + |
| 3 | +import org.gitanimals.core.TraceIdContextOrchestrator |
| 4 | +import org.gitanimals.core.TraceIdContextRollback |
| 5 | +import org.gitanimals.core.filter.MDCFilter.Companion.TRACE_ID |
| 6 | +import org.rooftop.netx.api.OrchestratorFactory |
| 7 | +import org.slf4j.MDC |
| 8 | +import org.springframework.web.bind.annotation.GetMapping |
| 9 | +import org.springframework.web.bind.annotation.RequestParam |
| 10 | +import org.springframework.web.bind.annotation.RestController |
| 11 | + |
| 12 | +@RestController |
| 13 | +class DeadLetterTestController( |
| 14 | + private val deadLetterEventPublisher: DeadLetterEventPublisher, |
| 15 | + private val orchestratorFactory: OrchestratorFactory, |
| 16 | +) { |
| 17 | + |
| 18 | + @GetMapping("/test/dead-letter") |
| 19 | + fun test( |
| 20 | + @RequestParam("message") message: String, |
| 21 | + ) { |
| 22 | + val orchestrator = orchestratorFactory.create<String>("dead-letter-test") |
| 23 | + .startWithContext( |
| 24 | + contextOrchestrate = TraceIdContextOrchestrator { _, it -> it }, |
| 25 | + contextRollback = TraceIdContextRollback { _, it -> |
| 26 | + it |
| 27 | + } |
| 28 | + ) |
| 29 | + .joinWithContext( |
| 30 | + contextOrchestrate = TraceIdContextOrchestrator { _, it -> it }, |
| 31 | + contextRollback = TraceIdContextRollback { _, it -> |
| 32 | + throw IllegalStateException("add dead letter") |
| 33 | + } |
| 34 | + ) |
| 35 | + .commitWithContext(TraceIdContextOrchestrator { _, _ -> |
| 36 | + throw IllegalStateException("test dead-letter") |
| 37 | + }) |
| 38 | + |
| 39 | + val result = orchestrator.sagaSync(message, context = mapOf("hello" to "world", "traceId" to MDC.get(TRACE_ID))) |
| 40 | + |
| 41 | + if (result.isSuccess.not()) { |
| 42 | + result.throwError() |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments