org.enodeframework.queue.command.DefaultCommandService.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of enode Show documentation
Show all versions of enode Show documentation
The enodeframework core implementation.
package org.enodeframework.queue.command
import org.enodeframework.commanding.CommandResult
import org.enodeframework.commanding.CommandReturnType
import org.enodeframework.commanding.ICommand
import org.enodeframework.commanding.ICommandService
import org.enodeframework.common.serializing.ISerializeService
import org.enodeframework.common.utilities.Ensure
import org.enodeframework.common.utilities.InetUtil
import org.enodeframework.queue.ISendMessageService
import org.enodeframework.queue.QueueMessage
import java.util.*
import java.util.concurrent.CompletableFuture
/**
* @author [email protected]
*/
class DefaultCommandService(private val topic: String, private val tag: String, private val commandResultProcessor: ICommandResultProcessor, private val sendMessageService: ISendMessageService, private val serializeService: ISerializeService) : ICommandService {
override fun sendAsync(command: ICommand): CompletableFuture {
return sendMessageService.sendMessageAsync(buildCommandMessage(command, false))
}
override fun executeAsync(command: ICommand): CompletableFuture {
return executeAsync(command, CommandReturnType.CommandExecuted)
}
override fun executeAsync(command: ICommand, commandReturnType: CommandReturnType): CompletableFuture {
val taskCompletionSource = CompletableFuture()
try {
Ensure.notNull(commandResultProcessor, "commandResultProcessor")
commandResultProcessor.registerProcessingCommand(command, commandReturnType, taskCompletionSource)
val sendMessageAsync = sendMessageService.sendMessageAsync(buildCommandMessage(command, true))
sendMessageAsync.exceptionally { ex: Throwable? ->
commandResultProcessor.processFailedSendingCommand(command)
taskCompletionSource.completeExceptionally(ex)
null
}
} catch (ex: Exception) {
taskCompletionSource.completeExceptionally(ex)
}
return taskCompletionSource
}
protected fun buildCommandMessage(command: ICommand, needReply: Boolean): QueueMessage {
Ensure.notNull(command.aggregateRootId, "aggregateRootId")
Ensure.notNull(topic, "topic")
val commandData = serializeService.serialize(command)
val commandMessage = CommandMessage()
if (needReply) {
val replyAddress = InetUtil.toSocketAddress(commandResultProcessor.bindAddress)
commandMessage.replyAddress = replyAddress
}
commandMessage.commandData = commandData
commandMessage.commandType = command.javaClass.name
val messageData = serializeService.serialize(commandMessage)
val queueMessage = QueueMessage()
queueMessage.topic = topic
queueMessage.tag = tag
queueMessage.body = messageData
queueMessage.routeKey = command.aggregateRootId
val key = String.format("%s%s", command.id, Optional.ofNullable(command.aggregateRootId)
.map { x: String -> "_cmd_agg_$x" }.orElse(""))
queueMessage.key = key
return queueMessage
}
}