Coder Social home page Coder Social logo

reactive-source's Introduction

reactive-source

Open source framework to turn your database into a reactive stream of information.

Instead of you querying the database for new events, let the "database" (aka the reactive-source framework) notify you that there is new or updated data available.

Currently supported database systems:

  • Postgres
  • MySQL

Upcoming database systems:

  • MongoDB

Usage and Examples

Using the framework is pretty straight forward.

In the following example you can see how you can start getting (next to real-time) events for a Mysql table.

//Define an event listener that receives events with meaningful objects
class PurchaseEventListener extends EventListener<Purchase> {
    PurchaseEventListener(PurchaseEntityExtractor extractor) {
        super(extractor);
    }

    public void onEvent(Event<Purchase> event) {
        //apply your business logic for the new event here
    }
}

//Define an extractor that converts data in maps to your business object
class PurchaseEntityExtractor implements EntityExtractor<Purchase> {
    @Override public Purchase extractEntity(Map<String, Object> entityData) {
        Purchase purchase = new Purchase();
        // create the purchase object from a map of data
        return purchase;
    }
}

//Define your event source
MysqlEventSource eventSource = new MysqlEventSource(URL, USERNAME, PASSWORD, TABLE_NAME);
//Create a reactiveSource
ReactiveSource<Purchase> reactiveSource = new ReactiveSource<>(eventSource);
reactiveSource.addEventListener(new PurchaseEventListener(new PurchaseEntityExtractor()));

//start the reactive source
reactiveSource.start();

// YEYY!! you are getting events

//stop the reactive source when you don't need it any more
reactiveSource.stop();

This example was for a MySQL database.

All you would need to change to use another database (ie PostgreSQL) would be creating a PsqlEventSource instead of a MysqlEventSource and passing it to the ReactiveSource.

Provided the same classes exist here is :

//Define your event source
PsqlEventSource eventSource = new PsqlEventSource(URL, USERNAME, PASSWORD, TABLE_NAME); //only line that changed
//Create a reactiveSource
ReactiveSource<Purchase> reactiveSource = new ReactiveSource<>(eventSource);
reactiveSource.addEventListener(new PurchaseEventListener(new PurchaseEntityExtractor()));

//start the reactive source
reactiveSource.start();

// YEYY!! you are getting events

//stop the reactive source when you don't need it any more
reactiveSource.stop();

What if I want a source that is not supported?

If you have ideas about what other types of sources we could support feel free to contact us at [email protected] (Kostas) or [email protected] (Xavi)

Even better, you could implement the add in for the new source type yourself and thus contribute to our framework.

We will be happy to hear from you in case you want to contribute.

reactive-source's People

Stargazers

 avatar Ryan J. McDonough avatar Sergei Egorov avatar gaurav patel avatar Radek Scheibinger avatar

Watchers

James Cloos avatar

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.