Skip to content

Async kotlin client for beanstalkd work queue

License

Notifications You must be signed in to change notification settings

virusbear/beanstalkt

Repository files navigation

beanstalkt

Build Status Kotlin GitHub Maven Central

A simple async kotlin client for beanstalkd work queue.

Description

This client implements all operations of beanstalkd as described in the protocol documentation.

All operations are implemented to be suspending functions, suspending the caller until a response is received by beanstalkd.

Usage

suspend fun main() = coroutineScope {
    val client = DefaultClient()

    while(true) {
        val job = client.reserve()
        println(job.id)
    }
}

Known Issues

Due to the blocking nature of the beanstalk protocol, it is currently not possible to have a client reserve a job, whilst performing other operations asynchronously. All operations are added to an internal queue and processed sequentially. To avoid blocking issues with the reserve operation, use the reserveWithTimeout() operation to execute other operations after a suitable timeout.

suspend fun main() = coroutineScope {
    val client = DefaultClient()

    val producerJob = launch {
        while(true) {
            client.put("Hello World!".toByteArray())
        }
    }
    
    val consumerJob = launch {
        while(true) {
            try {
                val job = client.reserveWithTimeout(10.0.seconds)
                println(job.id)
            } catch(ex: TimedOutException) {
                //Ignored
            }
        }
    }
    
    producerJob.join()
    consumerJob.join()
}

Features

  • beanstalkd protocol completely implemented
  • Connection Pooling
  • suspending iterator similar to kotlin Channel implementation
  • Kotlin Multiplatform

Using in your projects

Maven

Add dependencies (you can also add other modules that you need):

<dependency>
    <groupId>com.virusbear.beanstalkt</groupId>
    <artifactId>beanstalkt</artifactId>
    <version>1.0.0</version>
</dependency>

And make sure that you use the latest Kotlin version:

<properties>
    <kotlin.version>1.9.0</kotlin.version>
</properties>

Gradle

Add dependencies (you can also add other modules that you need):

dependencies {
    implementation("com.virusbear.beanstalkt:beanstalkt:1.0.0")
}

And make sure that you use the latest Kotlin version:

plugins {
    // For build.gradle.kts (Kotlin DSL)
    kotlin("jvm") version "1.9.0"
    
    // For build.gradle (Groovy DSL)
    id "org.jetbrains.kotlin.jvm" version "1.9.0"
}

Make sure that you have mavenCentral() in the list of repositories:

repositories {
    mavenCentral()
}

State of Dev & Contributing

This project is only a side project of mine, which originated from another project. I will try to fix any bugs I encounter during my own use cases, but will most likely be unable to work on parts that I am not personally using.

If you encounter any bugs or have ideas on how to improve this project, please file an issue or create a pull request.

Any help is appreciated.

Building

To build this project run the gradle build task.

CLI

./gradlew build