Advertisement
Guest User

MQTestJMS11.java

a guest
Jan 18th, 2023
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.20 KB | None | 0 0
  1. import java.text.SimpleDateFormat;
  2. import java.util.Date;
  3. import java.util.Hashtable;
  4. import javax.jms.*;
  5.  
  6. import com.ibm.mq.jms.*;
  7. import com.ibm.msg.client.wmq.WMQConstants;
  8.  
  9. /**
  10.  * Program Name
  11.  *  MQTestJMS11
  12.  *
  13.  * Description
  14.  *  This java JMS class will connect to a remote queue manager and put a message to a queue.
  15.  *
  16.  * Sample Command Line Parameters
  17.  *  -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password
  18.  *
  19.  * @author Roger Lacroix
  20.  */
  21. public class MQTestJMS11
  22. {
  23.    private static final SimpleDateFormat  LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
  24.  
  25.    private Hashtable<String,String> params;
  26.    private MQQueueConnectionFactory mqQCF = null;
  27.  
  28.  
  29.    /**
  30.     * The constructor
  31.     */
  32.    public MQTestJMS11()
  33.    {
  34.       super();
  35.       params = new Hashtable<String,String>();
  36.    }
  37.  
  38.    /**
  39.     * Make sure the required parameters are present.
  40.     * @return true/false
  41.     */
  42.    private boolean allParamsPresent()
  43.    {
  44.       boolean b = params.containsKey("-h") && params.containsKey("-p") &&
  45.                   params.containsKey("-c") && params.containsKey("-m") &&
  46.                   params.containsKey("-q") &&
  47.                   params.containsKey("-u") && params.containsKey("-x");
  48.       if (b)
  49.       {
  50.          try
  51.          {
  52.             Integer.parseInt((String) params.get("-p"));
  53.          }
  54.          catch (NumberFormatException e)
  55.          {
  56.             b = false;
  57.          }
  58.       }
  59.  
  60.       return b;
  61.    }
  62.  
  63.    /**
  64.     * Extract the command-line parameters and initialize the MQ variables.
  65.     * @param args
  66.     * @throws IllegalArgumentException
  67.     */
  68.    private void init(String[] args) throws IllegalArgumentException
  69.    {
  70.       if (args.length > 0 && (args.length % 2) == 0)
  71.       {
  72.          for (int i = 0; i < args.length; i += 2)
  73.          {
  74.             params.put(args[i], args[i + 1]);
  75.          }
  76.       }
  77.       else
  78.       {
  79.          throw new IllegalArgumentException();
  80.       }
  81.  
  82.       if (allParamsPresent())
  83.       {
  84.          try
  85.          {
  86.             mqQCF = new MQQueueConnectionFactory();
  87.             mqQCF.setQueueManager((String) params.get("-m"));
  88.             mqQCF.setHostName((String) params.get("-h"));
  89.             mqQCF.setChannel((String) params.get("-c"));
  90.             mqQCF.setTransportType(WMQConstants.WMQ_CM_CLIENT);
  91.             try
  92.             {
  93.                mqQCF.setPort(Integer.parseInt((String) params.get("-p")));
  94.             }
  95.             catch (NumberFormatException e)
  96.             {
  97.                mqQCF.setPort(1414);
  98.             }
  99.          }
  100.          catch (JMSException e)
  101.          {
  102.             if (e != null)
  103.             {
  104.                MQTestJMS11.logger("getLinkedException()=" + e.getLinkedException());
  105.                MQTestJMS11.logger(e.getLocalizedMessage());
  106.                e.printStackTrace();
  107.             }
  108.             throw new IllegalArgumentException();
  109.          }
  110.          catch (Exception e)
  111.          {
  112.             MQTestJMS11.logger(e.getLocalizedMessage());
  113.             e.printStackTrace();
  114.             throw new IllegalArgumentException();
  115.          }
  116.       }
  117.       else
  118.       {
  119.          throw new IllegalArgumentException();
  120.       }
  121.    }
  122.  
  123.    /**
  124.     * Test the connection to the queue manager.
  125.     * @throws MQException
  126.     */
  127.    private void testConn()
  128.    {
  129.       QueueConnection conn = null;
  130.       QueueSession session = null;
  131.       Queue myQ = null;
  132.  
  133.       try
  134.       {
  135.          conn = mqQCF.createQueueConnection((String) params.get("-u"), (String) params.get("-x"));
  136.          conn.start();
  137.  
  138.          session = conn.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
  139.          MQTestJMS11.logger("successfully connected.");
  140.  
  141.          myQ = session.createQueue((String) params.get("-q"));
  142.  
  143.          MQDestination mqd = (MQDestination) myQ;
  144.          mqd.setTargetClient(WMQConstants.WMQ_CLIENT_JMS_COMPLIANT);
  145. //         mqd.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
  146.  
  147.          sendMsg( session, myQ);
  148.       }
  149.       catch (JMSException e)
  150.       {
  151.          if (e != null)
  152.          {
  153.             MQTestJMS11.logger("getLinkedException()=" + e.getLinkedException());
  154.             MQTestJMS11.logger(e.getLocalizedMessage());
  155.             e.printStackTrace();
  156.          }
  157.       }
  158.       catch (Exception e)
  159.       {
  160.          MQTestJMS11.logger(e.getLocalizedMessage());
  161.          e.printStackTrace();
  162.       }
  163.       finally
  164.       {
  165.          try
  166.          {
  167.             if (session != null)
  168.                session.close();
  169.          }
  170.          catch (Exception ex)
  171.          {
  172.             MQTestJMS11.logger("session.close() : " + ex.getLocalizedMessage());
  173.          }
  174.  
  175.          try
  176.          {
  177.             if (conn != null)
  178.                conn.stop();
  179.          }
  180.          catch (Exception ex)
  181.          {
  182.             MQTestJMS11.logger("connection.stop() : " + ex.getLocalizedMessage());
  183.          }
  184.  
  185.          try
  186.          {
  187.             if (conn != null)
  188.                conn.close();
  189.          }
  190.          catch (Exception ex)
  191.          {
  192.             MQTestJMS11.logger("connection.close() : " + ex.getLocalizedMessage());
  193.          }
  194.       }
  195.    }
  196.  
  197.    /**
  198.     * Send a message to a queue.
  199.     * @throws MQException
  200.     */
  201.    private void sendMsg(QueueSession session, Queue myQ) throws JMSException
  202.    {
  203.       QueueSender sender = null;
  204.  
  205.       try
  206.       {
  207.          TextMessage msg = session.createTextMessage();
  208.          msg.setText("Nice simple test. Time in 'ms' is  -> " + System.currentTimeMillis());
  209.          // msg.setJMSReplyTo(tq);
  210.          // msg.setJMSDeliveryMode( DeliveryMode.NON_PERSISTENT);
  211.  
  212.          MQTestJMS11.logger("Sending request to " + myQ.getQueueName());
  213.          MQTestJMS11.logger("");
  214.  
  215.          sender = session.createSender(myQ);
  216.          sender.send(msg);
  217.       }
  218.       finally
  219.       {
  220.          try
  221.          {
  222.             if (sender != null)
  223.                sender.close();
  224.          }
  225.          catch (Exception ex)
  226.          {
  227.             MQTestJMS11.logger("sender.close() : " + ex.getLocalizedMessage());
  228.          }
  229.       }
  230.    }
  231.  
  232.    /**
  233.     * A simple logger method
  234.     * @param data
  235.     */
  236.    public static void logger(String data)
  237.    {
  238.       String className = Thread.currentThread().getStackTrace()[2].getClassName();
  239.  
  240.       // Remove the package info.
  241.       if ( (className != null) && (className.lastIndexOf('.') != -1) )
  242.          className = className.substring(className.lastIndexOf('.')+1);
  243.  
  244.       System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
  245.    }
  246.  
  247.    /**
  248.     * mainline
  249.     * @param args
  250.     */
  251.    public static void main(String[] args)
  252.    {
  253.       MQTestJMS11 write = new MQTestJMS11();
  254.  
  255.       try
  256.       {
  257.          write.init(args);
  258.          write.testConn();
  259.       }
  260.       catch (IllegalArgumentException e)
  261.       {
  262.          MQTestJMS11.logger("Usage: java MQTestJMS11 -m QueueManagerName -h host -p port -c channel -q JMS_Queue_Name -u UserID -x Password");
  263.          System.exit(1);
  264.       }
  265.       catch (Exception e)
  266.       {
  267.          MQTestJMS11.logger(e.getLocalizedMessage());
  268.          System.exit(1);
  269.       }
  270.  
  271.       System.exit(0);
  272.    }
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement