Home > mqseries, php, Programming > Using mqseries to put multiple messages to a queue in PHP

Using mqseries to put multiple messages to a queue in PHP

The basic usage can be found everywhere via any search engine and the most official one can be found at this link. The problem with those examples is that none of them (at least so far) tells you how to write multiple messages to a queue in a proper way. Through a small project that I finished recently I found the trick how to do just that. Let’s start with the basic – putting a single message to a queue. The following code is slightly modified off the one on the PHP site:

<?php
    // open connection to the queue manager
    mqseries_conn('WMQ1', $conn, $comp_code, $reason);
    // $conn now hold the reference to the connection to the queue manager.

    // open the connectio to the testq queueu
    mqseries_open(
                $conn,
                array('ObjectName' => 'TESTQ'),
                MQSERIES_MQOO_INPUT_AS_Q_DEF | MQSERIES_MQOO_FAIL_IF_QUIESCING | MQSERIES_MQOO_OUTPUT,
                $obj,
                $comp_code,
                $reason);
    // $obj now holds the reference to the object (TESTQ)

    // setup the message descriptor array. Check MQSeries reference manuals.
    $md = array(
                'Version' => MQSERIES_MQMD_VERSION_1,
                'Expiry' => MQSERIES_MQEI_UNLIMITED,
                'Report' => MQSERIES_MQRO_NONE,
                'MsgType' => MQSERIES_MQMT_DATAGRAM,
                'Format' => MQSERIES_MQFMT_STRING,
                'Priority' => 1,
                'Persistence' => MQSERIES_MQPER_PERSISTENT);

    // setup the put message options.
    $pmo = array('Options' => MQSERIES_MQPMO_NEW_MSG_ID|MQSERIES_MQPMO_SYNCPOINT);
    
    $msg='test message.';
    // put the message $msg on the queueu.
    mqseries_put($conn, $obj, $md, $pmo, $msg, $comp_code, $reason);

    if ($comp_code !== MQSERIES_MQCC_OK) {
        printf("put CompCode:%d Reason:%d Text:%s<br>\n", $comp_code, $reason, mqseries_strerror($reason));
    }

    // close the object reference $obj    
    mqseries_close($conn, $obj, MQSERIES_MQCO_NONE, $comp_code, $reason);

// disconnect from the queue manager.    
    mqseries_disc($conn, $comp_code, $reason);
    
?>

The above code is pretty straight forward and in order to put multiple messages to a queue, we can either
1) Connect to mq server, open the queue, put one message, close the queue, ,close the connection then start over (from making connection to mq server) and put another message. This will work for sure but it’s way too inefficient.
2) Connect to mq server, open the queue, then enter a loop by calling mqseries_put until all messages are put into the queue, then close the queue and disconnect from mq. This is the preferred way and sometimes is the only right way because there might be a large number of messages to be put into the queue.

Here’s the modified code for putting multiple messages into the queue showing only the parts that are different to the above code.

<?php
...
    
    $messages=array(...);
    // put the messages on the queueu.
    foreach($messages as $msg) {
        mqseries_put($conn, $obj, $md, $pmo, $msg, $comp_code, $reason);

        if ($comp_code !== MQSERIES_MQCC_OK) {
            printf("put CompCode:%d Reason:%d Text:%s<br>\n", $comp_code, $reason, mqseries_strerror($reason));
        }
    }
...
    
?>

We expect the above code to work because it seems to be a very easy task. But to your surprise you will find the loop is only able to put the first message to the queue and as soon as the second time mqseries_put is called, a “Segmentation Fault” error is raised. I don’t know what exactly causes the problem but I found out that I have to re-initialize $md and $pmo variables each time before calling mqseries_put. The following version works:

<?php
...
    
    $messages=array(...);
    // put the messages on the queueu.
    foreach($messages as $msg) {
        // setup the message descriptor array. Check MQSeries reference manuals.
        $md = array(
                'Version' => MQSERIES_MQMD_VERSION_1,
                'Expiry' => MQSERIES_MQEI_UNLIMITED,
                'Report' => MQSERIES_MQRO_NONE,
                'MsgType' => MQSERIES_MQMT_DATAGRAM,
                'Format' => MQSERIES_MQFMT_STRING,
                'Priority' => 1,
                'Persistence' => MQSERIES_MQPER_PERSISTENT);

        // setup the put message options.
        $pmo = array('Options' => MQSERIES_MQPMO_NEW_MSG_ID|MQSERIES_MQPMO_SYNCPOINT);
        mqseries_put($conn, $obj, $md, $pmo, $msg, $comp_code, $reason);

        if ($comp_code !== MQSERIES_MQCC_OK) {
            printf("put CompCode:%d Reason:%d Text:%s<br>\n", $comp_code, $reason, mqseries_strerror($reason));
        }
    }
...
    
?>

Hope that helps if you also need to do something similar with mqseries in PHP.

Advertisements
Categories: mqseries, php, Programming
  1. No comments yet.
  1. No trackbacks yet.

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

%d bloggers like this: