-
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.
Merge pull request #251 from thanhdanh27600/dev
support multiple queue platform
- Loading branch information
Showing
6 changed files
with
106 additions
and
79 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
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,69 @@ | ||
const { postProcessForward } = require('../postProcessForward'); | ||
const { ServiceBusClient } = require('@azure/service-bus'); | ||
const { connectionString, queueName, logger } = require('../utils'); | ||
|
||
/** | ||
* An array of objects representing message types. | ||
* @typedef {Object} MessageType | ||
* @property {string} subject - The subject of the message. | ||
* @property {*} body - The body of the message, which can be of any type. | ||
*/ | ||
|
||
/** | ||
* Process a single message. | ||
* | ||
* @param {MessageType} message - The message to be processed. | ||
* @returns {Promise<void>} A Promise that resolves when the processing is complete. | ||
* | ||
* @throws {Error} Throws an error if the message processing fails. | ||
*/ | ||
async function myMessageHandler(message) { | ||
try { | ||
// console.log(`Processing message ${message.subject} with content: ${JSON.stringify(message.body)}`); | ||
switch (message.subject) { | ||
case 'forward': | ||
await postProcessForward(message.body); | ||
break; | ||
default: | ||
break; | ||
} | ||
} catch (error) { | ||
logger.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
let sbClient; | ||
let receiver; | ||
|
||
async function main() { | ||
console.log('Starting Queue Receiver'); | ||
// create a Service Bus client using the connection string to the Service Bus namespace | ||
sbClient = new ServiceBusClient(connectionString); | ||
|
||
// createReceiver() can also be used to create a receiver for a subscription. | ||
receiver = sbClient.createReceiver(queueName); | ||
|
||
// function to handle any errors | ||
const myErrorHandler = async (error) => { | ||
console.log('Error Queue Handler', error); | ||
}; | ||
|
||
// subscribe and specify the message and error handlers | ||
receiver.subscribe({ | ||
processMessage: myMessageHandler, | ||
processError: myErrorHandler, | ||
}); | ||
} | ||
|
||
const queueReceiver = () => { | ||
main().catch((err) => { | ||
console.log('Error Queue Receiver: ', err); | ||
}); | ||
// .finally(async () => { | ||
// await receiver.close(); | ||
// await sbClient.close(); | ||
// }); | ||
}; | ||
|
||
module.exports = { queueReceiver }; |
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,69 +1,17 @@ | ||
const { postProcessForward } = require('./postProcessForward'); | ||
const { ServiceBusClient } = require('@azure/service-bus'); | ||
const { connectionString, queueName, logger } = require('./utils'); | ||
|
||
/** | ||
* An array of objects representing message types. | ||
* @typedef {Object} MessageType | ||
* @property {string} subject - The subject of the message. | ||
* @property {*} body - The body of the message, which can be of any type. | ||
*/ | ||
|
||
/** | ||
* Process a single message. | ||
* | ||
* @param {MessageType} message - The message to be processed. | ||
* @returns {Promise<void>} A Promise that resolves when the processing is complete. | ||
* | ||
* @throws {Error} Throws an error if the message processing fails. | ||
*/ | ||
async function myMessageHandler(message) { | ||
try { | ||
// console.log(`Processing message ${message.subject} with content: ${JSON.stringify(message.body)}`); | ||
switch (message.subject) { | ||
case 'forward': | ||
await postProcessForward(message.body); | ||
break; | ||
default: | ||
break; | ||
const { sendMessageToAzureQueue } = require("./azure/sendMessageToAzureQueue"); | ||
const { sendMessageToRabbitQueue } = require("./rabbit"); | ||
const { queuePlatform } = require("./utils"); | ||
|
||
const sendMessageToQueue = async (message) => { | ||
switch (queuePlatform) { | ||
case 'AZURE': | ||
await sendMessageToAzureQueue([message]) | ||
break; | ||
case 'RABBIT': | ||
default: | ||
await sendMessageToRabbitQueue(message); | ||
break; | ||
} | ||
} catch (error) { | ||
logger.error(error); | ||
throw error; | ||
} | ||
} | ||
|
||
let sbClient; | ||
let receiver; | ||
|
||
async function main() { | ||
console.log('Starting Queue Receiver'); | ||
// create a Service Bus client using the connection string to the Service Bus namespace | ||
sbClient = new ServiceBusClient(connectionString); | ||
|
||
// createReceiver() can also be used to create a receiver for a subscription. | ||
receiver = sbClient.createReceiver(queueName); | ||
|
||
// function to handle any errors | ||
const myErrorHandler = async (error) => { | ||
console.log('Error Queue Handler', error); | ||
}; | ||
|
||
// subscribe and specify the message and error handlers | ||
receiver.subscribe({ | ||
processMessage: myMessageHandler, | ||
processError: myErrorHandler, | ||
}); | ||
} | ||
|
||
const queueReceiver = () => { | ||
main().catch((err) => { | ||
console.log('Error Queue Receiver: ', err); | ||
}); | ||
// .finally(async () => { | ||
// await receiver.close(); | ||
// await sbClient.close(); | ||
// }); | ||
}; | ||
|
||
module.exports = { queueReceiver }; | ||
module.exports = { sendMessageToQueue }; |
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