JMS/MQ Program That Creates Connection Factory from Scratch

On StackOverflow, someone asked a question about why their JMS/MQ code was failing. There are many errors in the code. Here is a fully functioning JMS/MQ program that will create the Connection Factory from scratch, connect to a remote queue manager and put a message to a queue.

You can download the source code from here.

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import javax.jms.*;

import com.ibm.msg.client.jms.*;
import com.ibm.msg.client.wmq.*;

/**
 * Program Name
 *  MQTestJMS51
 *
 * Description
 *  This java JMS class will connect to a remote queue manager and put a message to a queue.
 *  This code will create the Connection Factory from scratch.
 *
 * 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 MQTestJMS51
{
   private static final SimpleDateFormat  LOGGER_TIMESTAMP = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
   private Hashtable<String,String> params;

   public MQTestJMS51()
   {
      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");
      
      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())
      {
         throw new IllegalArgumentException();
      }
   }

   /**
    * Create a connection to the queue manager then publish a message to a queue.
    */
   private void handleIt() 
   {
      Connection conn = null;
      Session session = null;
      Destination destination = null;
      MessageProducer producer = null;
      
      try
      {
         /*
          * Set JVM system environment variables
          */
         System.setProperty("com.ibm.mq.cfg.useIBMCipherMappings", "false");
         
//         System.setProperty("javax.net.ssl.trustStore", trustedStore);
//         System.setProperty("javax.net.ssl.trustStorePassword", trustedStorePasswd);
         
         JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
         JmsConnectionFactory cf = ff.createConnectionFactory();

         // Set the properties
         cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, (String) params.get("-h"));
         try
         {
            cf.setIntProperty(WMQConstants.WMQ_PORT, Integer.parseInt((String) params.get("-p")));
         }
         catch (NumberFormatException e)
         {
            cf.setIntProperty(WMQConstants.WMQ_PORT, 1414);
         }
         
         cf.setStringProperty(WMQConstants.WMQ_CHANNEL, (String) params.get("-c"));
         cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
         cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, (String) params.get("-m"));
         cf.setStringProperty(WMQConstants.WMQ_APPLICATIONNAME, "MQTestJMS51");
         cf.setBooleanProperty(WMQConstants.USER_AUTHENTICATION_MQCSP, true);
         
         cf.setStringProperty(WMQConstants.USERID, (String) params.get("-u"));
         cf.setStringProperty(WMQConstants.PASSWORD, (String) params.get("-x"));
         
//         cf.setStringProperty(WMQConstants.WMQ_SSL_CIPHER_SUITE, "TLS_RSA_WITH_AES_128_CBC_SHA256");
         
         conn = cf.createConnection((String) params.get("-u"), (String) params.get("-x"));
         logger("created connection.");
         
         // Start the connection
         conn.start();
         logger("started connection.");

         session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
         logger("created session.");

         /*
          * targetClient=0 means it is sending a JMS message
          * targetClient=1 means it is sending an MQ message (non JMS)
          */
         destination = session.createQueue("queue://" + (String) params.get("-m") + "/" + (String) params.get("-q") + "?targetClient=0");
         logger("created destination.");

         producer = session.createProducer(destination);
         logger("created producer.");
         
         sendMsg(session, producer);
      }
      catch (JMSException e)
      {
         if (e != null)
         {
            logger(e.getLocalizedMessage());
            e.printStackTrace();
            
            Exception gle = e.getLinkedException();
            if (gle != null)
               logger(gle.getLocalizedMessage());
         }
      }
      finally
      {
         try
         {
            if (producer != null)
            {
               producer.close();
               logger("closed producer.");
            }
         }
         catch (Exception e)
         {
            logger("producer.close() : " + e.getLocalizedMessage());
         }

         try
         {
            if (session != null)
            {
               session.close();
               logger("closed session.");
            }
         }
         catch (Exception e)
         {
            logger("session.close() : " + e.getLocalizedMessage());
         }

         try
         {
            if (conn != null)
            {
               conn.stop();
               logger("stopped connection.");
            }
         }
         catch (Exception e)
         {
            logger("conn.stop() : " + e.getLocalizedMessage());
         }

         try
         {
            if (conn != null)
            {
               conn.close();
               logger("closed connection.");
            }
         }
         catch (Exception e)
         {
            logger("connection.close() : " + e.getLocalizedMessage());
         }
      }
   }

   /**
    * Send a message to a queue.
    */
   private void sendMsg(Session session, MessageProducer producer)
   {
      try
      {
         long uniqueNumber = System.currentTimeMillis() % 1000;
         TextMessage msg = session.createTextMessage("Your lucky number today is " + uniqueNumber);

         producer.send(msg);
         logger("Sent message: " + msg);
      }
      catch (JMSException e)
      {
         if (e != null)
         {
            logger(e.getLocalizedMessage());
            e.printStackTrace();
            
            Exception gle = e.getLinkedException();
            if (gle != null)
               logger(gle.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);
   }

   /**
    * main line
    * @param args
    */
   public static void main(String[] args)
   {
      logger("starting...");
      try
      {
         MQTestJMS51 tj = new MQTestJMS51();
         tj.init(args);
         tj.handleIt();
      }
      catch (IllegalArgumentException e)
      {
         logger("Usage: java MQTestJMS51 -m QueueManagerName -h host -p port -c channel -q Queue_Name -u UserID -x Password");
      }
      catch (Exception e)
      {
         logger(e.getLocalizedMessage());
         e.printStackTrace();
      }
      logger("ending...");
   }
}

Regards,
Roger Lacroix
Capitalware Inc.

HPE NonStop, IBM i (OS/400), IBM MQ, IBM MQ Appliance, Java, JMS, Linux, macOS (Mac OS X), Programming, Raspberry Pi, Unix, Windows, z/OS Comments Off on JMS/MQ Program That Creates Connection Factory from Scratch

Ubuntu 21.10 Released

Canonical has just released Ubuntu v21.10.
https://releases.ubuntu.com/21.10/

Super-fast, easy to use and free, the Ubuntu operating system powers millions of desktops, netbooks and servers around the world. Ubuntu does everything you need it to. It’ll work with your existing PC files, printers, cameras and MP3 players. And it comes with thousands of free apps.

Regards,
Roger Lacroix
Capitalware Inc.

Linux, Open Source, Operating Systems Comments Off on Ubuntu 21.10 Released

OpenBSD v7.0 Released

Theo de Raadt has just released OpenBSD v7.0.
https://www.openbsd.org/70.html

The OpenBSD project produces a FREE, multi-platform 4.4BSD-based UNIX-like operating system. Our efforts emphasize portability, standardization, correctness, proactive security and integrated cryptography.

Regards,
Roger Lacroix
Capitalware Inc.

Open Source, Operating Systems Comments Off on OpenBSD v7.0 Released

IBM MQ End of Service Dates

So, what version of IBM MQ (aka WebSphere MQ & MQSeries) are you running? Are you behind in upgrading your release of MQ?

Everybody “should be” running IBM MQ v9.1 or v9.2, if you want support from IBM.

In case it has slipped your mind, here is a list of IBM MQ releases and there end of service dates:

Note: IBM v9.1 and v9.2 do not yet have end of service dates.

Regards,
Roger Lacroix
Capitalware Inc.

IBM i (OS/400), IBM MQ, IBM MQ Appliance, Linux, Unix, Windows, z/OS Comments Off on IBM MQ End of Service Dates

New features between Java 8 and Java 17

Ondro Mihályi has just posted a blog item called: New features between Java 8 and Java 17
https://ondro.inginea.eu/index.php/new-features-in-java-versions-since-java-8/

It is very handy when you are trying to write backwards compatible code.

Regards,
Roger Lacroix
Capitalware Inc.

Java, JMS, Linux, macOS (Mac OS X), Programming, Raspberry Pi, Unix, Windows Comments Off on New features between Java 8 and Java 17

Adoptium Released Java 17 Builds

The Eclipse Adoptium (previously known as Adopt OpenJDK) has released Java 17 builds for AIX ppc64, alpine-Linux x64, Linux aarch64, Linux arm32, Linux ppcle64, Linux s390x, Linux x64, Mac aarch64, Mac x64, Windows x32 & x64.

Eclipse Termurin 17.png

The mission of the Eclipse Adoptium Top-Level Project is to produce high-quality runtimes and associated technology for use within the Java ecosystem. We achieve this through a set of Projects under the Adoptium PMC and a close working partnership with external projects, most notably OpenJDK for providing the Java SE runtime implementation. Our goal is to meet the needs of both the Eclipse community and broader runtime users by providing a comprehensive set of technologies around runtimes for Java applications that operate alongside existing standards, infrastructures, and cloud platforms.

Downloads:
https://adoptium.net/releases.html?variant=openjdk17

GitHub Release Status:
https://github.com/adoptium/adoptium/issues/74?utm_content=180839560&utm_medium=social&utm_source=twitter&hss_channel=tw-980259840

Regards,
Roger Lacroix
Capitalware Inc.

Java, JMS, Linux, macOS (Mac OS X), Programming, Raspberry Pi, Unix, Windows Comments Off on Adoptium Released Java 17 Builds

Java 17 Released

Oracle has just released Java 17.

Java Platform, Standard Edition (Java SE) lets you develop and deploy Java applications on desktops and servers, as well as in today’s demanding embedded environments. Java offers the rich user interface, performance, versatility, portability, and security that today’s applicationsrequire.

Regards,
Roger Lacroix
Capitalware Inc.

IBM i (OS/400), Java, JMS, Linux, macOS (Mac OS X), Programming, Raspberry Pi, Unix, Windows, z/OS Comments Off on Java 17 Released

IBM MQ Fix Pack 9.1.0.9 Released

IBM has just released Fix Pack 9.1.0.9 for IBM MQ V9.1 LTS:
https://www.ibm.com/support/pages/downloading-ibm-mq-9109

Regards,
Roger Lacroix
Capitalware Inc.

Fix Packs for MQ, IBM i (OS/400), IBM MQ, IBM MQ Appliance, Linux, Unix, Windows Comments Off on IBM MQ Fix Pack 9.1.0.9 Released

IBM Announces IBM Semeru Runtimes

IBM has introduced no-cost IBM Semeru Runtimes to develop and run Java applications.
https://developer.ibm.com/blogs/introducing-the-ibm-semeru-runtimes/
https://developer.ibm.com/languages/java/semeru-runtimes/

IBM Semeru Runtimes use the class libraries from OpenJDK, along with the Eclipse OpenJ9 Java Virtual Machine to enable developers to build and deploy Java applications that will start quickly, deliver great performance, all while using less memory.

    Key highlights of these runtimes include:

  • Provides a stable, no-cost environment for developing Java workloads
  • Available across a wide variety of hardware and software platforms, including on-premises, in public cloud, or on container orchestrators like Kubernetes and OpenShift
  • Includes commonly used JDK releases like the JDK 8 and JDK 11 Long Term Support (LTS) releases
  • Performance benefits from deep technology investment in Eclipse OpenJ9
  • Zero usage restrictions, so you can use these runtimes for development or in production

You can review the performance overview at: https://www.eclipse.org/openj9/performance/. It looks pretty impressive.

Regards,
Roger Lacroix
Capitalware Inc.

Java, JMS, Linux, macOS (Mac OS X), Programming, Unix, Windows Comments Off on IBM Announces IBM Semeru Runtimes

How Long it Takes for a Hacker to Crack Your Password

Over at Komando’s web site, they have an article called: Use this chart to see how long it’ll take to crack your passwords.

A general rule is that your password should be at least 11 characters and use numbers, along with upper and lowercase letters. That combination will take hackers 41 years to crack.

Here’s the chart of password lengths, contents of password and how long it will take to crack it using brute force method:

Time it takes a hacker to brute force your password.

Regards,
Roger Lacroix
Capitalware Inc.

Education, Security Comments Off on How Long it Takes for a Hacker to Crack Your Password