welsonjs/lib/msmq.js

61 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-09-23 04:51:36 +00:00
////////////////////////////////////////////////////////////////////////
// MSMQ(Microsoft Message Queuing) API
2021-09-23 04:52:08 +00:00
//
// references:
// - https://gist.github.com/vladaman/6375841
2021-09-23 04:59:11 +00:00
// - https://docs.microsoft.com/en-us/previous-versions/windows/desktop/msmq/ms703952(v=vs.85)
// - https://docs.microsoft.com/en-us/previous-versions/windows/desktop/msmq/ms705286(v=vs.85)
2021-09-23 04:52:08 +00:00
//
2021-09-23 04:51:36 +00:00
////////////////////////////////////////////////////////////////////////
var MSMQObject = function(queueName) {
2021-09-23 04:53:09 +00:00
var MQ_SEND_ACCESS = 2;
var MQ_DENY_NONE = 0;
var MQ_RECEIVE_ACCESS = 1;
2021-09-23 04:51:36 +00:00
2021-09-23 04:53:09 +00:00
var MQ_NO_TRANSACTION = 0;
var MQ_MTS_TRANSACTION = 1;
var MQ_SINGLE_MESSAGE = 3;
2021-09-23 04:51:36 +00:00
2021-09-23 04:53:09 +00:00
this.machineName = "localhost";
this.queueName = queueName;
2021-09-23 04:51:36 +00:00
2021-09-23 04:53:09 +00:00
this.queueInfo = null;
this.queue = null;
2021-09-23 04:51:36 +00:00
2021-09-23 04:53:09 +00:00
this.create = function() {
2021-09-23 04:51:36 +00:00
try {
this.queueInfo = CreateObject("MSMQ.MSMQQueueInfo");
2021-09-23 04:53:09 +00:00
this.queueInfo.PathName = ".\\private$\\" + this.queueName;
2021-09-23 04:51:36 +00:00
} catch (e) {
console.error("MSMQObject.create() ->", e.message);
}
2021-09-23 04:58:16 +00:00
return this;
2021-09-23 04:53:09 +00:00
}
this.open = function() {
this.queue = this.queueInfo.Open(MQ_SEND_ACCESS, MQ_DENY_NONE);
2021-09-23 04:58:16 +00:00
return this;
2021-09-23 04:53:09 +00:00
}
// For example: .send("Sample Body", "LabelXX");
this.send = function(body, label) {
var mqmsg = CreateObject("MSMQ.MSMQMessage");
mqmsg.Body = body
mqmsg.Label = label
mqmsg.Send(this.queue);
2021-09-23 04:58:16 +00:00
return mqmsg;
2021-09-23 04:53:09 +00:00
};
2021-09-23 04:58:16 +00:00
2021-09-23 04:53:09 +00:00
this.recv = function() {
return this.queue.Receive(MQ_NO_TRANSACTION, false, true, 20000, false);
};
this.create();
2021-09-23 04:51:36 +00:00
};
2021-09-23 04:58:16 +00:00
exports.MSMQObject = MSMQObject;
exports.open = function(queueName) {
return (new MSMQObject(queueName)).open();
2021-09-23 04:59:11 +00:00
};