Skip to content

Instantly share code, notes, and snippets.

@krishofmans
Created June 19, 2018 12:06
Show Gist options
  • Save krishofmans/dcf37e5ab722cc48b8d42b7881b57855 to your computer and use it in GitHub Desktop.
Save krishofmans/dcf37e5ab722cc48b8d42b7881b57855 to your computer and use it in GitHub Desktop.
import org.apache.activemq.command.ActiveMQTextMessage
import org.springframework.context.event.EventListener
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.jms.core.JmsTemplate
import org.springframework.jms.core.MessageCreator
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import javax.jms.Queue
import javax.jms.TextMessage
// rest infra
internal class RestController(private val useCase: UseCase) {
@PostMapping
fun callSomething(body: String): ResponseEntity<*> {
val presenter = JsonPresenter()
useCase.doStuff(UseCaseRequest(body), presenter)
return presenter.responseEntity
}
}
data class JsonResult (val jsonValue: String)
internal class JsonPresenter : Presenter {
var responseEntity: ResponseEntity<*> = ResponseEntity.badRequest().body("")
get() = this.responseEntity
override fun failure(failReason: String) {
responseEntity = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(failReason)
}
override fun result(value: UseCaseResult){
responseEntity = ResponseEntity.ok(JsonResult(value.value))
}
}
// messaging infra
internal class MessageController(private val useCase: UseCase, private val dlqJms: JmsTemplate) {
@EventListener
fun callSomething(message: String) {
useCase.doStuff(
UseCaseRequest(message),
object : Presenter {
override fun failure(failReason: String) {
dlqJms.send { s -> s.createTextMessage(failReason) }
}
override fun result(value: UseCaseResult){
// happyness!
}
})
}
}
// app api
data class UseCaseRequest (val value: String)
data class UseCaseResult (val value: String)
interface Presenter {
fun result(value: UseCaseResult)
fun failure(failReason: String)
}
interface UseCase {
fun doStuff(request: UseCaseRequest, presenter: Presenter)
}
// app impl (ommitted interface)
internal class UseCaseImpl : UseCase {
override fun doStuff(request: UseCaseRequest, presenter: Presenter){
if (request.value == "shizzle"){
presenter.failure("We don't take no shizzle")
}
presenter.result(UseCaseResult("Shizzle 4 life"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment