Skip to content

Commit bba86dc

Browse files
committed
Removes @MessageRequest decorator in favor of subscribeToResponseOf() method.
1 parent 1ff60ee commit bba86dc

12 files changed

Lines changed: 81 additions & 289 deletions

integration/microservices/src/kafka/kafka.controller.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
import * as util from 'util';
2-
import { Body, Controller, HttpCode, Post, Query } from '@nestjs/common';
1+
import { Body, Controller, HttpCode, Post, OnModuleInit } from '@nestjs/common';
32
import {
43
Client,
5-
ClientProxy,
64
Transport,
7-
MessageRequest,
5+
ClientKafka,
86
} from '@nestjs/microservices';
97
import { Logger } from '@nestjs/common/services/logger.service';
108

@@ -13,7 +11,7 @@ import { UserDto } from './dtos/user.dto';
1311
import { BusinessDto } from './dtos/business.dto';
1412

1513
@Controller()
16-
export class KafkaController {
14+
export class KafkaController implements OnModuleInit {
1715
protected readonly logger = new Logger(KafkaController.name);
1816
static IS_NOTIFIED = false;
1917
static MATH_SUM = 0;
@@ -26,12 +24,28 @@ export class KafkaController {
2624
},
2725
},
2826
})
29-
private readonly client: ClientProxy;
27+
private readonly client: ClientKafka;
28+
29+
onModuleInit(){
30+
const requestPatterns = [
31+
'math.sum.sync.kafka.message',
32+
'math.sum.sync.without.key',
33+
'math.sum.sync.plain.object',
34+
'math.sum.sync.array',
35+
'math.sum.sync.string',
36+
'math.sum.sync.number',
37+
'user.create',
38+
'business.create',
39+
];
40+
41+
requestPatterns.forEach((pattern) => {
42+
this.client.subscribeToResponseOf(pattern);
43+
});
44+
}
3045

3146
// sync send kafka message
3247
@Post('mathSumSyncKafkaMessage')
3348
@HttpCode(200)
34-
@MessageRequest('math.sum.sync.kafka.message', 'math.sum.sync.kafka.message.reply')
3549
async mathSumSyncKafkaMessage(
3650
@Body() data: number[],
3751
): Promise<Observable<any>> {
@@ -47,7 +61,6 @@ export class KafkaController {
4761
// sync send kafka(ish) message without key and only the value
4862
@Post('mathSumSyncWithoutKey')
4963
@HttpCode(200)
50-
@MessageRequest('math.sum.sync.without.key', 'math.sum.sync.without.key.reply')
5164
async mathSumSyncWithoutKey(
5265
@Body() data: number[],
5366
): Promise<Observable<any>> {
@@ -62,7 +75,6 @@ export class KafkaController {
6275
// sync send message without key or value
6376
@Post('mathSumSyncPlainObject')
6477
@HttpCode(200)
65-
@MessageRequest('math.sum.sync.plain.object', 'math.sum.sync.plain.object.reply')
6678
async mathSumSyncPlainObject(
6779
@Body() data: number[],
6880
): Promise<Observable<any>> {
@@ -75,7 +87,6 @@ export class KafkaController {
7587
// sync send message without key or value
7688
@Post('mathSumSyncArray')
7789
@HttpCode(200)
78-
@MessageRequest('math.sum.sync.array', 'math.sum.sync.array.reply')
7990
async mathSumSyncArray(
8091
@Body() data: number[],
8192
): Promise<Observable<any>> {
@@ -85,7 +96,6 @@ export class KafkaController {
8596

8697
@Post('mathSumSyncString')
8798
@HttpCode(200)
88-
@MessageRequest('math.sum.sync.string', 'math.sum.sync.string.reply')
8999
async mathSumSyncString(
90100
@Body() data: number[],
91101
): Promise<Observable<any>> {
@@ -96,7 +106,6 @@ export class KafkaController {
96106

97107
@Post('mathSumSyncNumber')
98108
@HttpCode(200)
99-
@MessageRequest('math.sum.sync.number', 'math.sum.sync.number.reply')
100109
async mathSumSyncNumber(
101110
@Body() data: number[],
102111
): Promise<Observable<any>> {
@@ -113,7 +122,6 @@ export class KafkaController {
113122
// Complex data to send.
114123
@Post('/user')
115124
@HttpCode(200)
116-
@MessageRequest('user.create', 'user.create.reply')
117125
async createUser(@Body() user: UserDto): Promise<Observable<any>> {
118126
const result = await this.client.send('user.create', {
119127
key: '1',
@@ -127,7 +135,6 @@ export class KafkaController {
127135
// Complex data to send.
128136
@Post('/business')
129137
@HttpCode(200)
130-
@MessageRequest('business.create', 'business.create.reply')
131138
async createBusiness(@Body() business: BusinessDto) {
132139
const result = await this.client.send('business.create', {
133140
key: '1',

integration/microservices/src/kafka/kafka.messages.controller.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
import * as util from 'util';
21
import { Controller } from '@nestjs/common';
3-
import { Client, ClientProxy, EventPattern, MessagePattern, MessageRequest, Transport } from '@nestjs/microservices';
2+
import { EventPattern, MessagePattern } from '@nestjs/microservices';
43
import { Logger } from '@nestjs/common/services/logger.service';
54
import { KafkaController } from './kafka.controller';
65
import { BusinessDto } from './dtos/business.dto';
76
import { UserEntity } from './entities/user.entity';
87
import { BusinessEntity } from './entities/business.entity';
98
import { UserDto } from './dtos/user.dto';
10-
import { KafkaClient } from 'kafka-node';
11-
import { map } from 'bluebird';
129

1310
@Controller()
1411
export class KafkaMessagesController {
1512
protected readonly logger = new Logger(KafkaMessagesController.name);
1613
static IS_NOTIFIED = false;
17-
static MATH_SUM = 0;
18-
19-
@Client({
20-
transport: Transport.KAFKA,
21-
options: {
22-
client: {
23-
brokers: ['localhost:9092'],
24-
},
25-
},
26-
})
27-
private readonly client: ClientProxy;
2814

2915
@MessagePattern('math.sum.sync.kafka.message')
3016
mathSumSyncKafkaMessage(data: any){

packages/microservices/client/client-kafka.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ export class ClientKafka extends ClientProxy {
3939
private readonly groupId: string;
4040

4141
protected consumerAssignments: {[key: string]: number[]} = {};
42-
43-
protected static readonly REPLY_PATTERN_AFFIX: string = '.reply';
42+
protected responsePatterns: string[] = [];
4443

4544
constructor(protected readonly options: KafkaOptions['options']) {
4645
super();
@@ -59,6 +58,15 @@ export class ClientKafka extends ClientProxy {
5958
kafkaPackage = loadPackage('kafkajs', ClientKafka.name, () => require('kafkajs'));
6059
}
6160

61+
public subscribeToResponseOf(pattern: any): void {
62+
const request = this.normalizePattern(pattern);
63+
this.responsePatterns.push(this.getResponsePatternName(request));
64+
}
65+
66+
protected getResponsePatternName(pattern: string): string {
67+
return `${pattern}.reply`;
68+
}
69+
6270
public close(): void {
6371
this.producer && this.producer.disconnect();
6472
this.consumer && this.consumer.disconnect();
@@ -100,15 +108,10 @@ export class ClientKafka extends ClientProxy {
100108
}
101109

102110
public async bindTopics(): Promise<void> {
103-
const requestPatterns = [...this.requestMap.keys()];
104-
105-
await Promise.all(requestPatterns.map(async requestPattern => {
106-
// get the reply pattern
107-
const replyPattern = this.getReplyPattern(requestPattern, ClientKafka.REPLY_PATTERN_AFFIX);
108-
111+
await Promise.all(this.responsePatterns.map(async responsePattern => {
109112
// subscribe to the pattern of the topic
110113
await this.consumer.subscribe({
111-
topic: replyPattern
114+
topic: responsePattern
112115
});
113116
}));
114117

@@ -198,7 +201,7 @@ export class ClientKafka extends ClientProxy {
198201
}, this.options.send || {}));
199202
}
200203

201-
protected getReplyPartition(topic: string): string {
204+
protected getReplyTopicPartition(topic: string): string {
202205
// get topic assignment
203206
const topicAssignments = this.consumerAssignments[topic];
204207

@@ -226,8 +229,8 @@ export class ClientKafka extends ClientProxy {
226229

227230
// get the response meta
228231
const pattern = this.normalizePattern(partialPacket.pattern);
229-
const replyTopic = this.getReplyPattern(pattern, ClientKafka.REPLY_PATTERN_AFFIX);
230-
const replyPartition = this.getReplyPartition(replyTopic);
232+
const replyTopic = this.getResponsePatternName(pattern);
233+
const replyPartition = this.getReplyTopicPartition(replyTopic);
231234

232235
// set the route
233236
this.routingMap.set(packet.id, callback);

packages/microservices/client/client-proxy.ts

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,6 @@ export abstract class ClientProxy {
2525
public abstract close(): any;
2626

2727
protected routingMap = new Map<string, Function>();
28-
protected requestMap = new Map<string, string>();
29-
30-
public addMessageRequest(
31-
requestPattern: any,
32-
replyPattern?: any
33-
) {
34-
const request = this.normalizePattern(requestPattern);
35-
let reply = null;
36-
37-
if (!isNil(replyPattern)) {
38-
reply = this.normalizePattern(replyPattern);
39-
}
40-
41-
this.requestMap.set(request, reply);
42-
}
43-
44-
protected getReplyPattern(requestPattern: string, affix: string): string {
45-
const replyPattern = this.requestMap.get(requestPattern);
46-
47-
if (isNil(replyPattern)) {
48-
return `${requestPattern}${affix}`;
49-
}
50-
51-
return replyPattern;
52-
}
5328

5429
public send<TResult = any, TInput = any>(
5530
pattern: any,

packages/microservices/decorators/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ export * from './client.decorator';
22
export * from './event-pattern.decorator';
33
export * from './grpc-service.decorator';
44
export * from './message-pattern.decorator';
5-
export * from './message-request.decorator';

packages/microservices/decorators/message-request.decorator.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/microservices/listener-metadata-explorer.ts

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -84,41 +84,4 @@ export class ListenerMetadataExplorer {
8484
yield { property, metadata };
8585
}
8686
}
87-
88-
public exploreMessageRequests(
89-
instance: Controller,
90-
): MessageRequestProperties[] {
91-
const instancePrototype = Object.getPrototypeOf(instance);
92-
return this.metadataScanner.scanFromPrototype<
93-
Controller,
94-
MessageRequestProperties
95-
>(instance, instancePrototype, method =>
96-
this.exploreMessageRequestMethodMetadata(instancePrototype, method),
97-
);
98-
}
99-
100-
public exploreMessageRequestMethodMetadata(
101-
instancePrototype: any,
102-
methodKey: string,
103-
): MessageRequestProperties {
104-
const targetCallback = instancePrototype[methodKey];
105-
106-
const requestPattern = Reflect.getMetadata(
107-
REQUEST_PATTERN_METADATA,
108-
targetCallback,
109-
);
110-
const replyPattern = Reflect.getMetadata(
111-
REPLY_PATTERN_METADATA,
112-
targetCallback,
113-
);
114-
115-
if (isUndefined(requestPattern) || isUndefined(replyPattern)) {
116-
return;
117-
}
118-
119-
return {
120-
requestPattern,
121-
replyPattern,
122-
};
123-
}
12487
}

packages/microservices/listeners-controller.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,6 @@ export class ListenersController {
7676
metadata,
7777
} of this.metadataExplorer.scanForClientHooks(instance)) {
7878
const client = this.clientFactory.create(metadata);
79-
80-
const messageRequests = this.metadataExplorer.exploreMessageRequests(
81-
instance,
82-
);
83-
84-
messageRequests.forEach(messageRequest => {
85-
client.addMessageRequest(
86-
messageRequest.requestPattern,
87-
messageRequest.replyPattern,
88-
);
89-
});
90-
9179
this.clientsContainer.addClient(client);
9280

9381
this.assignClientToInstance(instance, property, client);

0 commit comments

Comments
 (0)