-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
102 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import * as AMQP from 'amqplib' | ||
|
||
export async function connect (amqpUrl: string): Promise<AMQP.Connection> { | ||
try { | ||
const connection = await AMQP.connect(amqpUrl) | ||
console.info(`Connection to amqp host at ${amqpUrl} successful`) | ||
return connection | ||
} catch (error) { | ||
console.error(`Error connecting to amqp host at ${amqpUrl}: ${error}`) | ||
throw error | ||
} | ||
} | ||
|
||
export async function initChannel ( | ||
connection: AMQP.Connection, | ||
exchange: {name: string, type: string}, | ||
exchangeOptions: AMQP.Options.AssertExchange | ||
): Promise<AMQP.Channel> { | ||
try { | ||
const channel = await connection.createChannel() | ||
await channel.assertExchange(exchange.name, exchange.type, exchangeOptions) | ||
console.info(`Exchange ${exchange.name} successfully initialized.`) | ||
return channel | ||
} catch (error) { | ||
console.error(`Error creating exchange ${exchange.name}: ${error}`) | ||
throw error | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import * as AMQP from 'amqplib' | ||
import { sleep, stringifiers } from '@jvalue/node-dry-basics' | ||
|
||
import * as AmqpConnector from './amqpConnector' | ||
|
||
const LOG_MAX_LENGTH = 30 | ||
|
||
export class AmqpConsumer { | ||
private connection?: AMQP.Connection | ||
|
||
public async init (amqpUrl: string, retries: number, msBackoff: number): Promise<void> { | ||
for (let i = 1; i <= retries; i++) { | ||
try { | ||
this.connection = await AmqpConnector.connect(amqpUrl) | ||
return | ||
} catch (error) { | ||
console.info(`Error initializing the AMQP Client (${i}/${retries}): | ||
${error}. Retrying in ${msBackoff}...`) | ||
} | ||
await sleep(msBackoff) | ||
} | ||
throw new Error(`Could not connect to AMQP broker at ${amqpUrl}`) | ||
} | ||
|
||
public async registerConsumer ( | ||
exchange: { name: string, type: string }, | ||
exchangeOptions: AMQP.Options.AssertExchange, | ||
queue: { name: string, routingKey: string }, | ||
queueOptions: AMQP.Options.AssertQueue, | ||
consumeEvent: (msg: AMQP.ConsumeMessage | null) => Promise<void> | ||
): Promise<void> { | ||
if (this.connection === undefined) { | ||
throw new Error('Consume not possible, AMQP client not initialized.') | ||
} | ||
|
||
try { | ||
const channel = | ||
await AmqpConnector.initChannel(this.connection, { name: exchange.name, type: exchange.type }, exchangeOptions) | ||
const q = await channel.assertQueue(queue.name, queueOptions) | ||
await channel.bindQueue(q.queue, exchange.name, queue.routingKey) | ||
await channel.consume(q.queue, msg => { | ||
console.debug("[AMQP][Consume] %s:'%s'", | ||
msg?.fields.routingKey, stringifiers.stringify(msg?.content.toString(), LOG_MAX_LENGTH)) | ||
consumeEvent(msg) | ||
.catch(error => console.error(`Failed to handle ${msg?.fields.routingKey ?? 'null'} event`, error)) | ||
}) | ||
} catch (error) { | ||
throw new Error(`Error subscribing to exchange ${exchange.name} under key ${queue.routingKey}: ${error}`) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { AmqpPublisher } from './amqpPublisher' | ||
export { AmqpConsumer } from './amqpConsumer' |