title: Mainframe MQ to Standalone JMS in EBCDIC
body: <p>From a Java app I'm trying to browse messages from a mainframe IBM MQ queue (EBCDIC messages). I need to browse messages, not consume them. Here is the code:</p>
  
  <pre class="lang-java prettyprint-override"><code>JmsFactoryFactory ff = JmsFactoryFactory.getInstance(WMQConstants.WMQ_PROVIDER);
  JmsConnectionFactory cf = ff.createConnectionFactory();
  
  // Set properties
  cf.setStringProperty(WMQConstants.WMQ_HOST_NAME, host);
  cf.setIntProperty(WMQConstants.WMQ_PORT, port);
  cf.setStringProperty(WMQConstants.WMQ_CHANNEL, channel);
  cf.setIntProperty(WMQConstants.WMQ_CONNECTION_MODE, WMQConstants.WMQ_CM_CLIENT);
  cf.setStringProperty(WMQConstants.WMQ_QUEUE_MANAGER, qmgr);
  cf.setIntProperty(WMQConstants.WMQ_RECEIVE_CONVERSION, WMQConstants.WMQ_RECEIVE_CONVERSION_QMGR);
  
  QueueBrowser browser = context.createBrowser(context.createQueue("queue:///" + queueName  + "?targetClient=1"));
  Enumeration enumeration = browser.getEnumeration();
  
  while (enumeration.hasMoreElements()) {
      TextMessage messageInTheQueue = (TextMessage) enumeration.nextElement();
      System.out.println(messageInTheQueue);
      nbRecords++;
  }
  </code></pre>
  
  <p>The result of <code>System.out.println()</code> looks like:</p>
  
  <pre><code>  JMSMessage class: jms_text
    JMSType:          null
    JMSDeliveryMode:  2
    JMSMessageID:     ID:c1d4d840d4d8e3c1e2f24040404040405e2432bd21aa1b02
    JMSTimestamp:     1579537307450
    JMSRedelivered:   false
      JMSXAppID:  
      JMSXDeliveryCount: 1
      JMSXUserID:    
      JMS_IBM_Character_Set: IBM037
      JMS_IBM_Encoding: 273
      JMS_IBM_Format: MQSTR   
      JMS_IBM_MsgType: 8
      JMS_IBM_PutApplType: 8
      JMS_IBM_PutDate: 20200120
      JMS_IBM_PutTime: 16214745
  ÍÍÑÀ ...
  </code></pre>
  
  <p>I would like to convert this EBCDIC message <code>ÍÍÑÀ ...</code> to something readable (ASCII).</p>
  
  <p>I tried to cast <code>enumeration.nextElement()</code> to <code>JMSByteMessage</code> but get this exception:</p>
  
  <pre><code>class com.ibm.msg.client.jms.internal.JmsTextMessageImpl cannot be cast to class com.ibm.jms.JMSBytesMessage
  </code></pre>
  
  <p>How could I do that?</p>
  
  <p>Solution:
  Use the <strong>MQ classes</strong> for Java instead of the <strong>MQ JMS classes</strong> for Java:</p>
  
  <pre><code>byte[] strData = new byte[theMessage.getMessageLength()];
  theMessage.readFully(strData, 0, theMessage.getMessageLength());
  </code></pre>
  
  <p>Some example here: <a href="https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.dev.doc/q030840_.htm" rel="nofollow noreferrer">https://www.ibm.com/support/knowledgecenter/en/SSFKSJ_7.5.0/com.ibm.mq.dev.doc/q030840_.htm</a></p>