项目作者: hadilq

项目描述 :
Yet another message queue
高级语言: Kotlin
项目地址: git://github.com/hadilq/CommandKU.git
创建时间: 2021-06-16T00:51:28Z
项目社区:https://github.com/hadilq/CommandKU

开源协议:Apache License 2.0

下载


Health Check
Maven Central

CommandKU

This library is so similar to an event bus or a message queue. The differences are

  • For what we called event or message in other libraries here we named them command.
  • Commands are in request-result pairs, which means for each request command we have a result
    command.
  • No broadcast command, because this library matches the results with requests, which is not
    achievable for broadcasts.

To handle the request-result pairs we used Kotlin coroutines’ biggest power, which is making
callbacks similar to ordinary lines of code with an input and output.

Usage

Let’s have some RequestCommand and ResultCommand like

  1. data class RequestCommand(val request: String) : Command
  2. data class ResultCommand(val result: String) : Command

we want to have a few lines of coroutines code to send the request and receive the result.

  1. suspend fun CommandExecutor.runCommand(request: String): CommandResult<ResultCommand> =
  2. exe(RequestCommand(request))
  3. scope.launch {
  4. when (val result = executor.runCommand("Are you there?")) {
  5. is Available<*> -> assert(result.command == ResultCommand("Yes! Of course!"))
  6. }
  7. }

There are a few notes here.

  • It doesn’t have any thread executor, so the coroutine context of calling exe will be used to
    run all suspend functions.
  • As you can see it’s possible that no callback is registered to handle the requested command,
    in that case the is Available<*> branch of when will not be called.

To receive the request and respond to it

  1. val registration = commandRegister.register(RequestCommand::class,
  2. CommandCallbackImpl(commandShooter) {
  3. ResultCommand("Yes! Of course!")
  4. })

whenever is needed we can dispose the registration.

Download

Download via gradle

  1. implementation "com.github.hadilq:command-ku-api:$libVersion"
  2. implementation "com.github.hadilq:command-ku-impl:$libVersion"

where you can find the libVersion in the Releases page of this repository.

Snapshots of the development version are available in Sonatype’s snapshots repository.

Contribution

Create an issue and suggest your idea and raise your hand if you want to implement it. If it’s okay
please create a pull request. Thank you, you’re awesome.