A simple yet powerful pubsub system for Java and Kotlin.
- Fluent API for Java and Kotlin
- Message filtering and mapping
- Thread safe by default
- Use JDK 1.8 or later
<dependency>
<groupId>pw.stamina</groupId>
<artifactId>pubsub4k</artifactId>
<version>{version}</version>
</dependency>
Groovy DSL:
implementation 'pw.stamina:pubsub4k:{version}'
Kotlin DSL:
implementation("pw.stamina:pubsub4k:{version}")
- Get an
EventBus
instance:
val bus = EventBus.createDefaultBus()
- Create a
Publisher
forString
s:
val stringPublisher = bus.getPublisher<String>()
- Create a class and implement
MessageSubscriber
:
class StringSubscriber : MessageSubscriber
- Create subscription(s) for your subscriber:
class StringSubscriber : MessageSubscriber {
val stringSubscription = newSubscription<String> {
println("Received: $it")
}
}
- Instantiate your subscriber and register your subscription(s):
val subscriber = StringSubscriber()
bus.subscriptions.register(subscriber.stringSubscription)
- Publish a message to your publisher:
stringPublisher.publish("Hello world")
// "Received: Hello world" is printed