Tutorial on Linux + Asterisk +MySQL + Java – part 2

This part of the tutorial explain how to implement Asterisk’ Realtime functionality with MySQL.

After the brief introduction about LAMJ and the Linux + Asterisk + MySQL + Java, let’s begin to do some configuration.
I’ll not explain the general rules for Asterisk setup on a Linux box, here’s some good starting point:
Asterisk Documentation
Quick Start Guide (pdf)

With the file-based configuration (i.e. all the modifications done to the *.conf files) you setup a static configuration of your PBX – it may be right for you, if you don’t need to change something frequently, mostly in the dialplan.
BUT there is also the chance that you have to do it or, even more funny, you have to write a software that does this for you following the request of an end user.
So, REALTIME to the rescue.
The Realtime function (to be used inside your extensions.conf) allows you to configure your dialplan with data coming from a “RealTime repository” (that is, for me, a database); more interesting is that the modification you are doing in your database are instantly taken by Asterisk (after all, there must be a reason for the “Realtime” term).
One thing to clarify, anyway, is that you may also obtain “realtime” configuration only using Java (we will see that in later post) but the configuration will not persist (unless, after a very heavy dinner, a lot of drinks, and a sleepless night full of nightmares, you decided to programmatically edit the .conf files!!!).

Now, since the LAMJ imply the MySQL support, I will explain only this part of the configuration.
To cut a long story short, you have to:

  1. Have the res_config_mysql.so (it is part of the “asterisk add-ons” – don’t ask me why; explaining how to get or build it is part of a lot of other tutorials);
  2. Create the database and grant privileges to a given user (in this tutorial “asterisk” will be used as database name, user name, and user password: exceptionally secure, uh?! 🙂 )
  3. Create the dialplan table (“extensions” in this tutorial):

    CREATE TABLE `extensions` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `context` varchar(20) NOT NULL DEFAULT '',
    `exten` varchar(20) NOT NULL DEFAULT '',
    `priority` varchar(10) NOT NULL,
    `app` varchar(20) NOT NULL DEFAULT '',
    `appdata` varchar(128) NOT NULL DEFAULT '',
    PRIMARY KEY (`context`,`exten`,`priority`),
    KEY `id` (`id`)
  4. Edit the “res_config_mysql.conf” file with the connection parameters to the database, beside a specific [section-name] (“asterisk-section” in this tutorial)

    dbhost =
    dbname = asterisk
    dbuser = asterisk
    dbpass = asterisk
    dbport = 3306
    dbsock = /var/run/mysqld/mysqld.sock
    dbcharset = latin1
  5. Add to the “extconfig.conf” file a line with the format
    <family> => <realtime driver>,<[section-name]>[,<table>]
    to specify that for a given configuration the given repository will be used; in our tutorial we will focus on dialplan configuration (whose family is “extensions”) with MySQL (whose realtime driver is “mysql”), like this:
    extensions => mysql,asterisk-section,extensions
    WARN: this is different from old versions of asterisk, where instead of the [section-name] you have to put the database name (that would have been “asterisk” in this tutorial); I can’t tell exactly when this has changed, but in 1.6 it was different;
  6. Add a “switch” directive in “extensions.conf” (the dialplan configuration file):

    switch => Realtime/conference@extensions

    please note that “conference” in the above line is the “context” to be loaded, and it must match the value you put in the “context” column inside the “extensions” table; you may have more than one switch, one for each “context” you want to be managed in realtime;
  7. Be sure to load the “res_config_mysql” module, and the others required for realtime to work; this can be done inside “modules.conf” file with this simple directive:


If everything is correct, when you reload or restart asterisk you should see in the console a line containing “Registered Config Engine mysql”; if not, check everything from beginning.
Using “dialplan show” in the asterisk CLI you should also have the entry:

Alt. Switch => 'Realtime/conference@extensions'
Check your “extensions.conf” if you don’t see it.
Now, as far as I can tell there is no direct way to see the “realtime” extensions from the CLI: you have to test it with a sip client. The following .sql snippet define an “echo” extension at 6878@conference:

INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "1", "Playback", "demo-echotest");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "2", "Echo", "");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "3", "Playback", "demo-echodone");
INSERT INTO extensions (context, exten, priority, app, appdata) VALUES ("conference", "6878", "4", "Hangup", "");

After inserted this records in the database (no need to reload – that’s realtime magic!) dialing “6878” from your sip client should enter you in echo test. A couple of things to keep in mind:

  1. “demo-echotest” and “demo-echodone” are audio files that you should have in your “sound” directory
  2. the sip peer should be registered with the context containing the “switch” directive, otherwise he won’t be able to access the realtime-configured context.

Well, I hope everything is clear enough for you to go on. Again, I am not diving too much in asterisk basics because the main focus of this tutorial will be hit in the following parts regarding java. Anyway, you are more than welcome to post your questions or to point me to the silly mistakes I may have done.
In the next part we will begin with actual Java coding: don’t miss it!!!


2 thoughts on “Tutorial on Linux + Asterisk +MySQL + Java – part 2

  1. What is the Asterisk version that you used in this series? I use asterisk 11 and the extensions tables look quite different from what you show above


    • Hi,
      this is the version I’m currently using: Asterisk 11.9.0. Just tested, and it worked. Anyway, I’ve been using this table definition since a long time ago, from a LOT of versions ago (Asterisk 1.4? 1.6?) and it always worked. What changed lately is the overall “Realtime” configuration


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s