|
| 1 | +import { Body, Controller, HttpCode, OnModuleInit, Post } from '@nestjs/common'; |
| 2 | +import { Logger } from '@nestjs/common/services/logger.service'; |
| 3 | +import { Client, ClientKafka, Transport } from '@nestjs/microservices'; |
| 4 | +import { Observable } from 'rxjs'; |
| 5 | +import { BusinessDto } from './dtos/business.dto'; |
| 6 | +import { UserDto } from './dtos/user.dto'; |
| 7 | + |
| 8 | +@Controller() |
| 9 | +export class KafkaController implements OnModuleInit { |
| 10 | + protected readonly logger = new Logger(KafkaController.name); |
| 11 | + static IS_NOTIFIED = false; |
| 12 | + static MATH_SUM = 0; |
| 13 | + |
| 14 | + @Client({ |
| 15 | + transport: Transport.KAFKA, |
| 16 | + options: { |
| 17 | + client: { |
| 18 | + brokers: ['localhost:9092'], |
| 19 | + }, |
| 20 | + }, |
| 21 | + }) |
| 22 | + private readonly client: ClientKafka; |
| 23 | + |
| 24 | + onModuleInit() { |
| 25 | + const requestPatterns = [ |
| 26 | + 'math.sum.sync.kafka.message', |
| 27 | + 'math.sum.sync.without.key', |
| 28 | + 'math.sum.sync.plain.object', |
| 29 | + 'math.sum.sync.array', |
| 30 | + 'math.sum.sync.string', |
| 31 | + 'math.sum.sync.number', |
| 32 | + 'user.create', |
| 33 | + 'business.create', |
| 34 | + ]; |
| 35 | + |
| 36 | + requestPatterns.forEach(pattern => { |
| 37 | + this.client.subscribeToResponseOf(pattern); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + // sync send kafka message |
| 42 | + @Post('mathSumSyncKafkaMessage') |
| 43 | + @HttpCode(200) |
| 44 | + async mathSumSyncKafkaMessage( |
| 45 | + @Body() data: number[], |
| 46 | + ): Promise<Observable<any>> { |
| 47 | + const result = await this.client |
| 48 | + .send('math.sum.sync.kafka.message', { |
| 49 | + key: '1', |
| 50 | + value: { |
| 51 | + numbers: data, |
| 52 | + }, |
| 53 | + }) |
| 54 | + .toPromise(); |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + // sync send kafka(ish) message without key and only the value |
| 59 | + @Post('mathSumSyncWithoutKey') |
| 60 | + @HttpCode(200) |
| 61 | + async mathSumSyncWithoutKey( |
| 62 | + @Body() data: number[], |
| 63 | + ): Promise<Observable<any>> { |
| 64 | + const result = await this.client |
| 65 | + .send('math.sum.sync.without.key', { |
| 66 | + value: { |
| 67 | + numbers: data, |
| 68 | + }, |
| 69 | + }) |
| 70 | + .toPromise(); |
| 71 | + return result; |
| 72 | + } |
| 73 | + |
| 74 | + // sync send message without key or value |
| 75 | + @Post('mathSumSyncPlainObject') |
| 76 | + @HttpCode(200) |
| 77 | + async mathSumSyncPlainObject( |
| 78 | + @Body() data: number[], |
| 79 | + ): Promise<Observable<any>> { |
| 80 | + const result = await this.client |
| 81 | + .send('math.sum.sync.plain.object', { |
| 82 | + numbers: data, |
| 83 | + }) |
| 84 | + .toPromise(); |
| 85 | + return result; |
| 86 | + } |
| 87 | + |
| 88 | + // sync send message without key or value |
| 89 | + @Post('mathSumSyncArray') |
| 90 | + @HttpCode(200) |
| 91 | + async mathSumSyncArray(@Body() data: number[]): Promise<Observable<any>> { |
| 92 | + const result = await this.client |
| 93 | + .send('math.sum.sync.array', data) |
| 94 | + .toPromise(); |
| 95 | + return result; |
| 96 | + } |
| 97 | + |
| 98 | + @Post('mathSumSyncString') |
| 99 | + @HttpCode(200) |
| 100 | + async mathSumSyncString(@Body() data: number[]): Promise<Observable<any>> { |
| 101 | + // this.logger.error(util.format('mathSumSyncString() data: %o', data)); |
| 102 | + const result = await this.client |
| 103 | + .send('math.sum.sync.string', data.toString()) |
| 104 | + .toPromise(); |
| 105 | + return result; |
| 106 | + } |
| 107 | + |
| 108 | + @Post('mathSumSyncNumber') |
| 109 | + @HttpCode(200) |
| 110 | + async mathSumSyncNumber(@Body() data: number[]): Promise<Observable<any>> { |
| 111 | + const result = await this.client |
| 112 | + .send('math.sum.sync.number', data[0]) |
| 113 | + .toPromise(); |
| 114 | + return result; |
| 115 | + } |
| 116 | + |
| 117 | + // async notify |
| 118 | + @Post('notify') |
| 119 | + async sendNotification(): Promise<any> { |
| 120 | + return this.client.emit('notify', { notify: true }); |
| 121 | + } |
| 122 | + |
| 123 | + // Complex data to send. |
| 124 | + @Post('/user') |
| 125 | + @HttpCode(200) |
| 126 | + async createUser(@Body() user: UserDto): Promise<Observable<any>> { |
| 127 | + const result = await this.client |
| 128 | + .send('user.create', { |
| 129 | + key: '1', |
| 130 | + value: { |
| 131 | + user, |
| 132 | + }, |
| 133 | + }) |
| 134 | + .toPromise(); |
| 135 | + return result; |
| 136 | + } |
| 137 | + |
| 138 | + // Complex data to send. |
| 139 | + @Post('/business') |
| 140 | + @HttpCode(200) |
| 141 | + async createBusiness(@Body() business: BusinessDto) { |
| 142 | + const result = await this.client |
| 143 | + .send('business.create', { |
| 144 | + key: '1', |
| 145 | + value: { |
| 146 | + business, |
| 147 | + }, |
| 148 | + }) |
| 149 | + .toPromise(); |
| 150 | + return result; |
| 151 | + } |
| 152 | +} |
0 commit comments