Getting Started
Import module
var Jqueue = require('jqueue');
Create jqueue
var Jqueue = require('jqueue');
var db = require('node-mysql');
var conncetionInfo = {
host : 'set host here',
user : 'set user here',
password : 'set password here',
database : 'jqueue'
};
var dataSource = new db.DB(conncetionInfo);
var jqueue = new Jqueue(dataSource);
Use
parameters: queueName, noCreate, ephemeral, callback;
- queueName: (STRING) name of the queue to be used, if the queue does not exist it will be created (mandatory);
- noCreate (BOOLEAN) if true the queue will not be created if it not exist (optional);
- ephemeral: (BOOLEAN) if true the queue will stored in memory, default: false (optional);
- callback (FUNCTION) is the callback function (mandatory).
jqueue.use('myqueue', function(error, queue) {
if(!error) {
console.log('I am a queue object', queue);
}
});
jqueue.use('myqueue', false, function(error, queue) {
if(!error) {
console.log('I am a queue object', queue);
}
});
jqueue.use('myqueue', true, function(error, queue) {
if(!error) {
console.log('I am a queue object', queue);
}
});
jqueue.use('myqueue', true, false, function(error, queue) {
if(!error) {
console.log('I am a queue object', queue);
}
});
jqueue.use('myqueue', true, true, function(error, queue) {
if(!error) {
console.log('I am a queue object', queue);
}
});
Queue
Queue Object
Getters
- getName(): will return the name of the queue.
Put
Parameters: message, delay, priority, callback.
- message: (STRING) the message data (mandatory);
- delay: (INT) delay in seconds, default: 0 (optional);
- priority: (INT) priority of the message, default: 0 (optional);
- tag: (STRING) tag of the message, default: null (optional);
- callback (FUNCTION) the callback function (mandatory).
Important: Message data needs to be less or equal 4096 bytes.
queue.put('my message data', function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
queue.put('my message data', 5, function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
queue.put('my message data', 5, 1, function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
queue.put('my message data', 5, 1, 'testTag', function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
queue.put('my message data', 0, 1, function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
queue.put('my message data', 0, 0, 'testTag', function(error, messageId) {
if(!error) {
console.log('success, message id = ' + messageId);
}
});
Reserve
Parameters: timeToRun, callback.
- timeToRun: (INT) time to process message in seconds, default: 5 (optional);
- tag: (STRING) tag of the message, default: null (optional);
- callback (FUNCTION) the callback function (mandatory).
Important: If there is no message in queue, the message object in callback will be undefined.
timeToRun is the time that the message will be reserved.
queue.reserve(function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.reserve(3, function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.reserve(3, 'testTag', function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.reserve(5, 'testTag', function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
Watch
Parameters: timeToRun, intervalTime, callback.
- timeToRun: (INT) time to process message in seconds, default: 5 (optional);
- intervalTime: (INT) period of check for new messages in milliseconds, default: 1000 (optional);
- tag: (STRING) tag of the message, default: null (optional);
- callback (FUNCTION) the callback function (mandatory).
Important: timeToRun is the time that the message will be reserved.
When a message is reserved the watching is stopped, so you need to call watch again if you want a new message
queue.watch(function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.watch(3, function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.watch(3, 500, function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.watch(3, 500, 'testTag', function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
var watcher = queue.watch(function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
watcher.cancel();
Kick
Parameters: max, delay, callback.
- max: (INT) maximun number of buried messages to change to ready status (optional);
- delay: (INT) delay in seconds, default: 0 (optional);
- callback (FUNCTION) the callback function (mandatory).
queue.kick(function(error) {
if(!error) {
console.log('success');
}
});
queue.kick(20, function(error) {
if(!error) {
console.log('success');
}
});
queue.kick(20, 3, function(error) {
if(!error) {
console.log('success');
}
});
KickMessage
Parameters: id, delay, callback.
- id: (INT) id of the buried message (mandatory);
- delay: (INT) delay in seconds, default: 0 (optional);
- callback (FUNCTION) the callback function (mandatory).
queue.kickMessage(123, function(error) {
if(!error) {
console.log('success');
}
});
queue.kickMessage(123, 5, function(error) {
if(!error) {
console.log('success');
}
});
Pick
Parameters: id, timeToRun, callback.
- id: (INT) id of the message (mandatory);
- timeToRun: (INT) time to process message in seconds, default: 5 (optional);
- callback: (FUNCTION) the callback function (mandatory).
queue.pick(123, function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
queue.pick(123, 3, function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
Message
Message Object
Getters
- getId(): will return the message id;
- getData(): will return the content of the message;
- getStatus(): will return the status of the message;
- getDelay(): will return the delay of the message, if it not have been inserted into a queue;
- getPriority(): will return the priority of the message;
- getDateTime(): will return the time when the message was ready;
- getTimeToRun(): will return the time to run;
- getQueueName(): will return the name of the queue.
- getTag(): will return the tag of the message.
Release
Parameters: delay, callback.
- delay: (INT) delay in seconds, default: 0 (optional);
- tag: (STRING) new tag to the message, default: null;
- callback (FUNCTION) the callback function (mandatory).
Obs: if delay is set to 0, the delay will not be changed
message.release(function(error) {
if(!error) {
console.log('success');
}
});
message.release(3, function(error) {
if(!error) {
console.log('success');
}
});
message.release(3, 'testTag', function(error) {
if(!error) {
console.log('success');
}
});
message.release(0, 'testTag', function(error, message) {
if(!error && message) {
console.log('I am a message object', message);
}
});
Touch
Parameters: callback.
- callback (FUNCTION) the callback function (mandatory).
message.touch(function(error) {
if(!error) {
console.log('success');
}
});
Delete
Parameters: callback.
- callback (FUNCTION) the callback function (mandatory).
message.delete(function(error) {
if(!error) {
console.log('success');
}
});
Bury
Parameters: callback.
- callback (FUNCTION) the callback function (mandatory).
message.bury(function(error) {
if(!error) {
console.log('success');
}
});
Callbacks standard
The callbacks functions need to respect the callback standard (error, data). So, when you set a callback function, make sure that there at least an error parameter. Even if no error occurs, the error parameter needs to be set, but in this case, the error value will be null.