Skip to content

Commit 3de3f9c

Browse files
committed
feat(amqp): added more generic way to define connections
1 parent 62549ff commit 3de3f9c

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

packages/amqp/src/amqp.module.ts

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,13 @@ export class AmqpModule {
1414
providers: [
1515
{
1616
provide: AmqpConnection,
17-
useFactory: () => amqpClient.connect(config),
17+
useFactory: () => AmqpService.createConnection(config),
1818
},
1919
{
2020
provide: AmqpChannel,
2121
deps: [AmqpConnection],
22-
useFactory: async (connection: Connection) => {
23-
const channel = await connection.createChannel();
24-
if (config.prefetchCount) {
25-
await channel.prefetch(config.prefetchCount);
26-
}
27-
return channel;
28-
},
22+
useFactory: async (connection: Connection) =>
23+
AmqpService.createChannel(connection, config.prefetchCount),
2924
},
3025
],
3126
};

packages/amqp/src/amqp.service.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Inject, Injectable } from '@rhtml/di';
22
import { Channel, ConsumeMessage, Options } from 'amqplib';
3+
import amqpClient, { Connection } from 'amqplib';
34

4-
import { AmqpChannel } from './amqp.constants';
5+
import { AmqpChannel, ModuleConfig } from './amqp.constants';
56

67
@Injectable()
78
export class AmqpService {
@@ -32,4 +33,19 @@ export class AmqpService {
3233
options?.consumeOptions
3334
);
3435
}
36+
37+
public static createConnection(config: ModuleConfig) {
38+
return amqpClient.connect(config);
39+
}
40+
41+
public static async createChannel(
42+
connection: Connection,
43+
prefetchCount?: number
44+
) {
45+
const channel = await connection.createChannel();
46+
if (prefetchCount) {
47+
await channel.prefetch(prefetchCount);
48+
}
49+
return channel;
50+
}
3551
}

packages/amqp/src/decorators/subscribe.decorator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const Subscribe =
2525
await target[memberName].call(this, msg, channel);
2626
} catch (e) {
2727
console.error(
28-
`[Subscription]: queue "${queue}" fail internally inside ${target} ${memberName}`
28+
`[AMQP][Subscribe]: queue "${queue}" failed to handle internally inside subscription "${memberName}" method`
2929
);
3030
}
3131
},

0 commit comments

Comments
 (0)