Nestjs RabbitMQ无法发送到队列

htrmnn0y  于 11个月前  发布在  RabbitMQ
关注(0)|答案(1)|浏览(168)

我知道有类似的问题,但到目前为止没有对我起作用。我有一个NestJS项目,它应该得到一个HTTP请求并将其发送到RabbitMQ。RabbitMQ已经设置好,一个监听C#脚本可以连接到它。我在RabbitMQ的Web界面中看到了连接。但是当我在AppService中调用函数sendIntoQueue()时,什么也没有发生。没有那么我错过了什么吗?
这个应用程序没有收到任何东西,这是正确的.它应该只发送到队列.有main()应该看起来正确?
这些是我的档案:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { MicroserviceOptions, Transport } from '@nestjs/microservices';
import { ConfigService } from '@nestjs/config';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  app.listen(3000);
}
bootstrap();
import { Module } from "@nestjs/common";
import { AppController } from "./app.controller";
import { AppService } from "./app.service";
import { ClientsModule, Transport } from "@nestjs/microservices";

const user = 'quest';
const password = 'quest';
const host = '127.0.0.1:5672';
const queueName = 'dev';
@Module({
  imports: [
    ClientsModule.registerAsync([
      {
        name: "RMQ_CLIENT",
        imports: [],
        useFactory: () => ({
          transport: Transport.RMQ,
          options: {
            urls: [`amqp://${user}:${password}@${host}`],
            queue: queueName,
            queueOptions: {
              durable: false,
            },
          },
        }),
      }
    ])
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}
import { Inject, Injectable } from "@nestjs/common";
import { PdfRequest } from "./model/pdf-request";
import { ClientProxy } from "@nestjs/microservices";
import { PdfEvent } from "./events/pdf-event";
import { PdfEventPattern } from "./events/pdf-event-pattern";

@Injectable()
export class AppService {
  private readonly queue: any[] = [];

  constructor(@Inject("RMQ_CLIENT") private readonly pdfClient: ClientProxy) {
  }

  async sendIntoQueue(pdfRequest: PdfRequest) {
    try {
      this.queue.push(pdfRequest);
      let result = this.pdfClient.send("test", {"test": "test"});
    } catch (e){
      console.log(e);
    }
  }
}
kwvwclae

kwvwclae1#

您的问题可能是send返回了一个rxjs Observable,但您正在丢弃它,并且它从未被订阅。

this.pdfClient
   .send("test", {"test": "test"})
   .subscribe(response => {
       console.log('Response:', JSON.stringify(response));
  });

字符串

相关问题