NestJs bull 用法

2024-08-31 07:52
文章标签 用法 nestjs bull

本文主要是介绍NestJs bull 用法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

bull简介

队列 bull

bull用法

https://github.com/OptimalBits/bull

Bull is currently in maintenance mode, we are only fixing bugs. For new features check BullMQ, a modern rewritten implementation in Typescript. You are still very welcome to use Bull if it suits your needs, which is a safe, battle tested librarySourceURL:https://github.com/OptimalBits/bull?tab=readme-ov-file 公牛目前处于维护模式,我们只是修复错误。有关新特性,请检查 BullMQ,这是一个用 Typecript 重写的现代实现。如果符合您的需要,您仍然非常欢迎使用 Bull,这是一个安全的、经过战斗考验的库。

真正用法用bullMq参考bullMq

bull参考bull

不过概念理解知识内容,多参考bullMq,文档更加丰富

https://github.com/taskforcesh/bullmq

What is BullMQ | BullMQ

@Injectable()
export class QueueConfigurationService implements OnApplicationBootstrap {constructor() {}private queueRegistry = new Map<string, Queue>();async onApplicationBootstrap() {const redisQueueConfig = {host: process.env['REDIS_HOST'],port: Number(process.env['REDIS_PORT']),password: process.env['REDIS_PASSWORD'],connectionTimeout: 30000,db: 4,};try {const standardQueue = new Queue('StandardQueue', { redis: redisQueueConfig });this.queueRegistry.set('StandardQueue', standardQueue);} catch (error) {throw error;}}async getQueueByLabel(queueLabel: string): Promise<Queue | undefined> {return this.queueRegistry.get(queueLabel);}
}

// 在其他地方使用
this.taskQueue = new Queue(serviceQueueName, {redis: {host: process.env['REDIS_HOST'],port: Number(process.env['REDIS_PORT']),password: process.env['REDIS_PASSWORD'],connectTimeout: 30000,db: 4,},
});
this.taskQueue.process(taskSubscriptionName, async (job, done) => {});

Nestjs bull用法

Documentation | NestJS - A progressive Node.js framework

import { Injectable, Inject } from '@nestjs/common';
import { InjectRedis } from 'nestjs-redis';
import { InjectQueue, BullModule } from '@nestjs/bull';
import { BaseService } from './base.service';
import { TypeOrmModule } from '@nestjs/typeorm';// 常量定义
const NORMAL_QUEUE_NAME = 'normal_queue';
const PRIORITY_QUEUE_NAME = 'priority_queue';
const TEST_QUEUE_NAME = 'test_queue';// 实现 OnApplicationBootstrap 接口
@Injectable()
export class QueueConfigService {// 构造函数constructor(@InjectRedis('redisDistribute') private readonly redisDistribute: any,@InjectQueue(NORMAL_QUEUE_NAME) private readonly normalQueue: any,@InjectQueue(PRIORITY_QUEUE_NAME) private readonly priorityQueue: any,@InjectQueue(TEST_QUEUE_NAME) private readonly testQueue: any,private readonly base: BaseService) {}// 根据队列名称返回对应的队列实例getQueueByName(queueName: string): any {const queueMap = {[NORMAL_QUEUE_NAME]: this.normalQueue,[PRIORITY_QUEUE_NAME]: this.priorityQueue,[TEST_QUEUE_NAME]: this.testQueue,};return queueMap[queueName];}
}

// 模块定义
@Module({imports: [BullModule.registerQueueAsync([{name: NORMAL_QUEUE_NAME,useFactory: () => ({redis: {host: process.env['REDIS_HOST'],port: +process.env['REDIS_PORT'],password: process.env['REDIS_PASSWORD'],},}),},{name: PRIORITY_QUEUE_NAME,useFactory: () => ({redis: {host: process.env['REDIS_HOST'],port: +process.env['REDIS_PORT'],password: process.env['REDIS_PASSWORD'],},}),},{name: TEST_QUEUE_NAME,useFactory: () => ({redis: {host: process.env['REDIS_HOST'],port: +process.env['REDIS_PORT'],password: process.env['REDIS_PASSWORD'],},}),},]),BaseModule,],providers: [QueueConfigService],exports: [QueueConfigService],
})
export class QueueConfigModule {}

queueMap[TEST_QUEUE_NAME].add('test', 20);

@Processor('TEST_QUEUE_NAME')
export class QueneService {constructor(@InjectQueue('task') private readonly taskQueue: Queue) {}@Process('test')async processTask(job: Job<number>) {console.log('Processing', job);console.log('Processing done', job.id);}
}

这篇关于NestJs bull 用法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1123311

相关文章

bytes.split的用法和注意事项

当然,我很乐意详细介绍 bytes.Split 的用法和注意事项。这个函数是 Go 标准库中 bytes 包的一个重要组成部分,用于分割字节切片。 基本用法 bytes.Split 的函数签名如下: func Split(s, sep []byte) [][]byte s 是要分割的字节切片sep 是用作分隔符的字节切片返回值是一个二维字节切片,包含分割后的结果 基本使用示例: pa

UVM:callback机制的意义和用法

1. 作用         Callback机制在UVM验证平台,最大用处就是为了提高验证平台的可重用性。在不创建复杂的OOP层次结构前提下,针对组件中的某些行为,在其之前后之后,内置一些函数,增加或者修改UVM组件的操作,增加新的功能,从而实现一个环境多个用例。此外还可以通过Callback机制构建异常的测试用例。 2. 使用步骤         (1)在UVM组件中内嵌callback函

这些ES6用法你都会吗?

一 关于取值 取值在程序中非常常见,比如从对象obj中取值 const obj = {a:1b:2c:3d:4} 吐槽: const a = obj.a;const b = obj.b;const c = obj.c;//或者const f = obj.a + obj.b;const g = obj.c + obj.d; 改进:用ES6解构赋值

2021-8-14 react笔记-2 创建组件 基本用法

1、目录解析 public中的index.html为入口文件 src目录中文件很乱,先整理文件夹。 新建components 放组件 新建assets放资源   ->/images      ->/css 把乱的文件放进去  修改App.js 根组件和index.js入口文件中的引入路径 2、新建组件 在components文件夹中新建[Name].js文件 //组件名首字母大写

Cmake之3.0版本重要特性及用法实例(十三)

简介: CSDN博客专家、《Android系统多媒体进阶实战》一书作者 新书发布:《Android系统多媒体进阶实战》🚀 优质专栏: Audio工程师进阶系列【原创干货持续更新中……】🚀 优质专栏: 多媒体系统工程师系列【原创干货持续更新中……】🚀 优质视频课程:AAOS车载系统+AOSP14系统攻城狮入门视频实战课 🚀 人生格言: 人生从来没有捷径,只有行动才是治疗恐惧

关于断言的部分用法

1、带变量的断言  systemVerilog assertion 中variable delay的使用,##[variable],带变量的延时(可变延时)_assertion中的延时-CSDN博客 2、until 的使用 systemVerilog assertion 中until的使用_verilog until-CSDN博客 3、throughout的使用   常用于断言和假设中的

ExpandableListView的基本用法

QQ上的好友列表在Android怎么实现,有一个最简单的方法,那就是ExpandableListView,下面简单介绍一下ExpandableListview的用法。 先看看效果图,没有找到大小合适的图片,所以凑合着看吧。     一、准备工作(界面,和需要的数据)             <? xml   version = "1.0"   encoding =

Hbase 查询相关用法

Hbase 查询相关用法 public static void main(String[] args) throws IOException {//Scan类常用方法说明//指定需要的family或column ,如果没有调用任何addFamily或Column,会返回所有的columns; // scan.addFamily(); // scan.addColumn();// scan.se

BeanUtils.copyProperties()在不同包下,用法不同!!! 切记!!!

用法一: 在import org.springframework.beans.BeanUtils;包下: <span style="white-space:pre"> </span>//赋值vo对象的值到po中 <span style="white-space:pre"> </span>/** <span style="white-space:pre"> </span>* <spa

Mybatis注解用法

MyBatis(八) mybatis注解 一、mybatis简单注解 1、@Select、@Results、@Result2、@Delete、@Param、@ResultMap3、@Insert、@SelectKey4、@Delete、@Param5、@Update二、动态SQL 1、简单处理,直接使用``脚本2、使用Provider注解标识 2.1、创建Provider类2.2、注解使用Prov