Message queue [examples]

Open a handle to a message queue
MQOPEN
mqdes=mqopen("/myqueue")
if (mqdes < 0)     messagebox(strerror()+",errno="+alltrim(str(error())))     return endif do while (mqcurmsgs(mqdes) > 0)
    mstr=mqreceive(mqdes)
    if (empty(mstr))
        messagebox(strerror()+",errno="+alltrim(str(error())))
        return
    endif
    messagebox(mstr)
enddo
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQOPEN()


Close an open message queue
MQCLOSE
// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, "Test message")
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQCLOSE()


Create a message queue and open a pointer to the queue
MQCREATE
// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, "Test message")
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQCREATE()


Number of unread messages in the specified queue
MQCURMSGS
// Open up the queue for reading
mqdes=mqopen("/myqueue")
if (mqdes < 0)     messagebox(strerror()+",errno="+alltrim(str(error())))     return endif // do while there are messages do while (mqcurmsgs(mqdes) > 0)
    // Get each message
    mstr=mqreceive(mqdes)
    if (empty(mstr))
        messagebox(strerror()+",errno="+alltrim(str(error())))
        return
    endif
    messagebox(mstr)
enddo
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQCURMSGS()


Get a message from an open message queue
MQRECEIVE
// Open up the queue for reading
mqdes=mqopen("/myqueue")
if (mqdes < 0)     messagebox(strerror()+",errno="+alltrim(str(error())))     return endif // do while there are messages do while (mqcurmsgs(mqdes) > 0)
    // Get each message
    mstr=mqreceive(mqdes)
    if (empty(mstr))
        messagebox(strerror()+",errno="+alltrim(str(error())))
        return
    endif
    messagebox(mstr)
enddo
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQRECEIVE()


Send a message to an open message queue
MQSEND
// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, "Test message")
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
mqclose(mqdes)

https://www.lianja.com/doc/index.php/MQSEND()


Send a message to a named message queue
MQSENDMESSAGE
// Consumer
local mqdes, msg, data
 
// Create the consumer and wait for a connection
mqdes = mqCreate("unique_name_of_your_consumer")
 
// loop processing messages as they come in from multiple producers
do while .t.
       msg = mqReceive(mqdes)
        if len(msg)=0
            loop
        endif
        data = json_decode(msg)
        // Process the message which is now encoded as an object
        // ...
enddo
// ...
mqclose(mqdes)
 
// Producer using MQSENDMESSAGE()
// ...
mqSendMessage(“unique_name_of_your_consumer”,  json_encode(anobject))
 
// or
 
// Producer using MQSEND()
// ...
mqdes = mqOpen(“unique_name_of_your_consumer”)
msg = json_encode(anobject)
mqSend(mqdes, msg)
mqClose(mqdes)

https://www.lianja.com/doc/index.php/MQSENDMESSAGE()


Remove a message queue
MQUNLINK

 

// Open up the queue for read/write access
mqdes=mqcreate("/myqueue2", 2)
if (mqdes < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// send a message to the queue
rc = mqsend(mqdes, "Test message")
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif
// close the queue
mqclose(mqdes)
// remove the queue
rc = mqremove("/myqueue2")
if (rc < 0)
    messagebox(strerror()+",errno="+alltrim(str(error())))
    return
endif

https://www.lianja.com/doc/index.php/MQUNLINK()


Advertisements