Difference between revisions of "Building Data Agents Using the Messaging Functions"

From Recital Documentation Wiki
Jump to: navigation, search
(Building a Simple Data Agent)
(Building a Simple Data Agent)
Line 7: Line 7:
  
 
===Building a Simple Data Agent===
 
===Building a Simple Data Agent===
In this simple Data Agent example we will open a queue called recital_queue which sends XML encoded messages.
+
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
+
First we need to open a the queue  
  
  mqdes=mqopen("/recital_queue")
+
<code lang="recital">
 +
  mqdes=mqopen("/recital_mail")
 
  // Check for error
 
  // Check for error
 
  if (mqdes < 0)
 
  if (mqdes < 0)
Line 17: Line 18:
 
     return
 
     return
 
  endif
 
  endif
 
+
</code>
 
Then we need to wait in a loop processing incoming data.  
 
Then we need to wait in a loop processing incoming data.  
 +
<code lang="recital">
 
  do while .t.
 
  do while .t.
 
     // Read each message  
 
     // Read each message  
Line 25: Line 27:
 
         mstr=mqreceive(mqdes)
 
         mstr=mqreceive(mqdes)
 
         // Decode the xml message
 
         // Decode the xml message
         obj1 = xml_decode(mstr)
+
         email = xml_decode(mstr)
         // TODO... process the message
+
         // process the message
 +
        process_email(email)
 
     enddo
 
     enddo
 
     // wait 1 second before retrying  
 
     // wait 1 second before retrying  
 
     sleep 1
 
     sleep 1
 
   enddo
 
   enddo
 
+
</code>
 
When we have finished we can close the queue
 
When we have finished we can close the queue
 +
<code lang="recital">
 
   mqclose(mqdes)
 
   mqclose(mqdes)
 +
</code>
 +
 +
The following code is for the process_email() function
 +
<code lang="recital">
 +
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
 +
 +
    mailsend(email.from, email.to,email.cc, email.sub, email.message, email.attachments)
 +
    mailclose()
 +
return
 +
</code>
  
 
===Running the Simple Data Agent===
 
===Running the Simple Data Agent===
 
===Summary===
 
===Summary===

Revision as of 02:21, 23 October 2009

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
 
    mailsend(email.from, email.to,email.cc, email.sub, email.message, email.attachments)
    mailclose()
return

Running the Simple Data Agent

Summary