Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.Hashtable;
- import javax.jms.*;
- import com.ibm.mq.jms.*;
- import com.ibm.msg.client.wmq.WMQConstants;
- /**
- * Program Name
- * MQTestJMS11
- *
- * Description
- * This java JMS class will connect to a remote queue manager and put a message to a queue.
- *
- * Sample Command Line Parameters
- * -m MQA1 -h 127.0.0.1 -p 1414 -c TEST.CHL -q TEST.Q1 -u UserID -x Password
- *
- * @author Roger Lacroix
- */
- public class MQTestJMS11
- {
- private static final SimpleDateFormat LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
- private Hashtable<String,String> params;
- private MQQueueConnectionFactory mqQCF = null;
- /**
- * The constructor
- */
- public MQTestJMS11()
- {
- super();
- params = new Hashtable<String,String>();
- }
- /**
- * Make sure the required parameters are present.
- * @return true/false
- */
- private boolean allParamsPresent()
- {
- boolean b = params.containsKey("-h") && params.containsKey("-p") &&
- params.containsKey("-c") && params.containsKey("-m") &&
- params.containsKey("-q") &&
- params.containsKey("-u") && params.containsKey("-x");
- if (b)
- {
- try
- {
- Integer.parseInt((String) params.get("-p"));
- }
- catch (NumberFormatException e)
- {
- b = false;
- }
- }
- return b;
- }
- /**
- * Extract the command-line parameters and initialize the MQ variables.
- * @param args
- * @throws IllegalArgumentException
- */
- private void init(String[] args) throws IllegalArgumentException
- {
- if (args.length > 0 && (args.length % 2) == 0)
- {
- for (int i = 0; i < args.length; i += 2)
- {
- params.put(args[i], args[i + 1]);
- }
- }
- else
- {
- throw new IllegalArgumentException();
- }
- if (allParamsPresent())
- {
- try
- {
- mqQCF = new MQQueueConnectionFactory();
- mqQCF.setQueueManager((String) params.get("-m"));
- mqQCF.setHostName((String) params.get("-h"));
- mqQCF.setChannel((String) params.get("-c"));
- mqQCF.setTransportType(WMQConstants.WMQ_CM_CLIENT);
- try
- {
- mqQCF.setPort(Integer.parseInt((String) params.get("-p")));
- }
- catch (NumberFormatException e)
- {
- mqQCF.setPort(1414);
- }
- }
- catch (JMSException e)
- {
- if (e != null)
- {
- MQTestJMS11.logger("getLinkedException()=" + e.getLinkedException());
- MQTestJMS11.logger(e.getLocalizedMessage());
- e.printStackTrace();
- }
- throw new IllegalArgumentException();
- }
- catch (Exception e)
- {
- MQTestJMS11.logger(e.getLocalizedMessage());
- e.printStackTrace();
- throw new IllegalArgumentException();
- }
- }
- else
- {
- throw new IllegalArgumentException();
- }
- }
- /**
- * Test the connection to the queue manager.
- * @throws MQException
- */
- private void testConn()
- {
- QueueConnection conn = null;
- QueueSession session = null;
- Queue myQ = null;
- try
- {
- conn = mqQCF.createQueueConnection((String) params.get("-u"), (String) params.get("-x"));
- conn.start();
- session = conn.createQueueSession(false, javax.jms.Session.AUTO_ACKNOWLEDGE);
- MQTestJMS11.logger("successfully connected.");
- myQ = session.createQueue((String) params.get("-q"));
- MQDestination mqd = (MQDestination) myQ;
- mqd.setTargetClient(WMQConstants.WMQ_CLIENT_JMS_COMPLIANT);
- // mqd.setTargetClient(WMQConstants.WMQ_CLIENT_NONJMS_MQ);
- sendMsg( session, myQ);
- }
- catch (JMSException e)
- {
- if (e != null)
- {
- MQTestJMS11.logger("getLinkedException()=" + e.getLinkedException());
- MQTestJMS11.logger(e.getLocalizedMessage());
- e.printStackTrace();
- }
- }
- catch (Exception e)
- {
- MQTestJMS11.logger(e.getLocalizedMessage());
- e.printStackTrace();
- }
- finally
- {
- try
- {
- if (session != null)
- session.close();
- }
- catch (Exception ex)
- {
- MQTestJMS11.logger("session.close() : " + ex.getLocalizedMessage());
- }
- try
- {
- if (conn != null)
- conn.stop();
- }
- catch (Exception ex)
- {
- MQTestJMS11.logger("connection.stop() : " + ex.getLocalizedMessage());
- }
- try
- {
- if (conn != null)
- conn.close();
- }
- catch (Exception ex)
- {
- MQTestJMS11.logger("connection.close() : " + ex.getLocalizedMessage());
- }
- }
- }
- /**
- * Send a message to a queue.
- * @throws MQException
- */
- private void sendMsg(QueueSession session, Queue myQ) throws JMSException
- {
- QueueSender sender = null;
- try
- {
- TextMessage msg = session.createTextMessage();
- msg.setText("Nice simple test. Time in 'ms' is -> " + System.currentTimeMillis());
- // msg.setJMSReplyTo(tq);
- // msg.setJMSDeliveryMode( DeliveryMode.NON_PERSISTENT);
- MQTestJMS11.logger("Sending request to " + myQ.getQueueName());
- MQTestJMS11.logger("");
- sender = session.createSender(myQ);
- sender.send(msg);
- }
- finally
- {
- try
- {
- if (sender != null)
- sender.close();
- }
- catch (Exception ex)
- {
- MQTestJMS11.logger("sender.close() : " + ex.getLocalizedMessage());
- }
- }
- }
- /**
- * A simple logger method
- * @param data
- */
- public static void logger(String data)
- {
- String className = Thread.currentThread().getStackTrace()[2].getClassName();
- // Remove the package info.
- if ( (className != null) && (className.lastIndexOf('.') != -1) )
- className = className.substring(className.lastIndexOf('.')+1);
- System.out.println(LOGGER_TIMESTAMP.format(new Date())+" "+className+": "+Thread.currentThread().getStackTrace()[2].getMethodName()+": "+data);
- }
- /**
- * mainline
- * @param args
- */
- public static void main(String[] args)
- {
- MQTestJMS11 write = new MQTestJMS11();
- try
- {
- write.init(args);
- write.testConn();
- }
- catch (IllegalArgumentException e)
- {
- MQTestJMS11.logger("Usage: java MQTestJMS11 -m QueueManagerName -h host -p port -c channel -q JMS_Queue_Name -u UserID -x Password");
- System.exit(1);
- }
- catch (Exception e)
- {
- MQTestJMS11.logger(e.getLocalizedMessage());
- System.exit(1);
- }
- System.exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement