Listener of a Queue
Steps
- Create a project message_queue_listener_project
- Then create a JavaScript service named my_listener_handler.js
- Replace the service code with the following content:
Handler
exports.onMessage = function(message) {
console.log("Hello from My Listener! Message: " + message);
};
exports.onError = function(error) {
console.error("Error from My Listener! Error: " + error);
};
- Then create a Message Listener named my_listener.listener
- Replace the file content with the following JSON code:
{
"name":"message_queue_listener_project/my_queue",
"type":"Q",
"handler":"message_queue_listener_project/my_listener_handler.js",
"description":"My Listener"
}
- Then create another back-end service which will play the role of a trigger my_trigger.js
- Replace the trigger content with the following code:
var producer = require('messaging/v3/producer');
var message = "*** I am a message created at: " + new Date() + " ***";
producer.queue("message_queue_listener_project/my_queue").send(message);
console.log("Hello from My Trigger! Message: " + message);
- Publish the project
- Select the my_trigger.js file in the Workspace view to be able to trigger the invocation of this service via the Preview view
-
In the Console view you should see the following lines:
[2018-05-14T11:57:13.197Z] [INFO] Hello from My Listener! Message: I am a message created at: Mon May 14 2018 14:57:13 GMT+0300 (EEST)
[2018-05-14T11:57:13.174Z] [INFO] Hello from My Trigger! Message: I am a message created at: Mon May 14 2018 14:57:13 GMT+0300 (EEST)
Note: the log messages in the Console view are in a reverse order - the newest are on top
For more information, see the API documentation.