Java combined with WebSphere MQ implements receive queue file functionality _java

Source: Internet
Author: User
Tags call shell time limit

Let's start with a brief introduction to WebSphere MQ and installation

WebSphere MQ: A cross-platform feature for transmitting information.

1 Install WebSphere MQ and start

2 WebSphere MQ establish queue Manager (e.g. MQSI_SAMPLE_QM)

3 Establish the queue type select a local type (such as LQ)

4 Establish channels type Select Server Connection (such as Bridgechannel)

Next, let's look at the instance code:

Mqfilereceiver.java package com.mq.dpca.file;
Import Java.io.File;
 
Import Java.io.FileOutputStream;
Import com.ibm.mq.MQEnvironment;
Import com.ibm.mq.MQException;
Import com.ibm.mq.MQGetMessageOptions;
Import Com.ibm.mq.MQMessage;
Import Com.ibm.mq.MQQueue;
Import Com.ibm.mq.MQQueueManager;
Import com.ibm.mq.constants.MQConstants;
Import Com.mq.dpca.msg.MQConfig;
Import Com.mq.dpca.util.ReadCmdLine;
 
Import Com.mq.dpca.util.RenameUtil; /** * * MQ packet receive file function * Active polling/public class Mqfilereceiver {private Mqqueuemanager qmgr;//Connect to Queue Manager private MQQ Ueue InQueue; Transmission queue Private String queuename = ""; Queue name Private String host = ""; private int port = 1414; The port number of the listener is private String channel = ""; Channel name private String qmgrname = ""; Queue Manager Private Mqmessage inmsg; Create message buffering private mqgetmessageoptions GMO; Set GET message option private static String fileName = null;
 
  Receive messages on queue and save to file private int ccsid = 0; private static String File_diR = null; /** * Program Entry * * @param args/public static void Main (String args[]) {mqfilereceiver MFS = new Mqfile
    Receiver ();
    Initialize connection Mfs.initproperty ();
    Receive file Mfs.rungoupreceiver ();
Gets the shell script name//String Shellname = Mqconfig.getvaluebykey (fileName); if (shellname!=null&&! "". Equals (Shellname)) {////Call Shell//Readcmdline.callshell (shellname);//}else{//System.out.println ("have
No shell name,only receive files. ");
      } public void Rungoupreceiver () {try {init ();
      Getgroupmessages ();
      Qmgr.commit ();
    System.out.println ("\ n Messages successfully Receive");
      catch (Mqexception mqe) {mqe.printstacktrace ();
        try {System.out.println ("\ backing out Transaction");
        Qmgr.backout ();
      System.exit (2);
        catch (Exception e) {e.printstacktrace ();
      System.exit (2); The catch (Exception e) {e.printstacktrace ()};
    System.exit (2); /** * Initialize Server connection information * * @throws Exception * * private void init () throws Exception {/* Set M for client connection
    Qenvironment attribute */mqenvironment.hostname = host;
    Mqenvironment.channel = Channel;
 
    Mqenvironment.port = port;
 
    /* Connect to Queue Manager * * qmgr = new Mqqueuemanager (qmgrname); /* Set queue open option to input/int OPNOPTN = Mqconstants.mqoo_input_as_q_def |
 
    mqconstants.mqoo_fail_if_quiescing;
  /* Open queue to lose/InQueue = Qmgr.accessqueue (queuename, OPNOPTN, NULL, NULL, NULL);  /** * Accept the main function of the file * * @throws Exception/public void Getgroupmessages () {/* Settings get message option * * GMO =
    New Mqgetmessageoptions ();
    Gmo.options = mqconstants.mqgmo_fail_if_quiescing;
    Gmo.options = gmo.options + mqconstants.mqgmo_syncpoint;
    /* Wait Message * * gmo.options = gmo.options + mqconstants.mqgmo_wait;
    /* Set wait time limit */gmo.waitinterval = 5000; /* Only get the message * * gmo.options = gmo.options + Mqconstants.mqgmo_all_msgs_available;
    * * To obtain the message in sequence/gmo.options = gmo.options + Mqconstants.mqgmo_logical_order;
    Gmo.matchoptions = mqconstants.mqmo_match_group_id;
    /* Create Message Buffer * * inmsg = new Mqmessage ();
      try {FileOutputStream fos = null;
          /* Processing Group Message */while (true) {try {inqueue.get (inmsg, GMO);
              if (FOS = = null) {try {filename = inmsg.getstringproperty ("filename");
              String filename_full = null;
              Filename_full = File_dir + renameutil.rename (fileName);
              FOS = new FileOutputStream (new File (Filename_full));
              int msglength = Inmsg.getmessagelength ();
              byte[] buffer = new Byte[msglength];
              inmsg.readfully (buffer);
              Fos.write (buffer, 0, msglength);
              /* See if it is the last message ID/char x = gmo.groupstatus; if (x = = Mqconstants.mqgs_last_msg_in_group) {System.out.println ("last MSG in GrouP ");
              Break
 
            } inmsg.clearmessage (); catch (Exception e) {System.out. println ("Receiver" without Property,do
              Ing! ");
            Inmsg.clearmessage ();
            } else {int msglength = Inmsg.getmessagelength ();
            byte[] buffer = new Byte[msglength];
            inmsg.readfully (buffer);
            Fos.write (buffer, 0, msglength);
            /* See if it is the last message ID/char x = gmo.groupstatus;
              if (x = = Mqconstants.mqgs_last_msg_in_group) {System.out.println ("Last MSG in GROUP");
            Break
          } inmsg.clearmessage ();
          The catch (Exception e) {char x = gmo.groupstatus;
          if (x = = Mqconstants.mqgs_last_msg_in_group) {System.out.println ("Last MSG in GROUP");
        } break; } if (FOS!= null) FOS. Close ();
    catch (Exception e) {System.out.println (E.getmessage ());
    } public void Initproperty () {mqconfig config = new Mqconfig (). getinstance ();
      if (Config.getmq_manager ()!= null) {qmgrname = Config.getmq_manager ();
      QueueName = Config.getm Q_Q ueue_name ();
      Channel = Config.getmq_channel ();
      Host = Config.getmq_host_name ();
      Port = integer.valueof (Config.getmq_prot ());
      Ccsid = integer.valueof (Config.getmq_ccsid ());
    File_dir = Config.getfile_dir (); }
  }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.