/pubsub4k

A simple yet powerful pubsub system for Kotlin.

Primary LanguageKotlinApache License 2.0Apache-2.0

pubsub4k

Java 8+ License Maven Central Build Status Code quality Code coverage

A simple yet powerful pubsub system for Java and Kotlin.

Features

  • Fluent API for Java and Kotlin
  • Message filtering and mapping
  • Thread safe by default

Prerequisites

  • Use JDK 1.8 or later

Add as dependency

Latest version: Maven Central

Maven

<dependency>
  <groupId>pw.stamina</groupId>
  <artifactId>pubsub4k</artifactId>
  <version>{version}</version>
</dependency>

Gradle

Groovy DSL:

implementation 'pw.stamina:pubsub4k:{version}'

Kotlin DSL:

implementation("pw.stamina:pubsub4k:{version}")

Quick start

  1. Get an EventBus instance:
val bus = EventBus.createDefaultBus()
  1. Create a Publisher for Strings:
val stringPublisher = bus.getPublisher<String>()
  1. Create a class and implement MessageSubscriber:
class StringSubscriber : MessageSubscriber
  1. Create subscription(s) for your subscriber:
class StringSubscriber : MessageSubscriber {

    val stringSubscription = newSubscription<String> {
        println("Received: $it")
    }
}
  1. Instantiate your subscriber and register your subscription(s):
val subscriber = StringSubscriber()

bus.subscriptions.register(subscriber.stringSubscription)
  1. Publish a message to your publisher:
stringPublisher.publish("Hello world")
// "Received: Hello world" is printed