Giter VIP home page Giter VIP logo

xerj.eventstack's Introduction

Table of contents

Overview

Simple event handling library!

This project composes of components for implementing the event handling parts of the DDD/CQRS pattern. This library was built with simplicity, modularity and pluggability in mind.

Features

  • Send events to one or many registered event handlers.
  • Multiple ways of registering event handlers:
    • Simple registration (no IoC container).

    • IoC container registration

      • achieved by creating implementations of EventHandlerProvider:
        • Spring Context

          Maven Central

        • Guice

          Maven Central

    • Attribute registration (Soon!)

      • achieved by marking methods with @EventHandler annotations.

Installation

  • You can simply clone this repository, build the source, reference the jar from your project, and code away!

  • XerJ.EventStack is also available in the Maven Central:

    Maven Central

Getting Started

Sample Command and Command Handler

// Example command.
public class ProductRegisteredEvent
{
    private final int productId;
    private final String productName;

    public ProductRegisteredEvent(int productId, String productName) 
    {
        this.productId = productId;
        this.productName = productName;
    }

    public int getProductId() {
        return productId;
    }

    public String getProductName() {
        return productName;
    }
}

// Event handler 1.
public class ProductRegisteredNotificationHandler : EventHandler<ProductRegisteredEvent>
{
    private final NotificationService notificationService;

    public ProductRegisteredNotificationHandler(NotificationService notificationService)
    {
        this.notificationService = notificationService;
    }

    @Override
    public CompletableFuture<Void> handle(ProductRegisteredEvent event)
    {
        return notificationService.notify("Product registered! - " + event.getProductName());
    }
}

... Other event handlers

Event Handler Registration

Before we can dispatch any events, first we need to register our event handlers. There are several ways to do this:

1. Simple Registration (No IoC container)

public static void main(String[] args)
{
    RegistryEventHandlerProvider provider = new RegistryEventHandlerProvider(registry -> {
        registry.registerEventHandler(ProductRegisteredEvent.class, () -> new ProductRegisteredNotificationHandler(
            new EmailNotificationService()
        ));
    });

    EventDispatcher dispatcher = new EventDispatcher(provider);
    
    // Dispatch event.
    CompletableFuture<Void> future = dispatcher.send(new ProductRegisteredEvent(1, "My Product Name"));
}

2. Container Registration

Spring Context

public static void main(String[] args)
{ 
    ApplicationContext appContext = new AnnotationConfigApplicationContext(BeanConfigs.class);

    SpringContextEventHandlerPovider provider = new SpringContextEventHandlerPovider(appContext);

    EventDispatcher dispatcher = new EventDispatcher(provider);

    // Dispatch event.
    CompletableFuture<Void> future = dispatcher.send(new ProductRegisteredEvent(1, "My Product Name"));
}

Guice

public static void main(String[] args)
{ 
    Injector injector = Guice.createInjector(new AppModule());

    GuiceEventHandlerPovider provider = new GuiceEventHandlerPovider(injector);

    EventDispatcher dispatcher = new EventDispatcher(provider);

    // Dispatch event.
    CompletableFuture<Void> future = dispatcher.send(new ProductRegisteredEvent(1, "My Product Name"));
}

xerj.eventstack's People

Contributors

joel-jeremy avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  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.