Skip to content
This repository has been archived by the owner on Jul 4, 2023. It is now read-only.

certinia/orizuru-transport-rabbitmq

Repository files navigation

Orizuru Transport RabbitMQ

Build Status

Orizuru Transport RabbitMQ is a transport library for the Orizuru framework.

It is a thin wrapper around amqplib and allows Orizuru to publish and subscribe to events via RabbitMQ.

Install

npm install @financialforcedev/orizuru-transport-rabbitmq

Usage

Use this dependency to specify the transport layer that @financialforcedev/orizuru uses as RabbitMQ.

// get classes from orizuru
import { Handler, Publisher, Server } from '@financialforcedev/orizuru';

// get the transport
import { Transport } from '@financialforcedev/orizuru-transport-rabbitmq';

// create the transport
const transport = new Transport({
    url:  process.env.CLOUDAMQP_URL || 'amqp://localhost'
});

const server = new Server({
    transport
});

const handler = new Handler({
    transport
});

const publisher = new Publisher({
    transport
});

Messages can be published to a work queue using the publisher...

import { Publisher } from '@financialforcedev/orizuru';
import { Transport } from '@financialforcedev/orizuru-transport-rabbitmq';

const transport = new Transport({
    url:  process.env.CLOUDAMQP_URL || 'amqp://localhost'
});

const app = new Publisher({ transport });

app.publish({
    message: {
        context: {},
        message: {
            test: 'message'
        }
    },
    publishOptions: {
        eventName: 'test.queue'
    }
});

and consumed by the handler.

import { Handler, IOrizuruMessage } from '@financialforcedev/orizuru';
import { Transport } from '@financialforcedev/orizuru-transport-rabbitmq';

const transport = new Transport({
    url:  process.env.CLOUDAMQP_URL || 'amqp://localhost'
});

const app = new Handler({ transport });

app.handle({
    handler: ({ context, message }: IOrizuruMessage<any, any>) => {
        app.info(context);
        app.info(message);
    }),
    schema: {
        namespace: 'testNamespace',
        name: 'testSchema',
        type: 'record',
        fields: [{
            name: 'test',
            type: 'string'
        }]
    },
    subscribeOptions: {
        eventName: 'test.queue'
    }
});

Or via a topic exchange using the publish/subscribe mechanism.

import { Handler, IOrizuruMessage, Publisher } from '@financialforcedev/orizuru';
import { Transport } from '@financialforcedev/orizuru-transport-rabbitmq';

const publisherTransport = new Transport({
    url:  process.env.CLOUDAMQP_URL || 'amqp://localhost'
});

const publisher = new Publisher({ transport: publisherTransport });

publisher.publish({
    message: {
        context: {},
        message: 'test message'
    },
    publishOptions: {
        eventName: 'test.queue',
        exchange: {
            key: 'testKey',
            name: 'testExchange',
            type: 'topic'
        }
    }
});

const handlerTransport = new Transport({
    url:  process.env.CLOUDAMQP_URL || 'amqp://localhost'
});

const app = new Handler({ transport: handlerTransport});

app.handle({
    handler: ({ context, message }: IOrizuruMessage<any, any>) => {
        app.info(context);
        app.info(message);
    }),
    schema: {
        namespace: 'testNamespace',
        name: 'testSchema',
        type: 'record',
        fields: [{
            name: 'test',
            type: 'string'
        }]
    },
    subscribeOptions: {
        eventName: 'test.queue',
        exchange: {
            key: 'testKey',
            name: 'testExchange',
            type: 'topic'
        }
    }
});

Development

Before running npm test, make sure that Docker is installed.

brew cask install docker

npm test is configured to build and start the orizuru-transport-rabbitmq--rabbitmq service, then run the system tests.

API Docs

Click to view TSDoc API documentation.