Nirvana Java Client: JMS BytesMessage Subscriber

This example uses Nirvana JMS to consume Bytes Messages from a JMS Topic.

Usage

jmsbytessub <factoryname> <destinationName> <transacted> <selector>

<Required Arguments> 

<factoryname> - JMS Factory (Must exist in target realm)
<destinationName> - JMS Destination to subscribe to
<transacted> - Whether the session is transacted
<selector> - An optional message selector


Note: -? provides help on environment variables

Application Source Code

See also:

/**
 *
 *  ----------------------------------------------------------------------------------
 *
 *  PCB Systems Limited License Version 1.1
 *  Copyright  PCB Systems Limited. All rights reserved
 *
 *  In the event that you should download or otherwise use this software
 *  ( the "Software" ) you hereby acknowledge and agree that:
 *
 *  1. The Software is the property of PCB Systems Limited: Title, Copyright and all
 *  other proprietary rights, interest and benefit in and to the Software is and
 *  shall be owned by PCB Systems Limited;
 *
 *  2. You will not make copies of the Software whatsoever other than, if you should
 *  so wish, a single copy for archival purposes only;
 *
 *  3. You will not modify, reverse assemble, decompile, reverse engineer or otherwise
 *  translate the Software;
 *
 *  4. You will not redistribute, copy, forward electronically or circulate the Software
 *  to any person for any purpose whatsoever without the prior written consent of
 *  PCB Systems Limited;
 *
 *  5. You will not charge for, market or provide any managed service or product that
 *  is based upon or includes the Software or any variant of it; and
 *
 *  6. You will not use the Software for any purpose apart from your own personal,
 *  noncommercial and lawful use;
 *
 *  You hereby agree that the software is used by you on an "as is" basis, without
 *  warranty of any kind. PCB Systems Limited hereby expressly disclaim all warranties
 *  and conditions, either expressed or implied, including but not limited to any
 *  implied warranties or conditions or merchantability and fitness for a particular
 *  purpose.
 *
 *  You agree that you are solely responsible for determining the appropriateness of
 *  using the Software and assume all risks associated with it including but not
 *  limited to the risks of program errors, damage to or loss of of data, programs or
 *  equipment and unavailability or interruption of operations.
 *
 *  PCB Systems Limited will not be liable for any direct damages or for any, special,
 *  incidental or indirect damages or for any economic consequential damages ( including
 *  lost profits or savings ), or any damage howsoever arising.
 *
 *  ----------------------------------------------------------------------------------
 *
 */
package com.pcbsys.nirvana.nJMSApps;
import com.pcbsys.foundation.utils.fEnvironment;
import com.pcbsys.nirvana.nJMS.BytesMessageImpl;
import javax.jms.BytesMessage;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
public class BytesMessageSubscriber extends Subscriber {
  static public String sClassVersion = "$Name:  $ - $Revision: 1.1 $";
  // Sesson required for transacted operations
  Session s = null;
  // is the session transacted
  boolean transacted = false;
  public static void main(String[] args) {
    //Check to see if args were specified
    if ((args.length == 1) && args[0].equals("-?")) {
      UsageEnv();
    }
    if (args.length < 2) {
      Usage();
      System.exit(1);
    }
    //Process Environment Variables
    processEnvironmentVariable("RNAME");
    processEnvironmentVariable("PRINCIPAL");
    processEnvironmentVariable("PASSWORD");
    processEnvironmentVariable("CONTEXT_FACTORY");
    processEnvironmentVariable("PROVIDER_URL");
    processEnvironmentVariable("LOGLEVEL");
    processEnvironmentVariable("HPROXY");
    processEnvironmentVariable("HAUTH");
    processEnvironmentVariable("CKEYSTORE");
    processEnvironmentVariable("CKEYSTOREPASSWD");
    processEnvironmentVariable("CAKEYSTORE");
    processEnvironmentVariable("CAKEYSTOREPASSWD");
    // Install any proxy server settings
    fEnvironment.setProxyEnvironments();
    // Install any ssl settings
    fEnvironment.setSSLEnvironments();
    //Create an instance for this class
    BytesMessageSubscriber subscriber = new BytesMessageSubscriber();
    boolean transacted = false;
    String selector = null;
    if (args.length > 2) {
      transacted = new Boolean(args[2]).booleanValue();
    }
    if (args.length > 3) {
      selector = args[3];
    }
    //Subscribe to the destination specified
    subscriber.doIt(args[0], args[1], transacted, selector);
  }
  /**
   * A callback is received by the API to this method each time a message is received from
   * the destination.
   *
   * @param msg A JMS Message object
   */
  public void onMessage(Message msg) {
    try {
      if (msg instanceof BytesMessage) {
        BytesMessage bmsg = (BytesMessage) msg;
        byte[] data = new byte[1024];
        int count = bmsg.readBytes(data);
        System.out.println("JMS MSG ID : " + bmsg.getJMSMessageID());
        System.out.println("JMS DELIVERY MODE : " + bmsg.getJMSDeliveryMode());
        System.out.println("JMS TIME STAMP : " + bmsg.getJMSTimestamp());
        System.out.println("Received : " + new String(data));
        } else {
        super.onMessage(msg);
      }
      if (transacted) {
        s.commit();
      }
    } catch (JMSException e) {
      e.printStackTrace();
    }
  }
  /**
   * Prints the usage message for this class
   */
  protected static void Usage() {
    System.out.println("Usage ...\n\n");
    System.out.println("jmsbytessub <factoryname> <destinationName> <transacted> <selector>\n");
    System.out.println("<Required Arguments> \n");
    System.out.println("<factoryname> - JMS Factory (Must exist in target realm)");
    System.out.println("<destinationName> - JMS Destination to subscribe to");
    System.out.println("<transacted> - Whether the session is transacted");
    System.out.println("<selector> - An optional message selector");
    System.out.println("\n\nNote: -? provides help on environment variables \n");
  }
}
 // End of BytesMessageSubscriber Class
EXAMPLE_SOURCE_END
Share this page with others:
Tell Your Tweets Facebook It! Add to Delicious Reddit! Digg It! Stumble Upon Add to Your Faves Mixx it
Follow Us:
Keep up with my-Channels on Twitter Become a fan on Facebook LinkedIn Profile Recent Highlights RSS Feed