From YiPs Wiki (i powered)

XMLSERVICE: Data Queues

(click to open)

Quick Page Table of Contents

Scanning…

Ruby Toolkit Don: Reading and Writing to Data Queues

XMLSERVICERubyDonWork

THIS PAGE IS COMPLETELY OUT OF DATE (WRONG)

do not use

The Basics

XMLService does not directly provide data queue support. Nevertheless, because XMLService makes it easy to call any IBMi command or program, you can easily work with data queues with the CRTDTAQ and DLTDTAQ commands and the QSNDDTAQ and QRCVDTAQ programs. The following creates a data queue:

    ActiveXMLService::Base.establish_connection connection: 'ActiveRecord'
    crtq = XMLService::I_CMD.new("CRTDTAQ DTAQ(QGPL/TESTDTAQ) MAXLEN(100)")
    crtq.xmlservice
    if crtq.xmlservice_error
      puts crtq.dump
    end

The following puts an entry on the data queue by calling QSNDDTAQ:

    qsnddtaq = XMLService::I_PGM.new("QSNDDTAQ", 'QSYS') <<
                       XMLService::I_a.new('queueName', 10, 'TESTDTAQ') <<
                       XMLService::I_a.new('libName', 10, 'QGPL') <<
                       XMLService::I_p.new('lenData', 5 , 0, 100.0) <<
                       XMLService::I_a.new('dataInput', 100, 'test data from Ruby for queue')
    qsnddtaq.xmlservice
    if qsnddtaq.xmlservice_error
      puts qsnddtaq.dump_all
    end

And now the following will retrieve the data queue entry just added above:

    qrcvdtaq = XMLService::I_PGM.new("QRCVDTAQ") <<
                      XMLService::I_a.new('queueName',10, 'TESTDTAQ') <<
                      XMLService::I_a.new('libName',10, 'QGPL') <<
                      XMLService::I_p.new('lenData', 5, 0, 100.0) <<
                      XMLService::I_a.new('dataOutput', 100, 'replace stuff here') <<
                      XMLService::I_p.new('waitTime', 5, 0, 20.0)
    qrcvdtaq.xmlservice
    if qrcvdtaq.xmlservice_error
      puts qrcvdtaq.dump_all
    else
      response = qrcvdtaq.xml_PARM3.xml_dataOutput.to_s
    end

For detailed information on the QSNDDTAQ and QRCVDTAQ programs see IBM’s documentation:
Send Data Queue program detail
Receive Data Queue program detail

Parsing and Formatting data queue entries

Data queue entries on the IBMi usually contain information that is in a fixed format. Typically an RPG data queue processing program uses an existing record format when reading or writing entries. To format and parse data queue entries in your Ruby code the most straightforward solution is to use the pack and unpack methods of the Array and String classes respectively. The Array class’s pack method turns all the elements of an array into a string following the format passed to the pack method. For example, the following takes an array with my name and address and “packs” the elements into a string using lengths specified in the argument to the pack method:

['Don', 'Denoncourt', '11203 Pinewood Ct.', 'Richmond', 'VA', '23238'].pack('A5A15A20A10A2A9')

produces:

"Don  Denoncourt     11203 Pinewood Ct.  Richmond  VA23238    "

If you’d like to use field names that match what is in the RPG data queue processing program, a simple strategy is to map field names to array entries is to use a parallel array with a hash to references values:

  DQ_FORMAT = 'A5A15A20A10A2A9'
  DQ_FIELD_NAMES = ["FIRST", "LAST", "STREET", "CITY", "STATE", "ZIP"]
  DQ_FIELDS = Hash[DQ_FIELD_NAMES.map.with_index.to_a]
  dq_array = Array.new(DQ_FIELDS.size, ' ')
  dq_array[DQ_FIELDS['FIRST']] = 'Don'
  dq_array[DQ_FIELDS['LAST']] = 'Denoncourt'
  dq_array[DQ_FIELDS['STREET']] = '11203 Pinewood Ct.'
  dq_array[DQ_FIELDS['CITY']] = 'Richmond'
  dq_array[DQ_FIELDS['STATE']] = 'VA'
  dq_array[DQ_FIELDS['ZIP']] = '23238'
  dq_entry = dq_array.pack(DQ_FORMAT)

The dq_entry string can then be pushed to a data queue with a call to QSNDDTAQ.

But to then read an entry retrieved from a data queue you would use the following:

  DQ_FORMAT = 'A5A15A20A10A2A9'
  DQ_FIELD_NAMES = ["FIRST", "LAST", "STREET", "CITY", "STATE", "ZIP"]
  DQ_FIELDS = Hash[DQ_FIELD_NAMES.map.with_index.to_a]
  # the dq_entry string would have been set from data queue read
  dq_entry = "Don  Denoncourt     11203 Pinewood Ct.  Richmond  VA23238    "
  dq_array = dq_entry.unpack(DQ_FORMAT)
  puts "Last name specifically from data queue: "+dq_array[DQ_FIELDS['LAST']]
  puts "Now just dump the whole array:"
  puts dq_array

Which outputs:

Last name specifically from data queue: Denoncourt
Now just dump the whole array:
Don
Denoncourt
11203 Pinewood Ct.
Richmond
VA
23238

Note that there are string parser and formatter utilities available such as Slither: A Dsl for Parsing Fixed Width Text Files

Retrieved from http://youngiprofessionals.com/wiki/index.php/XMLSERVICE/DataQueues
Page last modified on December 02, 2015, at 12:22 PM EST