forked from aojiaotage/mi_backend
-
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.
Add rabbitmq: consumer/producer implementation.
- Loading branch information
1 parent
24dfaea
commit 13b233e
Showing
7 changed files
with
135 additions
and
1 deletion.
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
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,47 @@ | ||
'use strict'; | ||
|
||
const rmq = require('amqplib'); | ||
|
||
class BaseConsumer { | ||
|
||
static get ['QUEUE_NAME_PREF']() { | ||
return 'mi_'; | ||
} | ||
|
||
get ['queueName']() { | ||
throw new Error('Please define your own queue name'); | ||
} | ||
|
||
async initConn() { | ||
const conn = await rmq.connect(this.app.config.rabbitmq.url); | ||
this.conn = conn; | ||
} | ||
|
||
async initChannel() { | ||
if (!this.conn) { | ||
await this.initConn(); | ||
} | ||
const channel = await this.conn.createChannel(this.queueName); | ||
await channel.assertQueue(this.queueName, { | ||
durable: true, | ||
}); | ||
this.channel = channel; | ||
return channel; | ||
} | ||
|
||
onMsg(msg) { | ||
throw new Error('Please define your own message handler'); | ||
} | ||
|
||
async init(app) { | ||
if (!this.app) this.app = app; | ||
if (!this.channel) { | ||
await this.initChannel(); | ||
} | ||
this.channel.consume(this.queueName, (msg)=>{this.onMsg(msg)}); | ||
} | ||
|
||
} | ||
|
||
module.exports = BaseConsumer; | ||
|
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,18 @@ | ||
'use strict'; | ||
|
||
const BaseConsumer = require('./base_comsumer'); | ||
|
||
class UnlockInventory extends BaseConsumer { | ||
|
||
get ['queueName']() { | ||
return BaseConsumer.QUEUE_NAME_PREF + 'inventory_unlock'; | ||
} | ||
|
||
onMsg(msg) { | ||
console.log(JSON.parse(msg.content.toString('utf-8'))); | ||
this.channel.ack(msg); | ||
} | ||
|
||
} | ||
|
||
module.exports = UnlockInventory; |
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
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,56 @@ | ||
'use strict'; | ||
|
||
const { Service } = require('egg'); | ||
|
||
const rmq = require('amqplib'); | ||
|
||
const QUEUE_NAME_PREF = 'mi_'; | ||
|
||
class MsgProducer extends Service { | ||
|
||
static get ['QUEUE_NAMES']() { | ||
return { | ||
INVENTORY_UNLOCK: QUEUE_NAME_PREF + 'inventory_unlock', | ||
}; | ||
} | ||
|
||
async initConn() { | ||
const conn = await rmq.connect(this.app.config.rabbitmq.url); | ||
this.conn = conn; | ||
} | ||
|
||
async initChannel(queueName) { | ||
if (!this.conn) { | ||
await this.initConn(); | ||
} | ||
const channel = await this.conn.createChannel(queueName); | ||
await channel.assertQueue(queueName, { | ||
durable: true, | ||
}); | ||
this.channels[queueName] = channel; | ||
return channel; | ||
} | ||
|
||
async sendMsg(queueName, msg) { | ||
|
||
if (!this.channels) { | ||
this.channels = {}; | ||
} | ||
|
||
let channel = this.channels[queueName]; | ||
|
||
if (!channel) { | ||
channel = await this.initChannel(queueName); | ||
} | ||
|
||
await channel.sendToQueue(queueName, Buffer.from(msg)); | ||
} | ||
|
||
async sendInventoryUnlockMsg(inventoryInfo) { | ||
await this.sendMsg(MsgProducer.QUEUE_NAMES.INVENTORY_UNLOCK, JSON.stringify(inventoryInfo)); | ||
} | ||
|
||
} | ||
|
||
module.exports = MsgProducer; | ||
|
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