Building Data Agents Using the Messaging Functions

From Recital Documentation Wiki
Revision as of 06:10, 26 October 2009 by Peterkelly (Talk | contribs)

Jump to: navigation, search

Building Data Agents Using the Messaging Functions

What Is Message Queuing

Message queuing is a communication tool that allows applications to reliably interconnect. The queue acts as a holding container for messages as they are sent between applications. The applications send messages to and read messages from queues to communicate back and forth. An application writes a message to a queue, which will then be received and processed by another application at some point determined by the receiving application. This type of communication is designed for asynchronous use where the applications involved are not waiting for an immediate response from the other end.

What are Data Agents

Data agents are applications written in the Recital scripting language that use the message queue functions to manage the transmitted data. Typically a data agent runs in the background reading data from the message queue and processing it. Any Recital application can open a message queue and transmit data to the Data Agent.

Building a Simple Data Agent

In this simple Data Agent example we will open a queue called recital_email which sends XML encoded email messages.

First we need to open a the queue

mqdes=mqopen("/recital_mail")
 // Check for error
 if (mqdes < 0)
     debug(strerror(error()))
     return
 endif

Then we need to wait in a loop processing incoming data.

do while .t.
     // Read each message 
     do while (mqcurmsgs(mqdes) > 0)
         // Read the next message
         mstr=mqreceive(mqdes)
         // Decode the xml message
         email = xml_decode(mstr)
         // process the message
         process_email(email)
     enddo
     // wait 1 second before retrying 
     sleep 1
  enddo

When we have finished we can close the queue

mqclose(mqdes)

The following code is for the process_email() function

function process_email(email)
    // Open SMTP for sending
    m_open = mailopen("mailserver.company.com","username","password","SMTP")
    if not m_open
        debug(mailerror())
        return
    endif
 
    sendmail(email.from, email.to,email.cc, email.subject, email.message)
    mailclose()
return

But first, messages will need to be added to the queue before the Data Agent can process them. The following example will add a XML encode email message to the queue.

mqdes=mqopen("/recital_mail")
 // Check for error
 if (mqdes < 0)
     debug(strerror(error()))
     return
 endif
 
declare email[] 
email.from = "info@recital.com"
email.to = "fred@recital.com;sue@recital.com"
email.cc = "bert@recital.co.uk;linda@recital.co.uk"
email.subject = "Update alert"
email.message = "Dear All" + chr(10) + "This is an alert message";
 + chr(10) + "Best regards" + chr(10) + "Recital"
mqsend(mqdes, xml_encode(email))
mqclose(mqdes)

Running the Simple Data Agent

Summary