-
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.
- Loading branch information
1 parent
f1bace2
commit 27a0c46
Showing
4 changed files
with
182 additions
and
6 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import * as AMQP from 'amqplib' | ||
|
||
import { sleep, stringifiers } from '@jvalue/node-dry-basics' | ||
|
||
export class AmqpPublisher { | ||
private channel?: AMQP.Channel | ||
|
||
public async init (amqpUrl: string, exchange: string, retries: number, msBackoff: number): Promise<void> { | ||
for (let i = 0; i <= retries; i++) { | ||
try { | ||
const connection = await this.connect(amqpUrl) | ||
this.channel = await this.initChannel(connection, exchange) | ||
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}`) | ||
} | ||
|
||
private async 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 | ||
} | ||
} | ||
|
||
private async initChannel (connection: AMQP.Connection, exchange: string): Promise<AMQP.Channel> { | ||
try { | ||
const channel = await connection.createChannel() | ||
await channel.assertExchange(exchange, 'topic') | ||
console.info(`Exchange ${exchange} successfully initialized.`) | ||
return channel | ||
} catch (error) { | ||
console.error(`Error creating exchange ${exchange}: ${error}`) | ||
throw error | ||
} | ||
} | ||
|
||
public publish (exchange: string, topic: string, content: object): boolean { | ||
if (this.channel === undefined) { | ||
console.error('Publish not possible, AMQP client not initialized.') | ||
return false | ||
} else { | ||
try { | ||
const success = this.channel.publish(exchange, topic, Buffer.from(JSON.stringify(content))) | ||
console.debug(`[EventProduce] ${topic}: ${stringifiers.stringify(content)}`) | ||
return success | ||
} catch (error) { | ||
console.error(`Error publishing to exchange ${exchange} under key ${topic}: ${error}`) | ||
return false | ||
} | ||
} | ||
} | ||
} |
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 @@ | ||
export { AmqpPublisher } from './amqpPublisher' |