All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.enodeframework.queue.command.DefaultCommandService.kt Maven / Gradle / Ivy

There is a newer version: 1.1.10
Show newest version
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 send(command: ICommand): Boolean {
        return this.sendAsync(command).join()
    }

    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
    }

    override fun execute(command: ICommand): CommandResult {
        return this.executeAsync(command).join()
    }

    override fun execute(command: ICommand, commandReturnType: CommandReturnType): CommandResult {
        return this.executeAsync(command, commandReturnType).join()
    }

    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
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy