Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(rabbitmq): add exchange-to-exchange bindings config #681

Merged
merged 2 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions integration/rabbitmq/e2e/configuration.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,5 +582,85 @@ describe('Module Configuration', () => {
await app.close();
});
});

it('should create exchange bindings', async () => {
const originalConnect = amqplib.connect;
let bindExchangeSpy;

const connectSpy = jest
.spyOn(amqplib, 'connect')
.mockImplementation((...args) => {
const result = originalConnect(...args);
result.then((conn) => {
const originalCreateConfirmChannel = conn.createConfirmChannel;
jest
.spyOn(conn, 'createConfirmChannel')
.mockImplementation(function (...args) {
const result = originalCreateConfirmChannel.apply(this, args);
result.then((channel) => {
bindExchangeSpy = jest.spyOn(channel, 'bindExchange');
});
return result;
});
});
return result;
});

const otherExchangeName = 'otherExchange';

app = await Test.createTestingModule({
imports: [
RabbitMQModule.forRootAsync(RabbitMQModule, {
useFactory: async () => {
return {
exchanges: [
{
name: nonExistingExchange,
type: 'topic',
createExchangeIfNotExists: true,
},
{
name: otherExchangeName,
type: 'topic',
createExchangeIfNotExists: true,
},
],
exchangeBindings: [
{
destination: otherExchangeName,
source: nonExistingExchange,
pattern: '*',
},
],
uri,
connectionInitOptions: {
wait: true,
reject: true,
timeout: 3000,
},
};
},
}),
],
}).compile();

const amqpConnection = app.get<AmqpConnection>(AmqpConnection);
expect(app).toBeDefined();

expect(connectSpy).toHaveBeenCalledTimes(1);
expect(connectSpy).toHaveBeenCalledWith(amqplibUri, undefined);
expect(bindExchangeSpy).toHaveBeenCalledWith(
otherExchangeName,
nonExistingExchange,
'*',
undefined,
);

await app.init();
expect(
await amqpConnection.channel.checkExchange(nonExistingExchange),
).toBeDefined();
await app.close();
});
});
});
12 changes: 12 additions & 0 deletions packages/rabbitmq/src/amqp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const defaultConfig = {
),
defaultSubscribeErrorBehavior: MessageHandlerErrorBehavior.REQUEUE,
exchanges: [],
exchangeBindings: [],
queues: [],
defaultRpcTimeout: 10000,
connectionInitOptions: {
Expand Down Expand Up @@ -291,6 +292,17 @@ export class AmqpConnection {
})
);

await Promise.all(
this.config.exchangeBindings.map((exchangeBinding) =>
channel.bindExchange(
exchangeBinding.destination,
exchangeBinding.source,
exchangeBinding.pattern,
exchangeBinding.args
)
)
);

await this.setupQueuesWithBindings(channel, this.config.queues);

if (this.config.enableDirectReplyTo) {
Expand Down
8 changes: 8 additions & 0 deletions packages/rabbitmq/src/rabbitmq.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ export interface RabbitMQQueueConfig {
bindQueueArguments?: any;
}

export interface RabbitMQExchangeBindingConfig {
destination: string;
source: string;
pattern: string;
args?: any;
}

export type ConsumeOptions = Options.Consume;

export interface MessageOptions {
Expand Down Expand Up @@ -110,6 +117,7 @@ export interface RabbitMQConfig {
*/
prefetchCount?: number;
exchanges?: RabbitMQExchangeConfig[];
exchangeBindings?: RabbitMQExchangeBindingConfig[];
queues?: RabbitMQQueueConfig[];
defaultRpcTimeout?: number;
defaultExchangeType?: string;
Expand Down
Loading