Giter Club home page Giter Club logo

newsboy's Introduction

NewsBoy

NewsBoy is a library that provides publish-subscribe style communication between Java components.

NewsBoy mimics EventBus from Google Guava, but has several differences:

  • it uses weak references
  • it is asynchronous
  • it is concurrent

Features

  1. Listeners are stored using WeakReferences to prevent memory leaks when they are not unsubscribed.
  2. Uses fork-join framework for concurrent event delivery.
  3. Publish methods returns collection of Futures that represent event notifications.
  4. Supports ordered publishing: guaranteed to notify of the events in order they were published.
  5. Provides EventSource and EventService interfaces for better integration with IOC containers and alternative implementations.

Dependencies

  1. Java SE 7
  2. Apache Commons Lang 3
  3. JUnit4 for tests

Maven

    <dependency>
        <groupId>com.elusive-code.newsboy</groupId>
        <artifactId>NewsBoy</artifactId>
        <version>0.2</version>
    </dependency>

Usage

  1. Create instance of AsyncEventService
  2. Create listener object: it should have public method(s) with zero or one argument marked with @Subscribe
  3. Subscribe listener object using AsyncEventService.subscribe() method
  4. Publish event using AsyncEventService.publish() or AsyncEventService.publishOrdered()

Example

    public class SimpleExample {

        public static class Event {
            private String eventInfo;

            public Event(String eventInfo) {
                this.eventInfo = eventInfo;
            }

            public String getEventInfo() {
                return eventInfo;
            }
        }

        public static class Listener {

            @Subscribe
            public void printEvent(Event event){
                System.out.println("Something happens: " + event.getEventInfo());
            }
        }

        public static void main(String[] args) {

            // initial setup
            // create event service and listener
            EventService eventService = new AsyncEventService();
            Listener listener = new Listener();
            eventService.subscribe(listener);

            //event publishing
            //results will contain java.util.concurrent.Future that represent notifications
            Collection<NotificationFuture> results = eventService.publish(new Event("Some event"));

            //this will make us wait until all listeners processed event
            for (NotificationFuture future: results) {
                try{
                    future.get();
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            }
        }

    }

License

Apache Software License 2.0.

Copyright (C) 2014, Vladislav Dolgikh.

See LICENSE for details.

newsboy's People

Watchers

Vlad avatar

newsboy's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.