mercury-messenger
Messaging Helper for Event Driven Systems
Last updated 2 months ago by cl3dson .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mercury-messenger 
SYNC missed versions from official npm registry.

CircleCI Codacy coverage Codacy grade Npm Npm

Mercury

Mercury is a tool for managing inter-service communication in distributed systems based in EDA (Event Driven Architecture). Mercury is intended for systems that relies in message brokers for asynchronous messaging.All Broker based configuration needed to process messages, like queues, topics, binds, listeners, handlers, retry are set up by Mercury under the hood and decoupled from your application.Mercury main purpose is for developers use their times to write business logic rather than messaging or broker specific code.

How it works

Mercury uses the publish/subscribe pattern for deliver messages to the correct service.You application just need to define the handlers functions for interested "events" or messages that occurs within the message ecosystem.

Quick Example

Message Consumers

To begin consuming messages, Start Mercury, providing the broker configuration values in your index file:

import Mercury, { Container } from 'mercury-messenger';
import './testHandler';

let mercury = new Mercury('RABBITMQ', 'localhost', 'user', 'password', 'testApp', 'testService');

mercury.init();

create class handlers for the corresponding event/message

import { MessageHandler, Handler, JSONMessage } from 'mercury-messenger';

@MessageHandler('user-created')
class UserCreatedHandler implements Handler {
    handle(msg) {
        console.log('A user has been created');

        // if a error occurs during execution, this message will be retried
        throw new Error('something gone wrong');
    }
}

@MessageHandler('order-created')
class OrderCreatedHandler implements Handler {
    handle(msg) {
        console.log('Something has been ordered');

        let msgContent = msg.getContent();

        /*your business rule (service call, database operations, etc ...)*/

        return new JSONMessage('product-purchased', { test: 'data' });
    }
}

And finnaly register handlerClasses in Mercury instance and init mercury after that:

const container = new Container();

container.bind('BIND1').to(OrderCreatedHandler);
container.bind('BIND2').to(UserCreatedHandler);

mercury.setContainer(container);

mercury.init();

Now, messages published in the broker by Mercury are correctly routed and consumed by subscribed applications.

Message Producers

To publish messages just use @MessagePublisher decorator in any method,return a new Mercury message in it and it's done.

import { MessagePublisher, JSONMessage } from 'mercury-message';

export class OrderController {
    @MessagePublisher()
    public async createOrderCommand(request){
        await newOrder = orderDatabaseService.save(request.data)
        return new JSONMessage('order-created', newOrder)
    }
}

Mercury Configuration

You must provide some information regarding broker credentials and names for your service

let mercury = new Mercury(applicationName, brokerHostName, brokerUserName, brokerPassword, serviceName, retryDelay);

List of mercury constructor parameters:

type description
applicationName string A descriptor/name for your entire distributed system (only services defined with the same 'appName' communicate with each other)
serviceName string A descriptor/name for the current service
brokerType string Here we define the message broker used (only rabbitmq supported for now)
brokerAddress string Message broker address
brokerUser string message broker admin credentials
brokerPassword string message broker password por provided user
retryDelay (optional) number the default delay in seconds during retries (default 60)

Mercury Initialization

mercury.init();

The init() method start the process to configure broker with the provided configuration and registering handlers as well.It's important to call this method only when all Mercury configuration is done (please be aware that handlers instances must be added to Mercury before using the userHandler() method)

Message Handlers

Mercury needs MessageHandlers to work, message handlers are special classes that provide information about what events or messages the application is interested and provide the logic to run when some message occur in the broker. Use class decorator @MessageHandler and implements the Handler class to define proper messageHandlers compatible with Mercury. Implementing Handler class implies the implementation of the handle() method in your class. Register message handlers instances using the userHandler() method prior to Mercury initialization.

Errors

If an error is thown during the message handling, the message will not be acknowledged in the message broker,and will be retried some time later.

@MessageHandler('user-created')
class UserCreatedHandler implements Handler {
    handle(msg) {
        console.log('A user has been created');

        // if a error occurs during execution, this message will be retried
        throw new Error('something gone wrong');
    }
}

Be careful with database logic or external services invocation here, Always find some way to rollback or revert previous operations before handler finish if the operations aren't idempotent. You can define the delay between retries in Mercury constructor.

Succeeded Handlers

If there is no errors during handler execution, then any Message or array of Messages returned by the handler function will be published into the system messaging ecosystem and possibly consumed by others subscribers services.

@MessageHandler('order-created')
class OrderCreatedHandler implements Handler {
    handle(msg) {
        console.log('Something has been ordered');

        let msgContent = msg.getContent();

        /*your business rule (service call, database operations, etc ...)*/

        return new JSONMessage('product-purchased', { test: 'data' });
    }
}

Publishers

Message publish are intended to only procuce new Mercury messages and publish then into the broker. To publish messages to the broker Just use the '@MessagePublisher' decorator and return a new message:

import { MessagePublisher, JSONMessage } from 'mercury-message';

export class OrderController {
    @MessagePublisher()
    public async createOrderCommand(request){
        await newOrder = orderDatabaseService.save(request.data)
        return new JSONMessage('order-created', newOrder)
    }
}

TODO

  • Exponential retry strategy with max retry using DLE (dead letter)

  • Register all messages received and published to external databases like MongoDB or Redis (event-store)

  • Implement some mechanism to ensure that the publishing of new messages happens only when some pre-registered conditions have been succeeded. (Like publishing the 'order-created' message only when client code commit the transaction to 'order-database' successfully)

  • Support to more message brokers like Redis, Apache ActiveMQ, Apache Kafka.

Current Tags

  • 2.1.0                                ...           latest (2 months ago)

81 Versions

  • 2.1.0                                ...           2 months ago
  • 2.0.13                                ...           6 months ago
  • 2.0.12                                ...           6 months ago
  • 2.0.11                                ...           6 months ago
  • 2.0.10                                ...           6 months ago
  • 2.0.9                                ...           7 months ago
  • 2.0.6                                ...           8 months ago
  • 2.0.5                                ...           8 months ago
  • 2.0.3                                ...           8 months ago
  • 2.0.2                                ...           8 months ago
  • 2.0.1                                ...           8 months ago
  • 2.0.0                                ...           8 months ago
  • 1.1.30                                ...           8 months ago
  • 1.1.29                                ...           9 months ago
  • 1.1.26                                ...           9 months ago
  • 1.1.25                                ...           9 months ago
  • 1.1.24                                ...           9 months ago
  • 1.1.23                                ...           9 months ago
  • 1.1.22                                ...           9 months ago
  • 1.1.21                                ...           9 months ago
  • 1.1.20                                ...           9 months ago
  • 1.1.19                                ...           9 months ago
  • 1.1.18                                ...           9 months ago
  • 1.1.17                                ...           9 months ago
  • 1.1.16                                ...           9 months ago
  • 1.1.15                                ...           9 months ago
  • 1.1.14                                ...           9 months ago
  • 1.1.13                                ...           9 months ago
  • 1.0.28                                ...           9 months ago
  • 1.1.10                                ...           9 months ago
  • 1.1.9                                ...           9 months ago
  • 1.1.8                                ...           9 months ago
  • 1.1.7                                ...           9 months ago
  • 1.1.6                                ...           9 months ago
  • 1.1.5                                ...           9 months ago
  • 1.1.4                                ...           9 months ago
  • 1.1.3                                ...           9 months ago
  • 1.1.2                                ...           9 months ago
  • 1.1.1                                ...           9 months ago
  • 1.0.51                                ...           9 months ago
  • 1.0.49                                ...           9 months ago
  • 1.0.48                                ...           9 months ago
  • 1.0.47                                ...           9 months ago
  • 1.0.46                                ...           9 months ago
  • 1.0.45                                ...           9 months ago
  • 1.0.44                                ...           9 months ago
  • 1.0.43                                ...           9 months ago
  • 1.0.42                                ...           9 months ago
  • 1.0.41                                ...           9 months ago
  • 1.0.40                                ...           9 months ago
  • 1.0.39                                ...           9 months ago
  • 1.0.38                                ...           9 months ago
  • 1.0.37                                ...           9 months ago
  • 1.0.36                                ...           9 months ago
  • 1.0.35                                ...           9 months ago
  • 1.0.34                                ...           9 months ago
  • 1.0.33                                ...           9 months ago
  • 1.0.32                                ...           9 months ago
  • 1.0.31                                ...           9 months ago
  • 1.0.30                                ...           9 months ago
  • 1.0.27                                ...           9 months ago
  • 1.0.22                                ...           9 months ago
  • 1.0.21                                ...           9 months ago
  • 1.0.20                                ...           9 months ago
  • 1.0.19                                ...           9 months ago
  • 1.0.18                                ...           9 months ago
  • 1.0.17                                ...           9 months ago
  • 1.0.16                                ...           9 months ago
  • 1.0.15                                ...           9 months ago
  • 1.0.13                                ...           9 months ago
  • 1.0.12                                ...           9 months ago
  • 1.0.11                                ...           9 months ago
  • 1.0.10                                ...           9 months ago
  • 1.0.9                                ...           9 months ago
  • 1.0.8                                ...           9 months ago
  • 1.0.7                                ...           9 months ago
  • 1.0.6                                ...           9 months ago
  • 1.0.5                                ...           9 months ago
  • 1.0.4                                ...           9 months ago
  • 1.0.3                                ...           9 months ago
  • 1.0.2                                ...           9 months ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 81
Dependencies (5)
Dev Dependencies (24)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |