mqueue Module

Elena-Ramona Modroiu

Edited by

Elena-Ramona Modroiu

Alex Balashov

Evariste Systems

Ovidiu Sas

VoIP Embedded, Inc.

Julien Chavanton

Table of Contents

1. Admin Guide
1. Overview
2. Dependencies
2.1. Kamailio Modules
2.2. External Libraries or Applications
3. Parameters
3.1. db_url (str)
3.2. mqueue (string)
3.3. mqueue_name (string)
3.4. mqueue_size (int)
3.5. mqueue_addmode (int)
4. Functions
4.1. mq_add(queue, key, value)
4.2. mq_fetch(queue)
4.3. mq_pv_free(queue)
4.4. mq_size(queue)
5. Exported Variables
6. RPC Commands
6.1. mqueue.get_size
6.2. mqueue.fetch
6.3. mqueue.get_sizes

List of Examples

1.1. Set db_url parameter
1.2. Set mqueue parameter
1.3. Set mqueue_name parameter
1.4. Set mqueue_size parameter
1.5. Set mqueue_addmode parameter
1.6. mq_add usage
1.7. mq_fetch usage
1.8. mq_pv_free usage
1.9. mq_size usage
1.10. mqueue.get_size usage
1.11. mqueue.fetch usage
1.12. mqueue.get_sizes usage

Chapter 1. Admin Guide

1. Overview

The mqueue module offers a generic message queue system in shared memory for inter-process communication using the config file. One example of usage is to send time consuming operations to one or several timer processes that consumes items in the queue, without affecting SIP message handling in the socket-listening process.

There can be many defined queues. Access to queued values is done via pseudo variables.

2. Dependencies

2.1. Kamailio Modules

The following modules must be loaded before this module:

  • None.

2.2. External Libraries or Applications

The following libraries or applications must be installed before running Kamailio with this module loaded:

  • None.

3. Parameters

3.1. db_url (str)

The URL to connect to database for loading values in mqueue table at start up and/or saving values at shutdown.

Default value is NULL (do not connect).

Example 1.1. Set db_url parameter

modparam("mqueue", "db_url", "mysql://kamailio:kamailiorw@localhost/kamailio")

# Example of table in sqlite, you have the set the fields to support the length according to the data that will be present in the mqueue
CREATE TABLE mqueue_name (
key character varying(64) DEFAULT "" NOT NULL,
val character varying(4096) DEFAULT "" NOT NULL

3.2. mqueue (string)

Definition of a memory queue

Default value is none.

Value must be a list of parameters: attr=value;...

  • Mandatory attributes:

    • name: name of the queue.

  • Optional attributes:

    • size: size of the queue. Specifies the maximum number of items in queue. If exceeded the oldest one is removed. If not set the queue will be limitless.

    • dbmode: If set to 1, the content of the queue is read from database at startup and is written to database table when the SIP server is stopped (i.e., ensure persistency over restarts). If set to 2, it is read at startup but not written at shutdown. If set to 3, it is written at shutdown but not read at startup. Default value is 0 (no db table interaction).

    • addmode: how to add new (key,value) pairs.

      • 0: Will push all new (key,value) pairs at the end of the queue. (default)

      • 1: Will keep oldest (key,value) pair in the queue, based on the key.

      • 2: Will keep newest (key,value) pair in the queue, based on the key.

The parameter can be set many times, each holding the definition of one queue.

Example 1.2. Set mqueue parameter

modparam("mqueue", "mqueue", "name=myq;size=20;")
modparam("mqueue", "mqueue", "name=myq;size=10000;addmode=2")
modparam("mqueue", "mqueue", "name=qaz")
modparam("mqueue", "mqueue", "name=qaz;addmode=1")

3.3. mqueue_name (string)

Definition of a memory queue, just by name.

Default value is none.

Value must be a string.

The parameter can be set many times, each holding the definition of one queue. The max size of each queue defined this way will be equal to mqueue_size(if mqueue_size configured), or limitless (if mqueue_size not configured).

Example 1.3. Set mqueue_name parameter

modparam("mqueue", "mqueue_name", "my_own_queue")

3.4. mqueue_size (int)

Definition of the size of all memory queues defined via "mqueue_name" parameter.

Default value is 0.

Value must be an int.

The parameter should be set before defining any "mqueue_name". If not set, the queues defined via "mqueue_name" will be limitless.

Example 1.4. Set mqueue_size parameter

modparam("mqueue", "mqueue_size", 1024)

3.5. mqueue_addmode (int)

Sets the mode to be used when adding a new (key,value) pair in the mqueue.

  • 0 Add all new (key,value) at the end of mqueue

  • 1 Unique key, keep oldest (key,value)

  • 2 Unique key, keep newest(key,value)

Default value is 0.

Value must be an int.

The parameter should be set before defining any "mqueue_name". If not set, the queues defined via "mqueue_name" will have default addmode 0.

Example 1.5. Set mqueue_addmode parameter

modparam("mqueue", "mqueue_addmode", 2)

4. Functions

4.1.  mq_add(queue, key, value)

Add a new item (key, value) in the queue. If max size of queue is exceeded, the oldest one is removed.

Example 1.6. mq_add usage

mq_add("myq", "$rU", "call from $fU");

4.2.  mq_fetch(queue)

Take oldest item from queue and fill $mqk(queue) and $mqv(queue) pseudo variables.

Return: true on success (1); false on failure (-1) or no item fetched (-2).

Example 1.7. mq_fetch usage

   xlog("$mqk(myq) - $mqv(myq)\n");

4.3.  mq_pv_free(queue)

Free the item fetched in pseudo-variables. It is optional, a new fetch frees the previous values.

Example 1.8. mq_pv_free usage


4.4.  mq_size(queue)

Returns the current number of elements in the mqueue.

If the mqueue is empty, the function returns -1. If the mqueue is not found, the function returns -2.

Example 1.9. mq_size usage

$var(q_size) = mq_size("queue");
xlog("L_INFO", "Size of queue is: $var(q_size)\n");

5. Exported Variables

  • $mqk(mqueue) - the most recent item key fetched from the specified mqueue
  • $mqv(mqueue) - the most recent item value fetched from the specified mqueue
  • $mq_size(mqueue) - the size of the specified mqueue

Exported pseudo-variables are documented at

6. RPC Commands

6.1. mqueue.get_size

Get the size of a memory queue.


  • name - the name of memory queue

Example 1.10. mqueue.get_size usage

kamcmd mqueue.get_size xyz

6.2. mqueue.fetch

Fetch a key-value pair from a memory queue.


  • name - the name of memory queue

Example 1.11. mqueue.fetch usage

kamcmd mqueue.fetch xyz

6.3. mqueue.get_sizes

Get the size for all memory queues.

Parameters: none

Example 1.12. mqueue.get_sizes usage

kamcmd mqueue.get_sizes