/* %Z% %W% %I% %E% %U% */
/*
 * <copyright
 * notice="lm-source-program"
 * pids="5755-P60"
 * years="2013,2014"
 * crc="3568777996" >
 * Licensed Materials - Property of IBM
 *
 * 5755-P60
 *
 * (C) Copyright IBM Corp. 2013, 2014
 *
 * US Government Users Restricted Rights - Use, duplication or
 * disclosure restricted by GSA ADP Schedule Contract with
 * IBM Corp.
 * </copyright>
 */
/* jslint node: true */
/* jshint -W083,-W097 */
'use strict';

var mqlight = require('mqlight');
var nopt = require('nopt');
var uuid = require('node-uuid');

// parse the commandline arguments
var types = {
  service: String,
  topic: String,
  delay: Number
};
var shorthands = {
  s: ['--service'],
  t: ['--topic'],
  d: ['--delay'],
  h: ['--help']
};
var parsed = nopt(types, shorthands, process.argv, 2);

if (parsed.help) {
  console.log('Usage: send.js [options] <msg_1> ... <msg_n>');
  console.log('');
  console.log('Options:');
  console.log('  -h, --help            show this help message and exit');
  console.log('  -s URL, --service=URL service to connect to' +
              ' (default: amqp://localhost)');
  console.log('  -t TOPIC, --topic=TOPIC');
  console.log('                        send messages to topic TOPIC' +
              ' (default: public)');
  console.log('  -d NUM, --delay=NUM   add a NUM seconds time delay between' +
              ' each request');
  console.log('');
  process.exit(0);
}

var topic = parsed.topic ? parsed.topic : 'public';
var service = parsed.service ? parsed.service : 'amqp://localhost';

// create client to connect to broker with
var opts = {
  service: service,
  id: 'send_' + uuid.v4().substring(0, 7)
};
var client = mqlight.createClient(opts);

// get message body data to send
var remain = parsed.argv.remain;
var messages = (remain.length > 0) ? remain : ['Hello World!'];

// insert a delay between sends if requested
var delay = parsed.delay * 1000 || 0;

// once connection is acquired, send messages
client.on('connected', function() {
  console.log('Connected to %s using client-id %s', service, client.id);
  console.log('Publishing to: %s', topic);

  // queue all messages for sending
  var i = 0;
  var sendNextMessage = function() {
    var body = messages[i];
    client.send(topic, body, function(err, topic, data, options) {
      if (err) {
        console.error('Problem with send request: %s', err.message);
        process.exit(1);
      }
      if (data) {
        console.log('# sent message:');
        console.log(data);
      }
      // if there are more messages pending, send the next in <delay> seconds
      if (messages.length > ++i) {
        if (delay > 0) {
          setTimeout(sendNextMessage, delay);
        } else {
          setImmediate(sendNextMessage);
        }
      } else {
        client.disconnect();
      }
    });
  };

  sendNextMessage();
});

client.on('error', function(error) {
  console.error('*** error ***');
  if (error) {
    if (error.message) console.error('message: %s', error.message);
    else if (error.stack) console.error(error.stack);
  }
  console.error('exiting.');
  process.exit(1);
});

// Make the connection
client.connect(function(err) {
  if (err) {
    console.error(err.message);
    process.exit(1);
  }
});

@@ -639,16 +639,41 @@
 lay:
+ Number,%0A  'message-ttl':
  Number%0A
 %7D;%0Av
@@ -668,16 +668,16 @@
  Number%0A
-
 %7D;%0Avar s
@@ -1282,24 +1282,125 @@
  public)');%0A
+  console.lof('  --message-ttl=NUM     set message time-to-live to NUM ' +%0A              'seconds');%0A
   console.lo
@@ -2351,16 +2351,226 @@
 ges%5Bi%5D;%0A
+    var options = %7B%7D;%0A    if (parsed%5B'message-ttl'%5D) %7B%0A      var ttl = Number(parsed%5B'message-ttl'%5D);%0A      if (!Number.isNaN(ttl) && Number.isFinite(ttl) && ttl %3E 0) %7B%0A        options.ttl = ttl;%0A      %7D%0A    %7D%0A
     clie
@@ -2589,16 +2589,25 @@
 c, body,
+ options,
  functio

Add --message-ttl option to send.js sample