necessary-utils-basic-queue

Necessary utils for the basic message queue in the internal system.

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
necessary-utils-basic-queue
101.3.24 months ago4 months agoMinified + gzip package size for necessary-utils-basic-queue in KB

Readme

Necessary Utils | Basic Queue

Version License Downloads
<img src="https://img.shields.io/npm/dt/necessary-utils-basic-queue" alt="Downloads">

Getting started

Yarn

yarn add necessary-utils-basic-queue

NPM

npm install necessary-utils-basic-queue --save

Example

import {
  BasicQueue,
  BasicQueueMessage,
  QueueSettings,
} from "necessary-utils-basic-queue";

class ExampleMessage extends BasicQueueMessage {
  public MessageDetail: string;
  constructor(messageDetail: string) {
    this.MessageDetail = messageDetail;
  }
}

/**
 * Queue Created
 */
const queue = new BasicQueue<ExampleMessage>();

/**
 * Consumer Method Registered
 * Auto ACK = true
 */
queue.Consume((message: ExampleMessage) => {
  console.log(message.MessageDetail);
  // Do anything...
}, /*AUTO ACK = */ true);

/**
 * Start Produce
 */
queue.PushMessage(
  new ExampleMessage("Hello. This is my first message on the queue.")
);

How its works ?

Overview

graph
    Producer1 --> |Message 1| Queue;
    Producer2 --> |Message 2| Queue;
    Producer3 --> |Message 3| Queue;

Queue --> |Messages One by One| Consumer;

Consuming

flowchart LR
    Producer1 --> |Message 1| Queue;
    Producer1 --> |Message 2| Queue;
    Producer1 --> |Message 3| Queue;
    Queue --> |Next Message| Consumer;
    Consumer --> EndProcess;
    EndProcess --> ACK{Auto Ack is True};
    ACK --> |True| Queue;
    ACK --> |False| WaitAck;
    WaitAck --> Queue;

Interceptors

WhenAnyMessage

Returns exception if message count greater than 0 in the queue object.
Example
queue.Intercept.WhenAnyMessage();

WhenZeroMessage

Returns exception if message count equals 0 in the queue object.
Example
queue.Intercept.WhenZeroMessage();

WhenQueueStopped

Returns exception if queue object is idle position.
Example
queue.Intercept.WhenQueueStopped();

WhenQueueStarted

Returns exception if queue object is not idle position.
Example
queue.Intercept.WhenQueueStarted();

Custom Query

Example
queue.Intercept.When(
  async (
    messageCount: number,
    currentMessage: T | null
  ): Promise<boolean> => {
    const validation = currentMessage.MessageDetail.length == 10;
    return validation;
  }
);

Multiple Using

Example
queue.Intercept.WhenQueueStopped().WhenAnyMessage().***;