     H DEBUG(*YES)
     T* Data queue handler
     O* CRTBNDRPG DFTACTGRP(*NO) ACTGRP(DQ) DBGVIEW(*ALL) BNDDIR(QC2LE)
     /*                                                                    +
      * Copyright (c) 2009 Rory Hewitt                                     +
      * All rights reserved.                                               +
      *                                                                    +
      * Redistribution and use in source and binary forms, with or without +
      * modification, are permitted provided that the following conditions +
      * are met:                                                           +
      * 1. Redistributions of source code must retain the above copyright  +
      *    notice, this list of conditions and the following disclaimer.   +
      * 2. Redistributions in binary form must reproduce the above         +
      *    copyright notice, this list of conditions and the following     +
      *    disclaimer in the documentation and/or other materials provided +
      *    with the distribution.                                          +
      *                                                                    +
      * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ''AS IS'' +
      * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED  +
      * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A    +
      * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR   +
      * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,    +
      * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT   +
      * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF   +
      * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    +
      * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        +
      * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN  +
      * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE    +
      * POSSIBILITY OF SUCH DAMAGE.                                        +
      */
      *=====================================================================
      *
      * This is an example data queue handler. It is called by a data queue
      * server and is passed the data queue entry, details of the job which
      * put the entry onto the data queue and a return code.
      *
      * If the data queue entry begins with 'CMD:', the remaining data will
      * be executed as a command, otherwise it will simply be written as an
      * information message to the job log.
      *
      * If a command fails, the return code will be set to '*WARN', which
      * will cause the data queue server to write a diagnostic message to
      * the job log. If an error occurs in this program, the return code
      * will be set to '*ERROR', which will cause the data queue server to
      * end abnormally.
      *
      *=====================================================================

     D ThisProgram     C                   'DQHDLR'

      *---------------------------------------------------------------------
      * Global variables
      *---------------------------------------------------------------------

      /COPY QRPGLESRC,DQCOPY                      

      * QCMDEXC - Execute Command API

     D CmdStr          S          32702A
     D qcmdexc         PR                  Extpgm('QCMDEXC')
     D   CmdStr                   32702A   Const Options(*Varsize)
     D   CmdStrLen                   15P 5 Const
     D   AllowDBCS                    3A   Const Options(*Nopass)

      * QUSEC - API Error Structure

     D QUSEC           DS
     D  ErrBytesProv                 10I 0 Inz(%size(QUSEC))
     D  ErrBytesAvail                10I 0 Inz
     D  ErrMsgID                      7A
     D                                1
     D  ErrMsgDta                   512A

      * QMHSNDPM - Send Program Message API

     D qmhsndpm        PR                  Extpgm('QMHSNDPM')
     D   Msgid                        7A   Const
     D   qMsgf                       20A   Const
     D   MsgDta                   65535A   Const Options(*Varsize)
     D   MsgDtaLen                   10I 0 Const
     D   MsgType                     10A   Const
     D   CSEntry                   4096A   Const
     D   CSCount                     10I 0 Const
     D   MsgKey                       4A
     D   ApiError                          Like(QUSEC)
     D   CSEntryLen                  10I 0 Const Options(*Nopass)
     D   qCSEntry                    20A   Const Options(*Nopass)
     D   DPMSWT                      10I 0 Const Options(*Nopass)
     D   CSEntryDType                10A   Const Options(*Nopass)
     D   CCSID                       10I 0 Const Options(*Nopass)

     D MsgKey          S              4A   Inz
     D MsgDta          S           3000A   Inz Varying
     D Data            S                   Inz Like(DQ_Data)
     D qSendJob        DS                  Inz Likeds(DQ_qSendJob)

      *---------------------------------------------------------------------
      * Main procedure interface
      *---------------------------------------------------------------------

     D main            PR                  ExtPgm(ThisProgram)
     D   p_Key                             Const Like(DQ_Key)
     D   p_Data                            Const Like(DQ_Data)
     D   p_qSendJob                        Const Likeds(DQ_qSendJob)
     D   p_RtnCde                                Like(DQ_RtnCde)

     D main            PI
     D   p_Key                             Const Like(DQ_Key)
     D   p_Data                            Const Like(DQ_Data)
     D   p_qSendJob                        Const Likeds(DQ_qSendJob)
     D   p_RtnCde                                Like(DQ_RtnCde)

      *=====================================================================
      * MAINLINE
      *=====================================================================
      /free

        Data = p_Data;
        qSendJob = p_qSendJob;
        clear p_RtnCde;

        // If the data begins with 'CMD:', it's a command, so execute it,
        // otherwise simply add it to the job log as an information message

        if %subst( p_Data : 1 : 4 ) = 'CMD:';
          Data = %subst( p_Data : 5 );
          MsgDta = '/* Command: ' + %trim( Data ) + ' */';
          reset QUSEC;
          qmhsndpm( *blanks : *blanks : MsgDta : %len( MsgDta ) :
                    '*INFO' : '*' : 2 : MsgKey : QUSEC );
          CmdStr = Data;
          callp(e) qcmdexc( CmdStr : %len( CmdStr ) );
          if %error;
            p_RtnCde = '*WARN';
          endif;
        else;
          MsgDta = %trim( p_Data );
          reset QUSEC;
          qmhsndpm( *blanks : *blanks : MsgDta : %len( MsgDta ) :
                    '*INFO' : '*' : 2 : MsgKey : QUSEC );
        endif;

        exsr return;

        // return: Return to the caller

        begsr return;
          return;
        endsr;

        // *PSSR: Error-handling subroutine

        begsr *pssr;
          p_RtnCde = '*ERROR';
          exsr return;
        endsr;

      /end-free

