RxBus

A simple EventBus using RX Java

License

License

Categories

Categories

Java Languages EventBus Application Layer Libs Messaging
GroupId

GroupId

com.cookingfox
ArtifactId

ArtifactId

rxbus-eventbus-java
Last Version

Last Version

0.1.1
Release Date

Release Date

Type

Type

jar
Description

Description

RxBus
A simple EventBus using RX Java
Project URL

Project URL

http://github.com/cookingfox/rxbus-eventbus-java
Project Organization

Project Organization

Cooking Fox
Source Code Management

Source Code Management

http://github.com/cookingfox/rxbus-eventbus-java

Download rxbus-eventbus-java

How to add to project

<!-- https://jarcasting.com/artifacts/com.cookingfox/rxbus-eventbus-java/ -->
<dependency>
    <groupId>com.cookingfox</groupId>
    <artifactId>rxbus-eventbus-java</artifactId>
    <version>0.1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.cookingfox/rxbus-eventbus-java/
implementation 'com.cookingfox:rxbus-eventbus-java:0.1.1'
// https://jarcasting.com/artifacts/com.cookingfox/rxbus-eventbus-java/
implementation ("com.cookingfox:rxbus-eventbus-java:0.1.1")
'com.cookingfox:rxbus-eventbus-java:jar:0.1.1'
<dependency org="com.cookingfox" name="rxbus-eventbus-java" rev="0.1.1">
  <artifact name="rxbus-eventbus-java" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.cookingfox', module='rxbus-eventbus-java', version='0.1.1')
)
libraryDependencies += "com.cookingfox" % "rxbus-eventbus-java" % "0.1.1"
[com.cookingfox/rxbus-eventbus-java "0.1.1"]

Dependencies

compile (1)

Group / Artifact Type Version
io.reactivex : rxjava jar 1.1.1

test (1)

Group / Artifact Type Version
junit : junit jar 4.12

Project Modules

There are no modules declared in this project.

RxBus: a Java EventBus using ReactiveX

Build Status

Download

Download Maven Central

The distribution is hosted on Bintray. To include the package in your projects, you can add the jCenter repository.

Gradle

Add jCenter to your repositories block:

repositories {
    jcenter()
}

and add the project to the dependencies block in your build.gradle:

dependencies {
    compile 'com.cookingfox:rxbus-eventbus-java:0.1.1'
}

Maven

Add jCenter to your repositories in pom.xml or settings.xml:

<repositories>
    <repository>
        <id>jcenter</id>
        <url>http://jcenter.bintray.com</url>
    </repository>
</repositories>

and add the project declaration to your pom.xml:

<dependency>
    <groupId>com.cookingfox</groupId>
    <artifactId>rxbus-eventbus-java</artifactId>
    <version>0.1.1</version>
</dependency>

Features

  • Simple methods for subscribing to and dispatching events
  • Helper method for converting ("forwarding") an existing stream to the event bus
  • Based on interfaces to make custom implementations and mocking possible.

Usage

Create an RxBus instance

The default implementation is called DefaultRxBus:

RxBus rxBus = new DefaultRxBus();

The DefaultRxBus uses a serialized PublishSubject, but you can also provide your own Subject implementation:

RxBus rxBus = new DefaultRxBus(BehaviorSubject.create());

Emit (dispatch / post) an event

rxBus.emit(new MyEvent());

Aliases: RxBus#dispatch(), RxBus#post().

Subscribing to events

You can subscribe to all events that are posted on the RxBus:

rxBus.subscribe(observer);

But you're probably only interested in events of a certain type:

rxBus.subscribe(MyEvent.class, observer);

RxBus#subscribe() copies the Observable#subscribe() methods and, likewise, returns a Subscription reference.

Observing event streams

Directly subscribing to events is nice, but the cool thing about Rx is its operators! To get an Observable instance, use RxBus.observe().

You can observe all events that are posted on the RxBus:

rxBus.observe().subscribe(observer);

But you're probably only interested in events of a certain type:

rxBus.observe(MyEvent.class).subscribe(observer);

Alias: RxBus#on().

'Forwarding' observable streams to events

A common pattern when working with an EventBus is observing one stream and emitting 'global' events in response. This could be achieved as follows:

Observable<String> exampleStream = Observable.just("example");

exampleStream.map(new Func1<String, ExampleStringEvent>() {
    @Override
    public ExampleStringEvent call(String value) {
        return new ExampleStringEvent(value);
    }
}).subscribe(new Action1<ExampleStringEvent>() {
    @Override
    public void call(ExampleStringEvent event) {
        rxBus.emit(event);
    }
});

To apply this pattern more comfortably, RxBus provides the RxBus#forward() methods:

rxBus.forward(exampleStream, new Func1<String, ExampleStringEvent>() {
    @Override
    public ExampleStringEvent call(String value) {
        return new ExampleStringEvent(value);
    }
});

This becomes even more readable when using a lambda:

rxBus.forward(exampleStream, value -> new ExampleStringEvent(value));

Or even a method reference:

rxBus.forward(exampleStream, ExampleStringEvent::new);

F.A.Q.

Why does this exist? Aren't there enough other EventBus implementations using Rx?

Yes, there are many other implementations available, but I wanted a simple RxJava library that

Copyright and license

Code and documentation copyright 2016 Cooking Fox. Code released under the MIT license.

Versions

Version
0.1.1