Simple, lightweight and fast event bus tailored for Android

Related tags

EventBus tinybus
Overview

This project is @deprecated in favor of RxJava. It offers the same event-driven programming model as TinyBus, but it's more capable and offers better control of threading.


tinybus

TinyBus is the faster implementation of Otto event bus with additional features you missed.

Build Status

TinyBus is

  • tiny (~ 26K jar)
  • fast (optimized for startup and event dispatching)
  • well tested (> 90 junit tests)
  • annotation based (no requirements on method names, no interfaces to implement)

TinyBus API in a nutshell

  • @Subscribe annotates event handler methods running in the main thread.
  • @Subscribe(mode=Mode.Background) annotates event handler methods running in a background thread.
  • @Subscribe(mode=Mode.Background, queue="web") annotates event handler methods running in a serialized background queue with given name. You can have as many queues as you want.
  • @Produce annotates methods returning most recent events (aka sticky events).
  • Bus.register(Object) and Bus.unregister(Object) register and unregister objects with annotated subscriber and producer methods.
  • Bus.hasRegistered(Object) checks, whether given object is already registered.
  • Bus.post(Object) posts given event object to all registered subscribers.
  • Bus.postDelayed(Object, long) and Bus.cancelDelayed(Class) schedules single event delivery for later in time and cancels it.

For a more detailed example check out Getting started step-by-step guide or example application.

Performance reference tests

tinybus

Executed on Nexus 5 device (Android 5.0.1, ART, screen off).

TinyBus extensions (still in 'β')

Extensions is a unique feature of TinyBus. With it you can easily subscribe to commonly used events like battery level, connectivity change, phone shake event or even standard Android broadcast Intents. Here is a short example.

public class MainActivity extends Activity {
    private TinyBus mBus;
        
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        // get bus instance 
        mBus = TinyBus.from(this)
        
        if (savedInstanceState == null) {
            // Note: ShakeEventWire stays wired when activity is re-created
            //       on configuration change. That's why we register is 
            //       only once inside if-statement.

            // wire device shake event provider
            mBus.wire(new ShakeEventWire());
        }
    }
    
    @Override
    protected void onStart() {
        super.onStart();
	    mBus.register(this);
	}
	
    @Override
    protected void onStop() {
        mBus.unregister(this);
        super.onStop();
    }
    
    @Subscribe
    public void onShakeEvent(ShakeEvent event) {
        // device has been shaken
    }
}

More detailed usage example can be found in example application.

Gradle dependencies

For pure event bus implementation

dependencies {
    compile 'de.halfbit:tinybus:3.0.2'
}

For event bus with extensions

dependencies {
    compile 'de.halfbit:tinybus:3.0.2'
    compile 'de.halfbit:tinybus-extensions:3.0.2'
}

ProGuard configuration

If you use Gradle build, then you don't need to configure anything, because it will use proper configuration already delivered with Android library archive. Otherwise you can use the configuration below:

-keepclassmembers, allowobfuscation class ** {
    @de.halfbit.tinybus.Subscribe public *;
    @de.halfbit.tinybus.Produce public *;
}

Used by

License

Copyright (c) 2014-2015 Sergej Shafarenka, halfbit.de
Copyright (C) 2012 Square, Inc.
Copyright (C) 2007 The Guava Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • java.lang.RuntimeException

    java.lang.RuntimeException

    Hello,

    In our app we are seeing this java.lang.RuntimeException once in a while. Unfortunately, there are nothing in the stack trace which indicates where in our code or when this happens.

    Version: de.halfbit:tinybus:3.0.2

    java.lang.RuntimeException de.halfbit.tinybus.TinyBus.handleExceptionOnEventDispatch(SourceFile:329) de.halfbit.tinybus.TinyBus.processQueue(SourceFile:383) de.halfbit.tinybus.TinyBus$TinyBusImpl.onPostFromBackground(SourceFile:529) de.halfbit.tinybus.impl.Task.run(SourceFile:87) android.os.Handler.handleCallback(Handler.java:739) android.os.Handler.dispatchMessage(Handler.java:95) android.os.Looper.loop(Looper.java:158) android.app.ActivityThread.main(ActivityThread.java:7229) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.reflect.Method.invoke(Native Method) de.halfbit.tinybus.TinyBus$TinyBusImpl.dispatchEvent(SourceFile:460) de.halfbit.tinybus.TinyBus.processQueue(SourceFile:380) de.halfbit.tinybus.TinyBus$TinyBusImpl.onPostFromBackground(SourceFile:529) de.halfbit.tinybus.impl.Task.run(SourceFile:87) android.os.Handler.handleCallback(Handler.java:739) android.os.Handler.dispatchMessage(Handler.java:95) android.os.Looper.loop(Looper.java:158) android.app.ActivityThread.main(ActivityThread.java:7229) java.lang.reflect.Method.invoke(Native Method) com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

    question 
    opened by TheKlint 6
  • Remove allowBackup=

    Remove allowBackup="false" from TinyBus manifest

    Any specific reason you are disallowing backup with this library? Right now to include this library and allow backup for an app you have to use tools:replace="android:allowBackup" in your manifest which is annoying.

    Thanks!

    enhancement 
    opened by sburba 5
  • Could not find at Maven Central

    Could not find at Maven Central

    Tinybus looks really promising but I couldn't find it on Maven Central :). It would be great when we could add this library via a dependency in Gradle to our projects.

    enhancement 
    opened by maveonair 5
  •  java.lang.NoClassDefFoundError: android/os/PersistableBundle crash

    java.lang.NoClassDefFoundError: android/os/PersistableBundle crash

    After some recent Play Services update this crash has become very common for any app using any kind of Event Bus implementation on pre-21 devices. Happens when trying to call register(Obj). The only solution I've seen so far is not using anything containing the PersistableBundle, which I don't use anyway and still have this issue. Do you have any ideas on that?

    TODO 
    opened by vad-zuev 4
  • TinyBus can't deliver large objects?

    TinyBus can't deliver large objects?

    Consider the following event class:

    public final class ParseEvent extends Event {
    
    private User user;
    
    public ParseEvent(User user) {
        this.type = Type.ON_USER_PARSE_COMPLETE;
        this.user = user;
    }
    
    public User user() {
        return user;
    }
    }
    

    Calling post() with an instance of this class does not trigger anything. The User object is quite large with a lot of fields, could this be the issue?

    opened by vad-zuev 4
  • Event processing stops when processQueue throws

    Event processing stops when processQueue throws

    When processQueue throws an exception, the entire event bus stops working (mProcessing remains true)

    Suggest adding a try - finnaly block that resets mProcessing

    bug 
    opened by NicolaVerbeeck 4
  • Performance improvements using an annotation processor

    Performance improvements using an annotation processor

    Hi @beworker,

    congratulation for this project and being referenced on Android Weekly.

    I looked at your source code and it looks like you are using a lot of reflection to discover the methods that are annotated to subscribe to or produce events (in https://github.com/beworker/tinybus/blob/master/tinybus/src/com/halfbit/tinybus/ObjectMeta.java).

    You could actually get some performance gains by using an annotation processor to detect all such methods at compile time and get references to them. Thus you would not need to scan the classes, you would know in advance where such methods are in a given class.

    You can find an example of such an annotation processor in :

    • blender, currently a fork of guice available here : https://github.com/stephanenicolas/guice/tree/blender-PR-2

    if you need more high level explanations, please refer to these slides : https://speakerdeck.com/stephanenicolas/blender-boosting-guice-with-annotation-processing

    enhancement 
    opened by stephanenicolas 4
  • Question regarding TinyBus limits of ussage

    Question regarding TinyBus limits of ussage

    Hi. I found your library very intersesting and I gave it a try in my custom Views. For now it seems to work, much better than Otto which threw null pointers and sometimes it worked. I wanted to ask you, can I use it in Long running Tasks, in Services for example, or Broadcast receivers and Volley HTTP call backs. Thx.

    opened by greenspand 4
  • Only last event gets delivered when posting multiple events via .postDelayed()

    Only last event gets delivered when posting multiple events via .postDelayed()

    If I send multiple consecutive events via .postDelayed(), only the last event is delivered to the subscriber.

    Example: MyEvent eventA = new MyEvent("A"); MyEvent eventB = new MyEvent("B");

    mBus.postDelayed(eventA, 1000); mBus.postDelayed(eventB, 1000);

    In the subscriber method I only get 'eventB' delivered.

    opened by TheKlint 3
  • API10 support added.

    API10 support added.

    Added initial implementation for API10 compatibility. Updated sample, build scripts and example.

    Tested only on emulator.

    Notes:

    • potentially due to no implementation of isChangingConfigurations API (it requires API11 level) may be a temporary leak in app (weak references at the final point will resolve the leak, but we are acting not very polite to GC).
    opened by OleksandrKucherenko 2
  • subclasses do not receive events?

    subclasses do not receive events?

    Hi there,

    it seems that subclasses won't receive an Event when overriding a superclass method annotated with [at]Subscribe. Could you comment on that? Thanks.

    question 
    opened by vad-zuev 1
  • Tinybus Android Studio Plugin

    Tinybus Android Studio Plugin

    Hi,

    I've taken over a project at work that use Tinybus events extensively, but I find it hard to keep track of the events and see where they are catched. I've used Otto before and it has a Android Studio plugin that makes it easy to keep track of the events. Is there something similar for Tinybus?

    TODO 
    opened by robbysmeticapps 1
  • Can support for activity post to another activity like eventbus?

    Can support for activity post to another activity like eventbus?

    RT,Use TinyBus but can not receive event in another activity, is my code mistake or tinybus do not support for "post to another activity like eventbus"?

    opened by xiaomeixw 2
  • Calling unregister() on an unregistered object throws NPE

    Calling unregister() on an unregistered object throws NPE

    Hello!

    Would be nice to fail silently or at least to throw an IllegalStateException?

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void de.halfbit.tinybus.impl.ObjectsMeta.unregisterFromReceivers(java.lang.Object, java.util.HashMap)' on a null object reference
    
    TODO 
    opened by renaudcerrato 0
  • Not enough information in error message

    Not enough information in error message

    It would be nice to see both producers: registered and attempted to get registered.

    Caused by: java.lang.IllegalArgumentException: Unable to register producer, because another producer is already registered, TopicsLoader{18ee7275 id=-1666337187} at de.halfbit.tinybus.impl.ObjectsMeta.registerAtProducers(ObjectsMeta.java:231) at de.halfbit.tinybus.TinyBus.processQueue(TinyBus.java:350) at de.halfbit.tinybus.TinyBus.post(TinyBus.java:205) at de.halfbit.tinybus.wires.ConnectivityWire.postEvent(ConnectivityWire.java:169) at de.halfbit.tinybus.wires.ConnectivityWire$1.onReceive(ConnectivityWire.java:112) at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)             at android.os.Handler.handleCallback(Handler.java:739)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:135)             at android.app.ActivityThread.main(ActivityThread.java:5254)             at java.lang.reflect.Method.invoke(Native Method)

    opened by nakvic 0
Releases(v3.0.2)
Owner
Sergej Shafarenka
Android Expert and Software Architect
Sergej Shafarenka
Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components decouples event s

Markus Junginger 24.2k Jan 7, 2023
An enhanced Guava-based event bus with emphasis on Android support.

Otto - An event bus by Square An enhanced Guava-based event bus with emphasis on Android support. Otto is an event bus designed to decouple different

Square 5.2k Jan 9, 2023
Event pattern & event properties framework

Renetik Android - Event & Property https://github.com/renetik/renetik-android-event Documentation Framework to enjoy, improve and speed up your applic

Renetik 2 Nov 2, 2022
The source code for the Bus Scheduler app codelab

Bus Scheduler App This folder contains the source code for the Bus Scheduler app codelab. Introduction The Bus Scheduler app displays a list of bus st

null 0 Nov 8, 2021
A lightweight eventbus library for android, simplifies communication between Activities, Fragments, Threads, Services, etc.

AndroidEventBus This is an EventBus library for Android. It simplifies the communication between Activities, Fragments, Threads, Services, etc. and lo

Mr.Simple 1.6k Nov 30, 2022
A Simple kotlin first EventBus implementation

EventBus Simple event bus for event-driven programming. Taking advantage of kotlin language features instead of typical reflections Table of Contents

Zarzel K. Shih 4 Oct 14, 2022
EventBus for Android,消息总线,基于SharedFlow,具有生命周期感知能力,支持Sticky,支持线程切换,支持延迟发送。

背景 跨页面通信是一个比较常见的场景,通常我们会选择使用EventBus,但EventBus无法感知声明周期,收到消息就会回调,所以有了LiveData之后很快就有了LiveEventBus。不过它也有缺点,比如不能切换线程。

BiuBiuQiu0 167 Jan 3, 2023
PubSub - 使用 Kotlin Coroutines 实现的 Local Pub/Sub、Event Bus、Message Bus

PubSub 使用 Kotlin Coroutines 实现的 Local Pub/Sub、Event Bus、Message Bus 下载 将它添加到项目的

Tony Shen 4 May 30, 2022
Bus Scheduler - The source code for the Bus Scheduler app codelab

Bus Scheduler App This folder contains the source code for the Bus Scheduler app

Dania Puertas 0 Jan 7, 2022
Event bus for Android and Java that simplifies communication between Activities, Fragments, Threads, Services, etc. Less code, better quality.

EventBus EventBus is a publish/subscribe event bus for Android and Java. EventBus... simplifies the communication between components decouples event s

Markus Junginger 24.2k Jan 7, 2023
Android library listening network connection state and change of the WiFi signal strength with event bus

NetworkEvents Android library listening network connection state and change of the WiFi signal strength with event bus. It works with any implementati

Piotr Wittchen 452 Nov 21, 2022
An enhanced Guava-based event bus with emphasis on Android support.

Otto - An event bus by Square An enhanced Guava-based event bus with emphasis on Android support. Otto is an event bus designed to decouple different

Square 5.2k Jan 9, 2023
Event Bus powered by Kotlin Coroutines Flow

FlowBus FlowBus is a kotlin event bus implementation. Powered by Kotlin Coroutines and Flows Android MainThread-aware Fully operable from Java code Ex

Robert Kosakowski 26 Dec 27, 2022
Burp extension to create target specific and tailored wordlist from burp history.

Burp extension to create target specific and tailored wordlist from burp history.

Dexter0us 173 Jan 2, 2023
Fokus - To Do app tailored specifically for students

Fokus - To Do app tailored specifically for students Fokus is an open source application that combines a todo list and a calendar that can help you ma

Isaiah Collins Abetong 154 Dec 24, 2022
This prototype app provides a list of events to be held under an organization (school, college, club, etc.) and the users can manually set event reminders at their scheduled time so that they do not miss an event.

E-CELL NITS Sample App This prototype app provides a list of events to be held under E-Cell NIT Silchar (for example, Srijan 2.0) and the users can ma

Ritam Nath 1 Nov 7, 2021
Event pattern & event properties framework

Renetik Android - Event & Property https://github.com/renetik/renetik-android-event Documentation Framework to enjoy, improve and speed up your applic

Renetik 2 Nov 2, 2022
Okuki is a simple, hierarchical navigation bus and back stack for Android, with optional Rx bindings, and Toothpick DI integration.

Okuki A simple, hierarchical navigation bus and back stack for Android, with optional Rx bindings, and Toothpick integration for automatic dependency-

Cain Wong 143 Nov 25, 2022
This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague

This Android app shows bus connections from Koleje Strahov station to Dejvická station and the other way in the city of Prague. These are important for many students from the Czech Technical University in Prague.

Lašťa Apps 3 Jul 28, 2022
SeatBookView is an Android Studio Library that helps to make it easier to create Bus, Train, Cinema Theater Seat UI and all functionalities are given.

SeatBookView SeatBookView is an Android Studio Library that helps to make it easier to create Bus ?? , Train ?? , Cinema Theater Seat UI and all funct

Md. Zahidul Islam 3 Oct 15, 2022